pub struct Arc<T: ?Sized> { /* private fields */ }
Available on crate features alloc or std only.
Expand description

A thread-safe, strongly reference counted pointer.

This is an equivalent to std::sync::Arc, but using portable-atomic for synchronization. See the documentation for the standard library’s Arc for more details.

Examples

use portable_atomic_util::Arc;
use std::thread;

let five = Arc::new(5);

for _ in 0..10 {
    let five = Arc::clone(&five);
    thread::spawn(move || {
        assert_eq!(*five, 5);
    });
}

Implementations§

Create a new Arc.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);

Create a new Arc whose pointer is pinned to the heap.

Example
use portable_atomic_util::Arc;

let five = Arc::pin(5);

Unwrap and try to get the inner value.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);
assert_eq!(Arc::try_unwrap(five).unwrap(), 5);

let five = Arc::new(5);
let five2 = Arc::clone(&five);
assert!(Arc::try_unwrap(five).is_err());

Consume this Arc and get the raw pointer to the inner value.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);
let five_ptr = Arc::into_raw(five);

// We should now free the pointer.
// SAFETY: The pointer is valid.
unsafe { Arc::from_raw(five_ptr) };

Get the raw pointer representing this Arc<T>.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);
let five_ptr = Arc::as_ptr(&five);

Convert a raw pointer previously created by into_raw into a new Arc.

Safety

This function can only be called with a pointer that was previously returned by into_raw.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);
let five_ptr = Arc::into_raw(five);

// SAFETY: The pointer is valid.
let five = unsafe { Arc::from_raw(five_ptr) };
assert_eq!(*five, 5);

Get a Weak reference from this Arc.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);
let weak_five = Arc::downgrade(&five);

assert!(weak_five.upgrade().is_some());

Get the number of weak pointers to this allocation.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);
let weak_five = Arc::downgrade(&five);

assert_eq!(Arc::weak_count(&five), 1);

Get the number of strong pointers to this allocation.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);
let five2 = Arc::clone(&five);

assert_eq!(Arc::strong_count(&five), 2);

Increment the strong count of the Arc pointed to by ptr by one.

Safety

The pointer must be a pointer previously returned by Arc::into_raw.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);
let five_ptr = Arc::into_raw(five);

// SAFETY: The pointer is valid.
unsafe { Arc::increment_strong_count(five_ptr) };

// SAFETY: The pointer is valid.
let five2 = unsafe { Arc::from_raw(five_ptr) };
assert_eq!(*five2, 5);

// SAFETY: Since the refcount is incremented, we can get another.
let five3 = unsafe { Arc::from_raw(five_ptr) };
assert_eq!(*five3, 5);

Decrement the strong count of the Arc pointed to by ptr by one.

Safety

The pointer must be a pointer previously returned by Arc::into_raw.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);
let five2 = Arc::clone(&five);

let five_ptr = Arc::into_raw(five);

// SAFETY: The pointer is valid.
unsafe { Arc::decrement_strong_count(five_ptr) };

Tell if two Arcs point to the same allocation.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);
let five2 = Arc::clone(&five);

assert!(Arc::ptr_eq(&five, &five2));

Get a mutable pointer to the inner value if there are no other strong references.

Example
use portable_atomic_util::Arc;

let mut five = Arc::new(5);
assert!(Arc::get_mut(&mut five).is_some());

let five2 = Arc::clone(&five);
assert!(Arc::get_mut(&mut five).is_none());

Try to get the inner value or clone it.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);
let five2 = Arc::clone(&five);

assert_eq!(Arc::unwrap_or_clone(five), 5);

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The resulting type after dereferencing.
Dereferences the value.
Executes the destructor for this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

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.