On Sun Jul 13, 2025 at 11:12 PM CEST, Rahul Rameshbabu wrote: > +// SAFETY: Instances of `Device` are always reference-counted. > +unsafe impl crate::types::AlwaysRefCounted for Device { > + fn inc_ref(&self) { > + // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. > + unsafe { bindings::kref_get(&mut ((*self.as_raw()).ref_)) } I'm confused, what's the lifecycle of a struct hid_device? It embedds a struct device, so it also inherits its reference count. Additionally it also has a struct kref. Can you elaborate please? I don't know what the struct kref is for, but I'm pretty sure you want to manage the reference count of the embedded struct device here. > + } > + > + unsafe fn dec_ref(obj: NonNull<Self>) { > + // SAFETY: The safety requirements guarantee that the refcount is non-zero. > + unsafe { > + bindings::kref_put( > + &mut ((*obj.cast::<bindings::hid_device>().as_ptr()).ref_), I think you want &raw mut instead. > + Some(bindings::hiddev_free), > + ) > + } > + } > +} > + > +impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> { > + fn as_ref(&self) -> &device::Device<Ctx> { > + // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid > + // `struct hid_device`. > + let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) }; You also use &raw mut meanwhile. > + > + // SAFETY: `dev` points to a valid `struct device`. > + unsafe { device::Device::as_ref(dev) } > + } > +}