History

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).

Danilo Krummrich committed 4 months ago

Commit 1c8a1f81 parent 7f994b81 file · +15 −1Browse files

Showing the changes to rust/kernel (1 of 1 file). Show all

Unified · Split

rust/kernel/drm/device.rs+15−1
@@ -79,6 +79,9 @@ macro_rules! drm_legacy_fields { · 78 unmodified lines
79/// - [`Normal`]: The general-purpose, reference-counted context. A [`Device`] in this context may79/// - [`Normal`]: The general-purpose, reference-counted context. A [`Device`] in this context may
80/// or may not be registered with userspace.80/// or may not be registered with userspace.
81/// - [`Registered`]: The device has been registered with userspace at some point.81/// - [`Registered`]: The device has been registered with userspace at some point.
82///
83/// `Device<T, Registered>` dereferences to `Device<T>` ([`Normal`]), so any method available on a
84/// [`Normal`] device is also available on a [`Registered`] one.
82pub trait DeviceContext: Sealed + Send + Sync + 'static {}85pub trait DeviceContext: Sealed + Send + Sync + 'static {}
83 86
84/// The general-purpose, reference-counted [`DeviceContext`].87/// The general-purpose, reference-counted [`DeviceContext`].
@@ -320,7 +323,7 @@ impl<T: drm::Driver, C: DeviceContext> Device<T, C> { · 235 unmodified lines
320 }323 }
321}324}
322 325
323impl<T: drm::Driver, C: DeviceContext> Deref for Device<T, C> {326impl<T: drm::Driver> Deref for Device<T> {
324 type Target = T::Data;327 type Target = T::Data;
325 328
326 fn deref(&self) -> &Self::Target {329 fn deref(&self) -> &Self::Target {
@@ -328,6 +331,17 @@ impl<T: drm::Driver, C: DeviceContext> Deref for Device<T, C> { · 1 unmodified line
328 }331 }
329}332}
330 333
334impl<T: drm::Driver> Deref for Device<T, Registered> {
335 type Target = Device<T>;
336
337 #[inline]
338 fn deref(&self) -> &Self::Target {
339 // SAFETY: The caller holds a `Device<T, Registered>`, which guarantees all invariants
340 // of the weaker `Normal` context.
341 unsafe { self.assume_ctx() }
342 }
343}
344
331// SAFETY: DRM device objects are always reference counted and the get/put functions345// SAFETY: DRM device objects are always reference counted and the get/put functions
332// satisfy the requirements.346// satisfy the requirements.
333unsafe impl<T: drm::Driver> AlwaysRefCounted for Device<T> {347unsafe impl<T: drm::Driver> AlwaysRefCounted for Device<T> {