#[repr(transparent)]pub struct Arc<T: ?Sized> { /* private fields */ }
Expand description
An atomically reference counted shared pointer
See the documentation for Arc
in the standard library. Unlike the
standard library Arc
, this Arc
does not support weak reference counting.
Implementations
sourceimpl<T> Arc<T>
impl<T> Arc<T>
sourcepub unsafe fn from_raw(ptr: *const T) -> Self
pub unsafe fn from_raw(ptr: *const T) -> Self
Reconstruct the Arc
Note: This raw pointer will be offset in the allocation and must be preceded by the atomic count.
It is recommended to use OffsetArc for this
sourcepub fn with_raw_offset_arc<F, U>(&self, f: F) -> Uwhere
F: FnOnce(&OffsetArc<T>) -> U,
pub fn with_raw_offset_arc<F, U>(&self, f: F) -> Uwhere
F: FnOnce(&OffsetArc<T>) -> U,
Temporarily converts |self| into a bonafide OffsetArc and exposes it to the provided callback. The refcount is not modified.
sourcepub fn into_raw_offset(a: Self) -> OffsetArc<T>
pub fn into_raw_offset(a: Self) -> OffsetArc<T>
Converts an Arc
into a OffsetArc
. This consumes the Arc
, so the refcount
is not modified.
sourcepub fn from_raw_offset(a: OffsetArc<T>) -> Self
pub fn from_raw_offset(a: OffsetArc<T>) -> Self
Converts a OffsetArc
into an Arc
. This consumes the OffsetArc
, so the refcount
is not modified.
sourcepub fn try_unwrap(this: Self) -> Result<T, Self>
pub fn try_unwrap(this: Self) -> Result<T, Self>
Returns the inner value, if the Arc
has exactly one strong reference.
Otherwise, an Err
is returned with the same Arc
that was
passed in.
Examples
use triomphe::Arc;
let x = Arc::new(3);
assert_eq!(Arc::try_unwrap(x), Ok(3));
let x = Arc::new(4);
let _y = Arc::clone(&x);
assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4);
sourceimpl<T: ?Sized> Arc<T>
impl<T: ?Sized> Arc<T>
sourcepub fn into_raw(this: Self) -> *const T
pub fn into_raw(this: Self) -> *const T
Convert the Arc
Note: This returns a pointer to the data T, which is offset in the allocation.
It is recommended to use OffsetArc for this.
sourcepub fn as_ptr(&self) -> *const T
pub fn as_ptr(&self) -> *const T
Returns the raw pointer.
Same as into_raw except self
isn’t consumed.
sourcepub fn borrow_arc(&self) -> ArcBorrow<'_, T>
pub fn borrow_arc(&self) -> ArcBorrow<'_, T>
Produce a pointer to the data that can be converted back
to an Arc. This is basically an &Arc<T>
, without the extra indirection.
It has the benefits of an &T
but also knows about the underlying refcount
and can be converted into more Arc<T>
s if necessary.
sourceimpl<T> Arc<MaybeUninit<T>>
impl<T> Arc<MaybeUninit<T>>
sourcepub fn new_uninit() -> Self
pub fn new_uninit() -> Self
Create an Arc contains an MaybeUninit<T>
.
sourcepub fn write(&mut self, val: T) -> &mut T
👎Deprecated since 0.1.7: this function previously was UB and now panics for non-unique Arc
s. Use UniqueArc::write
instead.
pub fn write(&mut self, val: T) -> &mut T
this function previously was UB and now panics for non-unique Arc
s. Use UniqueArc::write
instead.
sourcepub fn as_mut_ptr(&mut self) -> *mut MaybeUninit<T>
pub fn as_mut_ptr(&mut self) -> *mut MaybeUninit<T>
Obtain a mutable pointer to the stored MaybeUninit<T>
.
sourcepub unsafe fn assume_init(self) -> Arc<T>
pub unsafe fn assume_init(self) -> Arc<T>
Safety
Must initialize all fields before calling this function.
sourceimpl<T> Arc<[MaybeUninit<T>]>
impl<T> Arc<[MaybeUninit<T>]>
sourcepub fn new_uninit_slice(len: usize) -> Self
pub fn new_uninit_slice(len: usize) -> Self
Create an Arc contains an array [MaybeUninit<T>]
of len
.
sourcepub fn as_mut_slice(&mut self) -> &mut [MaybeUninit<T>]
👎Deprecated since 0.1.8: this function previously was UB and now panics for non-unique Arc
s. Use UniqueArc
or get_mut
instead.
pub fn as_mut_slice(&mut self) -> &mut [MaybeUninit<T>]
this function previously was UB and now panics for non-unique Arc
s. Use UniqueArc
or get_mut
instead.
Obtain a mutable slice to the stored [MaybeUninit<T>]
.
sourcepub unsafe fn assume_init(self) -> Arc<[T]>
pub unsafe fn assume_init(self) -> Arc<[T]>
Safety
Must initialize all fields before calling this function.
sourceimpl<T: Clone> Arc<T>
impl<T: Clone> Arc<T>
sourcepub fn make_mut(this: &mut Self) -> &mut T
pub fn make_mut(this: &mut Self) -> &mut T
Makes a mutable reference to the Arc
, cloning if necessary
This is functionally equivalent to Arc::make_mut
from the standard library.
If this Arc
is uniquely owned, make_mut()
will provide a mutable
reference to the contents. If not, make_mut()
will create a new Arc
with a copy of the contents, update this
to point to it, and provide
a mutable reference to its contents.
This is useful for implementing copy-on-write schemes where you wish to
avoid copying things if your Arc
is not shared.
sourcepub fn make_unique(this: &mut Self) -> &mut UniqueArc<T>
pub fn make_unique(this: &mut Self) -> &mut UniqueArc<T>
Makes a UniqueArc
from an Arc
, cloning if necessary.
If this Arc
is uniquely owned, make_unique()
will provide a UniqueArc
containing this
. If not, make_unique()
will create a new Arc
with a copy of the contents, update this
to point to it, and provide
a UniqueArc
to it.
This is useful for implementing copy-on-write schemes where you wish to
avoid copying things if your Arc
is not shared.
sourcepub fn unwrap_or_clone(this: Arc<T>) -> T
pub fn unwrap_or_clone(this: Arc<T>) -> T
If we have the only reference to T
then unwrap it. Otherwise, clone T
and return the clone.
Assuming arc_t
is of type Arc<T>
, this function is functionally equivalent to (*arc_t).clone()
, but will avoid cloning the inner value where possible.
sourceimpl<T: ?Sized> Arc<T>
impl<T: ?Sized> Arc<T>
sourcepub fn get_mut(this: &mut Self) -> Option<&mut T>
pub fn get_mut(this: &mut Self) -> Option<&mut T>
Provides mutable access to the contents if the Arc
is uniquely owned.
sourcepub fn get_unique(this: &mut Self) -> Option<&mut UniqueArc<T>>
pub fn get_unique(this: &mut Self) -> Option<&mut UniqueArc<T>>
Provides unique access to the arc if the Arc
is uniquely owned.
sourcepub fn try_unique(this: Self) -> Result<UniqueArc<T>, Self>
pub fn try_unique(this: Self) -> Result<UniqueArc<T>, Self>
Returns a UniqueArc
if the Arc
has exactly one strong reference.
Otherwise, an Err
is returned with the same Arc
that was
passed in.
Examples
use triomphe::{Arc, UniqueArc};
let x = Arc::new(3);
assert_eq!(UniqueArc::into_inner(Arc::try_unique(x).unwrap()), 3);
let x = Arc::new(4);
let _y = Arc::clone(&x);
assert_eq!(
*Arc::try_unique(x).map(UniqueArc::into_inner).unwrap_err(),
4,
);
sourceimpl<H, T> Arc<HeaderSlice<H, [T]>>
impl<H, T> Arc<HeaderSlice<H, [T]>>
sourcepub fn from_header_and_iter<I>(header: H, items: I) -> Selfwhere
I: Iterator<Item = T> + ExactSizeIterator,
pub fn from_header_and_iter<I>(header: H, items: I) -> Selfwhere
I: Iterator<Item = T> + ExactSizeIterator,
Creates an Arc for a HeaderSlice using the given header struct and iterator to generate the slice. The resulting Arc will be fat.
sourcepub fn from_header_and_slice(header: H, items: &[T]) -> Selfwhere
T: Copy,
pub fn from_header_and_slice(header: H, items: &[T]) -> Selfwhere
T: Copy,
Creates an Arc for a HeaderSlice using the given header struct and iterator to generate the slice. The resulting Arc will be fat.
sourcepub fn from_header_and_vec(header: H, v: Vec<T>) -> Self
pub fn from_header_and_vec(header: H, v: Vec<T>) -> Self
Creates an Arc for a HeaderSlice using the given header struct and vec to generate the slice. The resulting Arc will be fat.
sourceimpl<H> Arc<HeaderSlice<H, str>>
impl<H> Arc<HeaderSlice<H, str>>
sourcepub fn from_header_and_str(header: H, string: &str) -> Self
pub fn from_header_and_str(header: H, string: &str) -> Self
Creates an Arc for a HeaderSlice using the given header struct and a str slice to generate the slice. The resulting Arc will be fat.