rust: drm: split Deref for Device context typestates

Split the Deref implementation for drm::Device by context:

  - Device<T> (Normal) dereferences to T::Data.
  - Device<T, Registered> dereferences to Device<T> (Normal).

Reviewed-by: Lyude Paul <lyude@redhat.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Tested-by: Deborah Brouwer <deborah.brouwer@collabora.com>
Link: https://patch.msgid.link/20260628145406.2107056-10-dakr@kernel.org
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
Danilo Krummrich
2026-06-28 16:53:29 +02:00
parent 7f994b8912
commit 1c8a1f88ac

View File

@@ -79,6 +79,9 @@ macro_rules! drm_legacy_fields {
/// - [`Normal`]: The general-purpose, reference-counted context. A [`Device`] in this context may
/// or may not be registered with userspace.
/// - [`Registered`]: The device has been registered with userspace at some point.
///
/// `Device<T, Registered>` dereferences to `Device<T>` ([`Normal`]), so any method available on a
/// [`Normal`] device is also available on a [`Registered`] one.
pub trait DeviceContext: Sealed + Send + Sync + 'static {}
/// The general-purpose, reference-counted [`DeviceContext`].
@@ -320,7 +323,7 @@ pub(crate) unsafe fn assume_ctx<NewCtx: DeviceContext>(&self) -> &Device<T, NewC
}
}
impl<T: drm::Driver, C: DeviceContext> Deref for Device<T, C> {
impl<T: drm::Driver> Deref for Device<T> {
type Target = T::Data;
fn deref(&self) -> &Self::Target {
@@ -328,6 +331,17 @@ fn deref(&self) -> &Self::Target {
}
}
impl<T: drm::Driver> Deref for Device<T, Registered> {
type Target = Device<T>;
#[inline]
fn deref(&self) -> &Self::Target {
// SAFETY: The caller holds a `Device<T, Registered>`, which guarantees all invariants
// of the weaker `Normal` context.
unsafe { self.assume_ctx() }
}
}
// SAFETY: DRM device objects are always reference counted and the get/put functions
// satisfy the requirements.
unsafe impl<T: drm::Driver> AlwaysRefCounted for Device<T> {