[PATCH v2 4/8] rust: drm: add device abstraction
Danilo Krummrich
dakr at kernel.org
Thu Apr 17 20:20:07 UTC 2025
On Thu, Apr 17, 2025 at 02:53:33PM -0400, Lyude Paul wrote:
> On Fri, 2025-04-11 at 01:55 +0200, Danilo Krummrich wrote:
> > + /// Not intended to be called externally, except via declare_drm_ioctls!()
> > + ///
> > + /// # Safety
> > + ///
> > + /// Callers must ensure that `ptr` is valid, non-null, and has a non-zero reference count,
> > + /// i.e. it must be ensured that the reference count of the C `struct drm_device` `ptr` points
> > + /// to can't drop to zero, for the duration of this function call and the entire duration when
> > + /// the returned reference exists.
> > + ///
> > + /// Additionally, callers must ensure that the `struct device`, `ptr` is pointing to, is
> > + /// embedded in `Self`.
> > + #[doc(hidden)]
> > + pub unsafe fn as_ref<'a>(ptr: *const bindings::drm_device) -> &'a Self {
> > + // SAFETY: By the safety requirements of this function `ptr` is a valid pointer to a
> > + // `struct drm_device` embedded in `Self`.
> > + let ptr = unsafe { Self::from_drm_device(ptr) };
> > +
> > + // SAFETY: `ptr` is valid by the safety requirements of this function.
> > + unsafe { &*ptr.cast() }
> > + }
> > +}
>
> Hm. Are we sure that `as_ref()` is really the function name we want here? We
> already have AsRef<kernel::device::Device> implemented for DRM devices, I'm
> not sure if this function would cause a naming conflict since it doesn't use
> self but at the very least it does seem a little bit confusing.
>
> Why not call it either from_raw(), or (depending on what RFL people think)
> from_c()?
I chose as_ref() for consistency. Wherever we convert from some pointer type to
&Self, we use as_ref(); when we convert from some pointer type to Self we use
from_raw() (from_c() isn't really a thing). Hence I'd like to keep it as_ref().
More information about the dri-devel
mailing list