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
7979 /// - [`Normal`]: The general-purpose, reference-counted context. A [`Device`] in this context may
8080 /// or may not be registered with userspace.
8181 /// - [`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.
8285 pub trait DeviceContext: Sealed + Send + Sync + 'static {}
8386
8487 /// The general-purpose, reference-counted [`DeviceContext`].
@@ -320,7 +323,7 @@ impl<T: drm::Driver, C: DeviceContext> Device<T, C> { · 235 unmodified lines
320323 }
321324 }
322325
323−impl<T: drm::Driver, C: DeviceContext> Deref for Device<T, C> {
326+impl<T: drm::Driver> Deref for Device<T> {
324327 type Target = T::Data;
325328
326329 fn deref(&self) -> &Self::Target {
@@ -328,6 +331,17 @@ impl<T: drm::Driver, C: DeviceContext> Deref for Device<T, C> { · 1 unmodified line
328331 }
329332 }
330333
334+impl<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+
331345 // SAFETY: DRM device objects are always reference counted and the get/put functions
332346 // satisfy the requirements.
333347 unsafe impl<T: drm::Driver> AlwaysRefCounted for Device<T> {