Struct kernel::sync::Arc

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

A reference-counted pointer to an instance of T.

The reference count is incremented when new instances of Arc are created, and decremented when they are dropped. When the count reaches zero, the underlying T is also dropped.

Invariants

The reference count on an instance of Arc is always non-zero. The object pointed to by Arc is always pinned.

Examples

use kernel::sync::Arc;

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

// Create a ref-counted instance of `Example`.
let obj = Arc::try_new(Example { a: 10, b: 20 })?;

// Get a new pointer to `obj` and increment the refcount.
let cloned = obj.clone();

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

// Destroy `obj` and decrement its refcount.
drop(obj);

// Check that the values are still accessible through `cloned`.
assert_eq!(cloned.a, 10);
assert_eq!(cloned.b, 20);

// The refcount drops to zero when `cloned` goes out of scope, and the memory is freed.

Using Arc<T> as the type of self:

use kernel::sync::Arc;

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

impl Example {
    fn take_over(self: Arc<Self>) {
        // ...
    }

    fn use_reference(self: &Arc<Self>) {
        // ...
    }
}

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

Coercion from Arc<Example> to Arc<dyn MyTrait>:

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

trait MyTrait {
    // Trait has a function whose `self` type is `Arc<Self>`.
    fn example1(self: Arc<Self>) {}

    // Trait has a function whose `self` type is `ArcBorrow<'_, Self>`.
    fn example2(self: ArcBorrow<'_, Self>) {}
}

struct Example;
impl MyTrait for Example {}

// `obj` has type `Arc<Example>`.
let obj: Arc<Example> = Arc::try_new(Example)?;

// `coerced` has type `Arc<dyn MyTrait>`.
let coerced: Arc<dyn MyTrait> = obj;

Implementations§

source§

impl Arc<dyn Any + Send + Sync>

source

pub fn downcast<T>(self) -> Result<Arc<T>, Self>where T: Any + Send + Sync,

Attempt to downcast the Arc<dyn Any + Send + Sync> to a concrete type.

source§

impl<T> Arc<T>

source

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

Constructs a new reference counted instance of T.

source

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

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

If T: !Unpin it will not be able to move afterwards.

source

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

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

This is equivalent to Arc<T>::pin_init, since an Arc is always pinned.

source§

impl<T: ?Sized> Arc<T>

source

pub fn into_raw(self) -> *const T

Convert the Arc into a raw pointer.

The raw pointer has ownership of the refcount that this Arc object owned.

source

pub unsafe fn from_raw(ptr: *const T) -> Self

Recreates an Arc instance previously deconstructed via Arc::into_raw.

Safety

ptr must have been returned by a previous call to Arc::into_raw. Additionally, it must not be called more than once for each previous call to Arc::into_raw.

source

pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T>

Returns an ArcBorrow from the given Arc.

This is useful when the argument of a function call is an ArcBorrow (e.g., in a method receiver), but we have an Arc instead. Getting an ArcBorrow is free when optimised.

source

pub fn ptr_eq(this: &Self, other: &Self) -> bool

Compare whether two Arc pointers reference the same underlying object.

Trait Implementations§

source§

impl<T: ?Sized> AsRef<T> for Arc<T>

source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T: ?Sized> Clone for Arc<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: Debug + ?Sized> Debug for Arc<T>

source§

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

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

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

§

type Target = T

The resulting type after dereferencing.
source§

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

Dereferences the value.
source§

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

source§

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

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

impl<T: ?Sized> Drop for Arc<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T: 'static> ForeignOwnable for Arc<T>

§

type Borrowed<'a> = ArcBorrow<'a, T>

Type of values borrowed between calls to ForeignOwnable::into_foreign and ForeignOwnable::from_foreign.
source§

fn into_foreign(self) -> *const c_void

Converts a Rust-owned object to a foreign-owned one. Read more
source§

unsafe fn borrow<'a>(ptr: *const c_void) -> ArcBorrow<'a, T>

Borrows a foreign-owned object. Read more
source§

unsafe fn from_foreign(ptr: *const c_void) -> Self

Converts a foreign-owned object back to a Rust-owned one. Read more
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> From<Pin<UniqueArc<T>>> for Arc<T>

source§

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

Converts to this type from the input type.
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, const ID: u64> RawWorkItem<ID> for Arc<T>where T: WorkItem<ID, Pointer = Self> + HasWork<T, ID>,

§

type EnqueueOutput = Result<(), Arc<T>>

The return type of Queue::enqueue.
source§

unsafe fn __enqueue<F>(self, queue_work_on: F) -> Self::EnqueueOutputwhere F: FnOnce(*mut work_struct) -> bool,

Enqueues this work item on a queue using the provided queue_work_on method. Read more
source§

impl<T, const ID: u64> WorkItemPointer<ID> for Arc<T>where T: WorkItem<ID, Pointer = Self> + HasWork<T, ID>,

source§

unsafe extern "C" fn run(ptr: *mut work_struct)

Run this work item. Read more
source§

impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Arc<U>> for Arc<T>

source§

impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Arc<U>> for Arc<T>

source§

impl<T: ?Sized + Sync + Send> Send for Arc<T>

source§

impl<T: ?Sized + Sync + Send> Sync for Arc<T>

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Arc<T>

§

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

§

impl<T> !UnwindSafe for Arc<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.