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§

source§

impl<T> Arc<T>

source

pub fn new(item: T) -> Arc<T>

Create a new Arc.

Example
use portable_atomic_util::Arc;

let five = Arc::new(5);
source

pub fn pin(item: T) -> Pin<Arc<T>>

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

Example
use portable_atomic_util::Arc;

let five = Arc::pin(5);
source

pub fn try_unwrap(this: Self) -> Result<T, Self>

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());
source§

impl<T: ?Sized> Arc<T>

source

pub fn into_raw(self) -> *const T

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

Example
use portable_atomic_util::Arc;

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

// We should now free the pointer.
// SAFETY: The pointer is valid.
let five = unsafe { Arc::from_raw(five_ptr) };
assert_eq!(&*five, &5u8);
source

pub fn as_ptr(&self) -> *const T

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);
source

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

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);
source

pub fn downgrade(this: &Self) -> Weak<T>

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());
source

pub fn weak_count(this: &Self) -> usize

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);
source

pub fn strong_count(this: &Self) -> usize

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);
source

pub unsafe fn increment_strong_count(ptr: *const T)

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);
source

pub unsafe fn decrement_strong_count(ptr: *const T)

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) };
source

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

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));
source

pub fn get_mut(this: &mut Self) -> Option<&mut T>

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());
source§

impl<T: Clone> Arc<T>

source

pub fn unwrap_or_clone(this: Self) -> T

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§

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> Borrow<T> for Arc<T>

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
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: ?Sized + Debug> Debug for Arc<T>

source§

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

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

impl<T: Default> Default for Arc<T>

source§

fn default() -> Self

Returns the “default value” for a type. 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: ?Sized> Drop for Arc<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

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

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

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

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

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

source§

fn eq(&self, other: &Arc<U>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

source§

fn partial_cmp(&self, other: &Arc<U>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

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

source§

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

source§

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

source§

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

Auto Trait Implementations§

§

impl<T: ?Sized> RefUnwindSafe for Arc<T>
where T: RefUnwindSafe,

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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, U> Into<U> for T
where 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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.