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

Constructs a new reference counted instance of T.

Determines if two reference-counted pointers point to the same underlying instance of T.

Deconstructs a Arc object into a raw pointer.

It can be reconstructed once via Arc::from_raw.

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

This code relies on the repr(C) layout of structs as described in https://doc.rust-lang.org/reference/type-layout.html#reprc-structs.

Safety

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

Returns a ArcBorrow from the given Arc.

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

Trait Implementations

Converts this type into a shared reference of the (usually inferred) input type.
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
The resulting type after dereferencing.
Dereferences the value.
Cleans resources up when the device is removed. Read more
Executes the destructor for this type. Read more
Type of values borrowed between calls to ForeignOwnable::into_foreign and ForeignOwnable::from_foreign. Read more
Converts a Rust-owned object to a foreign-owned one. Read more
Borrows a foreign-owned object. Read more
Converts a foreign-owned object back to a Rust-owned one. Read more
Mutably borrows a foreign-owned object. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
The type of the entries in the list.
Returns the links to be used when linking an entry within a list.
Specifies which wrapper (e.g., Box and Arc) wraps the list entries.
The type returned in the event of a conversion error.
Performs the conversion.
Converts the wrapped object into a pointer that represents it.
Converts the object back from the pointer representation. Read more
Returns a reference to the wrapped object.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.