pub struct Arc<T: ?Sized, S: BackdropStrategy<Box<ArcInner<T>>>> { /* 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.
This Arc
allows customizing how it is dropped.
An backdrop_arc::Arc<T, S>
behaves much like a Arc<backdrop::Backdrop<Box<T>, S>>
,
in that the backdrop strategy is executed when the last Arc clone goes out of scope.
The difference with Arc<backdrop::Backdrop<Box<T>, S>>
is that there is no double pointer-indirection (arc -> box -> T), managing the allocated T
is done directly in the Arc.
Basic usage is as follows:
use backdrop_arc::Arc;
use backdrop_arc::{DebugStrategy, TrivialStrategy};
// Either specify the return type:
let mynum: Arc<usize, DebugStrategy<TrivialStrategy>> = Arc::new(42);
// Or use the 'Turbofish' syntax on the function call:
let mynum2 = Arc::<_, DebugStrategy<TrivialStrategy>>::new(42);
assert_eq!(mynum, mynum2);
// <- Because we are using the DebugStrategy, info is printed when the arcs go out of scope
See backdrop::Backdrop
for more info.
Implementations§
Source§impl<T, S: BackdropStrategy<Box<ArcInner<T>>>> Arc<T, S>
impl<T, S: BackdropStrategy<Box<ArcInner<T>>>> Arc<T, S>
Sourcepub fn with_strategy<S2: BackdropStrategy<Box<ArcInner<T>>>>(
arc: Arc<T, S>,
) -> Arc<T, S2>
pub fn with_strategy<S2: BackdropStrategy<Box<ArcInner<T>>>>( arc: Arc<T, S>, ) -> Arc<T, S2>
Alter the strategy that is used for an Arc<T, S> to another. This is a zero-cost operation.
Sourcepub unsafe fn from_raw(ptr: *const T) -> Self
pub unsafe fn from_raw(ptr: *const T) -> Self
Reconstruct the Arc<T, S> from a raw pointer obtained from into_raw()
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) -> U
pub fn with_raw_offset_arc<F, U>(&self, f: F) -> 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, S>
pub fn into_raw_offset(a: Self) -> OffsetArc<T, S>
Converts an Arc
into a OffsetArc
. This consumes the Arc
, so the refcount
is not modified.
Sourcepub fn from_raw_offset(a: OffsetArc<T, S>) -> Self
pub fn from_raw_offset(a: OffsetArc<T, S>) -> 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 backdrop_arc::{Arc, TrivialStrategy};
let x: Arc<usize, TrivialStrategy> = Arc::new(3);
assert_eq!(Arc::try_unwrap(x), Ok(3));
let x: Arc<usize, TrivialStrategy> = Arc::new(4);
let _y = Arc::clone(&x);
assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4);
Source§impl<T, S: BackdropStrategy<Box<ArcInner<[T]>>>> Arc<[T], S>
impl<T, S: BackdropStrategy<Box<ArcInner<[T]>>>> Arc<[T], S>
Sourcepub unsafe fn from_raw_slice(ptr: *const [T]) -> Self
pub unsafe fn from_raw_slice(ptr: *const [T]) -> Self
Reconstruct the Arc<[T]>
from a raw pointer obtained from into_raw()
.
Arc::from_raw
should accept unsized types, but this is not trivial to do correctly
until the feature pointer_bytes_offsets
is stabilized. This is stopgap solution for slices.
Source§impl<T: ?Sized, S: BackdropStrategy<Box<ArcInner<T>>>> Arc<T, S>
impl<T: ?Sized, S: BackdropStrategy<Box<ArcInner<T>>>> Arc<T, S>
Sourcepub fn into_raw(this: Self) -> *const T
pub fn into_raw(this: Self) -> *const T
Convert the Arc<T, S> to a raw pointer, suitable for use across FFI
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, S>
, 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>
s if necessary.
Source§impl<T, S> Arc<MaybeUninit<T>, S>
impl<T, S> Arc<MaybeUninit<T>, S>
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
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, S>
pub unsafe fn assume_init(self) -> Arc<T, S>
§Safety
Must initialize all fields before calling this function.
Source§impl<T, S> Arc<[MaybeUninit<T>], S>where
S: BackdropStrategy<Box<ArcInner<HeaderSlice<(), [MaybeUninit<T>]>>>> + BackdropStrategy<Box<ArcInner<[MaybeUninit<T>]>>> + BackdropStrategy<Box<ArcInner<[T]>>>,
impl<T, S> Arc<[MaybeUninit<T>], S>where
S: BackdropStrategy<Box<ArcInner<HeaderSlice<(), [MaybeUninit<T>]>>>> + BackdropStrategy<Box<ArcInner<[MaybeUninit<T>]>>> + BackdropStrategy<Box<ArcInner<[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>]
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], S>
pub unsafe fn assume_init(self) -> Arc<[T], S>
§Safety
Must initialize all fields before calling this function.
Source§impl<T: ?Sized, S> Arc<T, S>
impl<T: ?Sized, S> Arc<T, S>
Sourcepub fn clone_many<'a>(this: &'a Self, count: usize) -> ArcCloneIter<'a, T, S> ⓘ
pub fn clone_many<'a>(this: &'a Self, count: usize) -> ArcCloneIter<'a, T, S> ⓘ
Optimization over calling clone()
many times:
Instead of incrementing the reference count by one many times
(which requires an atomic barrier each time)
we increase the reference count by inc
once,
needing only a single atomic barrier.
§Failure scenarios
- Aborts if increasing the reference count by
inc
results in a refcount higher than isize::MAX, to make sure the refcount never overflows. (The only way to trigger this in a program is bymem::forget
ting Arcs in a loop).
§Examples
The resulting iterator gives out exactly count
Arc
s:
use backdrop_arc::{Arc, TrivialStrategy};
let myarc: Arc<u32, TrivialStrategy> = Arc::new(42);
let many_clones: Vec<_> = Arc::clone_many(&myarc, 1000).collect();
assert_eq!(Arc::count(&myarc), 1001);
If the iterator is dropped before all of them are given out, the reference count is decreased by the leftover amount (also in one atomic barier):
use backdrop_arc::{Arc, TrivialStrategy};
let myarc: Arc<u32, TrivialStrategy> = Arc::new(42);
let many_clones: Vec<_> = Arc::clone_many(&myarc, 1000).take(100).collect();
assert_eq!(Arc::count(&myarc), 101);
Source§impl<T: Clone, S> Arc<T, S>
impl<T: Clone, S> Arc<T, S>
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, S>
pub fn make_unique(this: &mut Self) -> &mut UniqueArc<T, S>
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, S>) -> T
pub fn unwrap_or_clone(this: Arc<T, S>) -> 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, S>
, this function is functionally equivalent to (*arc_t).clone()
, but will avoid cloning the inner value where possible.
Source§impl<T: ?Sized, S> Arc<T, S>
impl<T: ?Sized, S> Arc<T, S>
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, S>>
pub fn get_unique(this: &mut Self) -> Option<&mut UniqueArc<T, S>>
Provides unique access to the arc if the Arc
is uniquely owned.
Sourcepub fn try_unique(this: Self) -> Result<UniqueArc<T, S>, Self>
pub fn try_unique(this: Self) -> Result<UniqueArc<T, S>, 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 backdrop_arc::{Arc, UniqueArc, TrivialStrategy};
let x: Arc<usize, TrivialStrategy> = Arc::new(3);
assert_eq!(UniqueArc::into_inner(Arc::try_unique(x).unwrap()), 3);
let x: Arc<usize, TrivialStrategy> = Arc::new(4);
let _y = Arc::clone(&x);
assert_eq!(
*Arc::try_unique(x).map(UniqueArc::into_inner).unwrap_err(),
4,
);
Source§impl<H, T, S> Arc<HeaderSlice<H, [T]>, S>
impl<H, T, S> Arc<HeaderSlice<H, [T]>, S>
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.
Source§impl<H, S> Arc<HeaderSlice<H, str>, S>
impl<H, S> Arc<HeaderSlice<H, str>, S>
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.
Trait Implementations§
Source§impl<T, U: ?Sized, S> CoerciblePtr<U> for Arc<T, S>
impl<T, U: ?Sized, S> CoerciblePtr<U> for Arc<T, S>
Source§fn as_sized_ptr(&mut self) -> *mut T
fn as_sized_ptr(&mut self) -> *mut T
Source§impl<'de, T: Deserialize<'de>, S> Deserialize<'de> for Arc<T, S>
impl<'de, T: Deserialize<'de>, S> Deserialize<'de> for Arc<T, S>
Source§fn deserialize<D>(deserializer: D) -> Result<Arc<T, S>, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Arc<T, S>, D::Error>where
D: Deserializer<'de>,
Source§impl<T: Copy, S> From<&[T]> for Arc<[T], S>where
S: BackdropStrategy<Box<ArcInner<[T]>>> + BackdropStrategy<Box<ArcInner<HeaderSlice<(), [T]>>>>,
impl<T: Copy, S> From<&[T]> for Arc<[T], S>where
S: BackdropStrategy<Box<ArcInner<[T]>>> + BackdropStrategy<Box<ArcInner<HeaderSlice<(), [T]>>>>,
Source§impl<S> From<&str> for Arc<str, S>where
S: BackdropStrategy<Box<ArcInner<str>>> + BackdropStrategy<Box<ArcInner<HeaderSlice<(), str>>>>,
impl<S> From<&str> for Arc<str, S>where
S: BackdropStrategy<Box<ArcInner<str>>> + BackdropStrategy<Box<ArcInner<HeaderSlice<(), str>>>>,
Source§impl<T, S: BackdropStrategy<Box<ArcInner<T>>>> From<Arc<T>> for Arc<T, S>
Converting to- and from a triomphe::Arc<T>
is a zero-cost operation
impl<T, S: BackdropStrategy<Box<ArcInner<T>>>> From<Arc<T>> for Arc<T, S>
Converting to- and from a triomphe::Arc<T>
is a zero-cost operation
Source§impl<T, S: BackdropStrategy<Box<ArcInner<T>>>> From<Arc<T, S>> for Arc<T>
Converting to- and from a triomphe::Arc<T>
is a zero-cost operation
impl<T, S: BackdropStrategy<Box<ArcInner<T>>>> From<Arc<T, S>> for Arc<T>
Converting to- and from a triomphe::Arc<T>
is a zero-cost operation
Source§impl<S> From<String> for Arc<str, S>where
S: BackdropStrategy<Box<ArcInner<str>>> + BackdropStrategy<Box<ArcInner<HeaderSlice<(), str>>>>,
impl<S> From<String> for Arc<str, S>where
S: BackdropStrategy<Box<ArcInner<str>>> + BackdropStrategy<Box<ArcInner<HeaderSlice<(), str>>>>,
Source§impl<T, S> From<Vec<T>> for Arc<[T], S>where
S: BackdropStrategy<Box<ArcInner<[T]>>> + BackdropStrategy<Box<ArcInner<HeaderSlice<(), [T]>>>>,
impl<T, S> From<Vec<T>> for Arc<[T], S>where
S: BackdropStrategy<Box<ArcInner<[T]>>> + BackdropStrategy<Box<ArcInner<HeaderSlice<(), [T]>>>>,
Source§impl<A, S> FromIterator<A> for Arc<[A], S>where
S: BackdropStrategy<Box<ArcInner<[A]>>> + BackdropStrategy<Box<ArcInner<HeaderSlice<(), [A]>>>> + BackdropStrategy<Box<[A]>>,
impl<A, S> FromIterator<A> for Arc<[A], S>where
S: BackdropStrategy<Box<ArcInner<[A]>>> + BackdropStrategy<Box<ArcInner<HeaderSlice<(), [A]>>>> + BackdropStrategy<Box<[A]>>,
Source§fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self
Source§impl<T: ?Sized + Ord, S> Ord for Arc<T, S>
impl<T: ?Sized + Ord, S> Ord for Arc<T, S>
Source§impl<T: ?Sized + PartialOrd, S> PartialOrd for Arc<T, S>
impl<T: ?Sized + PartialOrd, S> PartialOrd for Arc<T, S>
Source§impl<T, S> RefCnt for Arc<T, S>
impl<T, S> RefCnt for Arc<T, S>
Source§fn into_ptr(me: Self) -> *mut Self::Base
fn into_ptr(me: Self) -> *mut Self::Base
Source§fn as_ptr(me: &Self) -> *mut Self::Base
fn as_ptr(me: &Self) -> *mut Self::Base
Source§impl<T: ?Sized, S: BackdropStrategy<Box<ArcInner<T>>>> TryFrom<Arc<T, S>> for UniqueArc<T, S>
impl<T: ?Sized, S: BackdropStrategy<Box<ArcInner<T>>>> TryFrom<Arc<T, S>> for UniqueArc<T, S>
impl<T: ?Sized, S> CloneStableDeref for Arc<T, S>
impl<T, S> CloneableCart for Arc<T, S>
impl<T: ?Sized + Eq, S> Eq for Arc<T, S>
impl<T: ?Sized + Sync + Send, S> Send for Arc<T, S>
impl<T: ?Sized, S> StableDeref for Arc<T, S>
impl<T: ?Sized + Sync + Send, S> Sync for Arc<T, S>
Auto Trait Implementations§
impl<T, S> Freeze for Arc<T, S>where
T: ?Sized,
impl<T, S> RefUnwindSafe for Arc<T, S>
impl<T, S> Unpin for Arc<T, S>
impl<T, S> UnwindSafe for Arc<T, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, U> CoerceUnsize<U> for Twhere
T: CoerciblePtr<U>,
U: ?Sized,
impl<T, U> CoerceUnsize<U> for Twhere
T: CoerciblePtr<U>,
U: ?Sized,
Source§impl<T, A> DynAccess<T> for A
impl<T, A> DynAccess<T> for A
Source§fn load(&self) -> DynGuard<T>
fn load(&self) -> DynGuard<T>
Access::load
.