Struct kernel::sync::UniqueArc

source ·
pub struct UniqueArc<T: ?Sized> { /* private fields */ }
Expand description

A refcounted object that is known to have a refcount of 1.

It is mutable and can be converted to an Arc so that it can be shared.

Invariants

inner always has a reference count of 1.

Examples

In the following example, we make changes to the inner object before turning it into an Arc<Test> object (after which point, it cannot be mutated directly). Note that x.into() cannot fail.

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

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

fn test() -> Result<Arc<Example>> {
    let mut x = UniqueArc::try_new(Example { a: 10, b: 20 })?;
    x.a += 1;
    x.b += 1;
    Ok(x.into())
}

In the following example we first allocate memory for a ref-counted Example but we don’t initialise it on allocation. We do initialise it later with a call to UniqueArc::write, followed by a conversion to Arc<Example>. This is particularly useful when allocation happens in one context (e.g., sleepable) and initialisation in another (e.g., atomic):

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

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

fn test() -> Result<Arc<Example>> {
    let x = UniqueArc::try_new_uninit()?;
    Ok(x.write(Example { a: 10, b: 20 }).into())
}

In the last example below, the caller gets a pinned instance of Example while converting to Arc<Example>; this is useful in scenarios where one needs a pinned reference during initialisation, for example, when initialising fields that are wrapped in locks.

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

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

fn test() -> Result<Arc<Example>> {
    let mut pinned = Pin::from(UniqueArc::try_new(Example { a: 10, b: 20 })?);
    // We can modify `pinned` because it is `Unpin`.
    pinned.as_mut().a += 1;
    Ok(pinned.into())
}

Implementations§

source§

impl<T> UniqueArc<T>

source

pub fn try_new(value: T) -> Result<Self, AllocError>

Tries to allocate a new UniqueArc instance.

source

pub fn try_new_uninit() -> Result<UniqueArc<MaybeUninit<T>>, AllocError>

Tries to allocate a new UniqueArc instance whose contents are not initialised yet.

source§

impl<T> UniqueArc<MaybeUninit<T>>

source

pub fn write(self, value: T) -> UniqueArc<T>

Converts a UniqueArc<MaybeUninit<T>> into a UniqueArc<T> by writing a value into it.

source

pub unsafe fn assume_init(self) -> UniqueArc<T>

Unsafely assume that self is initialized.

Safety

The caller guarantees that the value behind this pointer has been initialized. It is immediate UB to call this when the value is not initialized.

source

pub fn init_with<E>(self, init: impl Init<T, E>) -> Result<UniqueArc<T>, E>

Initialize self using the given initializer.

source

pub fn pin_init_with<E>( self, init: impl PinInit<T, E> ) -> Result<Pin<UniqueArc<T>>, E>

Pin-initialize self using the given pin-initializer.

Trait Implementations§

source§

impl<T: Debug + ?Sized> Debug for UniqueArc<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: ?Sized> Deref for UniqueArc<T>

§

type Target = T

The resulting type after dereferencing.
source§

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

Dereferences the value.
source§

impl<T: ?Sized> DerefMut for UniqueArc<T>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T: Display + ?Sized> Display for UniqueArc<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T: ?Sized> From<UniqueArc<T>> for Arc<T>

source§

fn from(item: UniqueArc<T>) -> Self

Converts to this type from the input type.
source§

impl<T: ?Sized> From<UniqueArc<T>> for Pin<UniqueArc<T>>

source§

fn from(obj: UniqueArc<T>) -> Self

Converts to this type from the input type.
source§

impl<T> InPlaceInit<T> for UniqueArc<T>

source§

fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>where E: From<AllocError>,

Use the given pin-initializer to pin-initialize a T inside of a new smart pointer of this type. Read more
source§

fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>where E: From<AllocError>,

Use the given initializer to in-place initialize a T.
source§

fn pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>>where Error: From<E>,

Use the given pin-initializer to pin-initialize a T inside of a new smart pointer of this type. Read more
source§

fn init<E>(init: impl Init<T, E>) -> Result<Self>where Error: From<E>,

Use the given initializer to in-place initialize a T.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for UniqueArc<T>

§

impl<T: ?Sized> Send for UniqueArc<T>where T: Send + Sync,

§

impl<T: ?Sized> Sync for UniqueArc<T>where T: Send + Sync,

§

impl<T: ?Sized> Unpin for UniqueArc<T>

§

impl<T> !UnwindSafe for UniqueArc<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.
This documentation is an old archive. Please see https://rust.docs.kernel.org instead.