Struct portable_atomic_util::Arc
source · pub struct Arc<T: ?Sized> { /* private fields */ }
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>
impl<T> Arc<T>
sourcepub fn try_unwrap(this: Self) -> Result<T, Self>
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>
impl<T: ?Sized> Arc<T>
sourcepub fn into_raw(self) -> *const T
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);
sourcepub fn as_ptr(&self) -> *const T
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);
sourcepub unsafe fn from_raw(ptr: *const T) -> Self
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);
sourcepub fn weak_count(this: &Self) -> usize
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);
sourcepub fn strong_count(this: &Self) -> usize
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);
sourcepub unsafe fn increment_strong_count(ptr: *const T)
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);
sourcepub unsafe fn decrement_strong_count(ptr: *const T)
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) };
sourcepub fn ptr_eq(this: &Self, other: &Self) -> bool
pub fn ptr_eq(this: &Self, other: &Self) -> bool
Tell if two Arc
s 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));
sourcepub fn get_mut(this: &mut Self) -> Option<&mut T>
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());
Trait Implementations§
source§impl<T: ?Sized + Ord> Ord for Arc<T>
impl<T: ?Sized + Ord> Ord for Arc<T>
source§impl<U: ?Sized, T: ?Sized + PartialEq<U>> PartialEq<Arc<U>> for Arc<T>
impl<U: ?Sized, T: ?Sized + PartialEq<U>> PartialEq<Arc<U>> for Arc<T>
source§impl<U: ?Sized, T: ?Sized + PartialOrd<U>> PartialOrd<Arc<U>> for Arc<T>
impl<U: ?Sized, T: ?Sized + PartialOrd<U>> PartialOrd<Arc<U>> for Arc<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more