From 1c8a1f88ac32a987643bf2d534ef4aa2d3da3aeb Mon Sep 17 00:00:00 2001 From: Danilo Krummrich Date: Sun, 28 Jun 2026 16:53:29 +0200 Subject: rust: drm: split Deref for Device context typestates Split the Deref implementation for drm::Device by context: - Device (Normal) dereferences to T::Data. - Device dereferences to Device (Normal). Reviewed-by: Lyude Paul Reviewed-by: Alexandre Courbot Tested-by: Deborah Brouwer Link: https://patch.msgid.link/20260628145406.2107056-10-dakr@kernel.org Signed-off-by: Danilo Krummrich --- rust/kernel/drm/device.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'rust') diff --git a/rust/kernel/drm/device.rs b/rust/kernel/drm/device.rs index eb8146ea1c98..f5342de190f4 100644 --- a/rust/kernel/drm/device.rs +++ b/rust/kernel/drm/device.rs @@ -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` dereferences to `Device` ([`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 @@ impl Device { } } -impl Deref for Device { +impl Deref for Device { type Target = T::Data; fn deref(&self) -> &Self::Target { @@ -328,6 +331,17 @@ impl Deref for Device { } } +impl Deref for Device { + type Target = Device; + + #[inline] + fn deref(&self) -> &Self::Target { + // SAFETY: The caller holds a `Device`, 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 AlwaysRefCounted for Device { -- cgit v1.2.3