Struct kernel::sync::ArcBorrow

source ·
pub struct ArcBorrow<'a, T: ?Sized + 'a> { /* private fields */ }
Expand description

A borrowed reference to an Arc instance.

For cases when one doesn’t ever need to increment the refcount on the allocation, it is simpler to use just &T, which we can trivially get from an Arc<T> instance.

However, when one may need to increment the refcount, it is preferable to use an ArcBorrow<T> over &Arc<T> because the latter results in a double-indirection: a pointer (shared reference) to a pointer (Arc<T>) to the object (T). An ArcBorrow eliminates this double indirection while still allowing one to increment the refcount and getting an Arc<T> when/if needed.

Invariants

There are no mutable references to the underlying Arc, and it remains valid for the lifetime of the ArcBorrow instance.

Example

use kernel::sync::{Arc, ArcBorrow};

struct Example;

fn do_something(e: ArcBorrow<'_, Example>) -> Arc<Example> {
    e.into()
}

let obj = Arc::try_new(Example)?;
let cloned = do_something(obj.as_arc_borrow());

// Assert that both `obj` and `cloned` point to the same underlying object.
assert!(core::ptr::eq(&*obj, &*cloned));

Using ArcBorrow<T> as the type of self:

use kernel::sync::{Arc, ArcBorrow};

struct Example {
    a: u32,
    b: u32,
}

impl Example {
    fn use_reference(self: ArcBorrow<'_, Self>) {
        // ...
    }
}

let obj = Arc::try_new(Example { a: 10, b: 20 })?;
obj.as_arc_borrow().use_reference();

Trait Implementations§

source§

impl<T: ?Sized> Clone for ArcBorrow<'_, T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: ?Sized> Deref for ArcBorrow<'_, T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T: ?Sized> From<ArcBorrow<'_, T>> for Arc<T>

source§

fn from(b: ArcBorrow<'_, T>) -> Self

Converts to this type from the input type.
source§

impl<T: ?Sized> Copy for ArcBorrow<'_, T>

source§

impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<ArcBorrow<'_, U>> for ArcBorrow<'_, T>

Auto Trait Implementations§

§

impl<'a, T> !RefUnwindSafe for ArcBorrow<'a, T>

§

impl<'a, T> !Send for ArcBorrow<'a, T>

§

impl<'a, T> !Sync for ArcBorrow<'a, T>

§

impl<'a, T: ?Sized> Unpin for ArcBorrow<'a, T>

§

impl<'a, T> !UnwindSafe for ArcBorrow<'a, T>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.