Now that we have more then one way to refer to connectors, we also want to ensure that any methods which are common to any kind of connector type can be used on all connector representations. This is where RawConnector and RawConnectorState come in: we implement these traits for any type which implements AsRawConnector or AsRawConnectorState respectively. Signed-off-by: Lyude Paul <lyude@xxxxxxxxxx> --- rust/kernel/drm/kms/connector.rs | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/rust/kernel/drm/kms/connector.rs b/rust/kernel/drm/kms/connector.rs index 98ac7fb781d4e..f62740d7f6469 100644 --- a/rust/kernel/drm/kms/connector.rs +++ b/rust/kernel/drm/kms/connector.rs @@ -331,6 +331,27 @@ unsafe fn from_raw<'a>(ptr: *mut bindings::drm_connector) -> &'a Self { } } +/// Common methods available on any type which implements [`AsRawConnector`]. +/// +/// This is implemented internally by DRM, and provides many of the basic methods for working with +/// connectors. +pub trait RawConnector: AsRawConnector { + /// Return the index of this DRM connector + #[inline] + fn index(&self) -> u32 { + // SAFETY: The index is initialized by the time we expose DRM connector objects to users, + // and is invariant throughout the lifetime of the connector + unsafe { (*self.as_raw()).index } + } + + /// Return the bitmask derived from this DRM connector's index + #[inline] + fn mask(&self) -> u32 { + 1 << self.index() + } +} +impl<T: AsRawConnector> RawConnector for T {} + unsafe extern "C" fn connector_destroy_callback<T: DriverConnector>( connector: *mut bindings::drm_connector, ) { @@ -523,6 +544,19 @@ pub trait FromRawConnectorState: AsRawConnectorState { unsafe fn from_raw_mut<'a>(ptr: *mut bindings::drm_connector_state) -> &'a mut Self; } +/// Common methods available on any type which implements [`AsRawConnectorState`]. +/// +/// This is implemented internally by DRM, and provides many of the basic methods for working with +/// the atomic state of [`Connector`]s. +pub trait RawConnectorState: AsRawConnectorState { + fn connector(&self) -> &Self::Connector { + // SAFETY: This is guaranteed safe by type invariance, and we're guaranteed by DRM that + // `self.state.connector` points to a valid instance of a `Connector<T>` + unsafe { Self::Connector::from_raw((*self.as_raw()).connector) } + } +} +impl<T: AsRawConnectorState> RawConnectorState for T {} + /// The main interface for a [`struct drm_connector_state`]. /// /// This type is the main interface for dealing with the atomic state of DRM connectors. In -- 2.46.1