Re: [PATCH v6 1/2] rust: core abstractions for HID drivers
From: Gary Guo
Date: Sun Mar 01 2026 - 08:18:21 EST
On Sun Mar 1, 2026 at 6:48 AM GMT, Rahul Rameshbabu wrote:
> On Fri, 27 Feb, 2026 15:27:59 +0000 "Gary Guo" <gary@xxxxxxxxxxx> wrote:
>> On Sun Feb 22, 2026 at 9:56 PM GMT, Rahul Rameshbabu wrote:
>>> These abstractions enable the development of HID drivers in Rust by binding
>>> with the HID core C API. They provide Rust types that map to the
>>> equivalents in C. In this initial draft, only hid_device and hid_device_id
>>> are provided direct Rust type equivalents. hid_driver is specially wrapped
>>> with a custom Driver type. The module_hid_driver! macro provides analogous
>>> functionality to its C equivalent. Only the .report_fixup callback is
>>> binded to Rust so far.
>>>
>>> Future work for these abstractions would include more bindings for common
>>> HID-related types, such as hid_field, hid_report_enum, and hid_report as
>>> well as more bus callbacks. Providing Rust equivalents to useful core HID
>>> functions will also be necessary for HID driver development in Rust.
>>>
>>> Signed-off-by: Rahul Rameshbabu <sergeantsagara@xxxxxxxxxxxxxx>
[snip]
>>> +pub trait Driver: Send {
>>> + /// The type holding information about each device id supported by the driver.
>>> + // TODO: Use `associated_type_defaults` once stabilized:
>>> + //
>>> + // ```
>>> + // type IdInfo: 'static = ();
>>> + // ```
>>> + type IdInfo: 'static;
>>> +
>>> + /// The table of device ids supported by the driver.
>>> + const ID_TABLE: IdTable<Self::IdInfo>;
>>> +
>>> + /// Called before report descriptor parsing. Can be used to mutate the
>>> + /// report descriptor before the core HID logic processes the descriptor.
>>> + /// Useful for problematic report descriptors that prevent HID devices from
>>> + /// functioning correctly.
>>> + ///
>>> + /// Optional to implement.
>>> + fn report_fixup<'a, 'b: 'a>(_hdev: &Device<device::Core>, _rdesc: &'b mut [u8]) -> &'a [u8] {
>>
>> I think this can just use a single lifetime?
>>
>
> I think the problem is when a driver decides to replace the _rdesc with
> a static lifetime report descriptor declared in the device driver. My
> example driver does not do this, but this is another common pattern in C
> HID drivers. This is why two separate lifetimes were required.
>
Lifetime on references are covariant, so it is okay to convert `&'static [u8]`
to `&'b mut [u8]`.
Best,
Gary