Struct kernel::device::Device

source ·
pub struct Device(/* private fields */);
Expand description

A reference-counted device.

This structure represents the Rust abstraction for a C struct device. This implementation abstracts the usage of an already existing C struct device within Rust code that we get passed from the C side.

An instance of this abstraction can be obtained temporarily or permanent.

A temporary one is bound to the lifetime of the C struct device pointer used for creation. A permanent instance is always reference-counted and hence not restricted by any lifetime boundaries.

For subsystems it is recommended to create a permanent instance to wrap into a subsystem specific device structure (e.g. pci::Device). This is useful for passing it to drivers in T::probe(), such that a driver can store the ARef<Device> (equivalent to storing a struct device pointer in a C driver) for arbitrary purposes, e.g. allocating DMA coherent memory.

§Invariants

A Device instance represents a valid struct device created by the C portion of the kernel.

Instances of this type are always reference-counted, that is, a call to get_device ensures that the allocation remains valid at least until the matching call to put_device.

bindings::device::release is valid to be called from any thread, hence ARef<Device> can be dropped from any thread.

Implementations§

source§

impl Device

source

pub unsafe fn from_raw(ptr: *mut device) -> ARef<Self>

Creates a new reference-counted abstraction instance of an existing struct device pointer.

§Safety

Callers must ensure that ptr is valid, non-null, and has a non-zero reference count, i.e. it must be ensured that the reference count of the C struct device ptr points to can’t drop to zero, for the duration of this function call.

It must also be ensured that bindings::device::release can be called from any thread. While not officially documented, this should be the case for any struct device.

source

pub unsafe fn as_ref<'a>(ptr: *mut device) -> &'a Self

Convert a raw C struct device pointer to a &'a Device.

§Safety

Callers must ensure that ptr is valid, non-null, and has a non-zero reference count, i.e. it must be ensured that the reference count of the C struct device ptr points to can’t drop to zero, for the duration of this function call and the entire duration when the returned reference exists.

Trait Implementations§

source§

impl AlwaysRefCounted for Device

source§

fn inc_ref(&self)

Increments the reference count on the object.
source§

unsafe fn dec_ref(obj: NonNull<Self>)

Decrements the reference count on the object. Read more
source§

impl Send for Device

source§

impl Sync for Device

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, E> Init<T, E> for T

source§

unsafe fn __init(self, slot: *mut T) -> Result<(), E>

Initializes slot. Read more
source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, E> PinInit<T, E> for T

source§

unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E>

Initializes slot. Read more
source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.