pub struct UniqueArc<T, A = Global>{ /* private fields */ }unique_rc_arc)Expand description
A uniquely owned Arc.
This represents an Arc that is known to be uniquely owned – that is, have exactly one strong
reference. Multiple weak pointers can be created, but attempts to upgrade those to strong
references will fail unless the UniqueArc they point to has been converted into a regular Arc.
Because it is uniquely owned, the contents of a UniqueArc can be freely mutated. A common
use case is to have an object be mutable during its initialization phase but then have it become
immutable and converted to a normal Arc.
This can be used as a flexible way to create cyclic data structures, as in the example below.
#![feature(unique_rc_arc)]
use std::sync::{Arc, Weak, UniqueArc};
struct Gadget {
me: Weak<Gadget>,
}
fn create_gadget() -> Option<Arc<Gadget>> {
let mut rc = UniqueArc::new(Gadget {
me: Weak::new(),
});
rc.me = UniqueArc::downgrade(&rc);
Some(UniqueArc::into_arc(rc))
}
create_gadget().unwrap();An advantage of using UniqueArc over Arc::new_cyclic to build cyclic data structures is that
Arc::new_cyclic’s data_fn parameter cannot be async or return a Result. As shown in the
previous example, UniqueArc allows for more flexibility in the construction of cyclic data,
including fallible or async constructors.
Implementations§
Source§impl<T> UniqueArc<T>
impl<T> UniqueArc<T>
Sourcepub fn new(value: T) -> UniqueArc<T>
🔬This is a nightly-only experimental API. (unique_rc_arc)
pub fn new(value: T) -> UniqueArc<T>
unique_rc_arc)Creates a new UniqueArc.
Weak references to this UniqueArc can be created with UniqueArc::downgrade. Upgrading
these weak references will fail before the UniqueArc has been converted into an Arc.
After converting the UniqueArc into an Arc, any weak references created beforehand will
point to the new Arc.
Sourcepub fn map<U>(this: UniqueArc<T>, f: impl FnOnce(T) -> U) -> UniqueArc<U>
🔬This is a nightly-only experimental API. (smart_pointer_try_map)
pub fn map<U>(this: UniqueArc<T>, f: impl FnOnce(T) -> U) -> UniqueArc<U>
smart_pointer_try_map)Maps the value in a UniqueArc, reusing the allocation if possible.
f is called on a reference to the value in the UniqueArc, and the result is returned,
also in a UniqueArc.
Note: this is an associated function, which means that you have
to call it as UniqueArc::map(u, f) instead of u.map(f). This
is so that there is no conflict with a method on the inner type.
§Examples
#![feature(smart_pointer_try_map)]
#![feature(unique_rc_arc)]
use std::sync::UniqueArc;
let r = UniqueArc::new(7);
let new = UniqueArc::map(r, |i| i + 7);
assert_eq!(*new, 14);Sourcepub fn try_map<R>(
this: UniqueArc<T>,
f: impl FnOnce(T) -> R,
) -> <<R as Try>::Residual as Residual<UniqueArc<<R as Try>::Output>>>::TryType
🔬This is a nightly-only experimental API. (smart_pointer_try_map)
pub fn try_map<R>( this: UniqueArc<T>, f: impl FnOnce(T) -> R, ) -> <<R as Try>::Residual as Residual<UniqueArc<<R as Try>::Output>>>::TryType
smart_pointer_try_map)Attempts to map the value in a UniqueArc, reusing the allocation if possible.
f is called on a reference to the value in the UniqueArc, and if the operation succeeds,
the result is returned, also in a UniqueArc.
Note: this is an associated function, which means that you have
to call it as UniqueArc::try_map(u, f) instead of u.try_map(f). This
is so that there is no conflict with a method on the inner type.
§Examples
#![feature(smart_pointer_try_map)]
#![feature(unique_rc_arc)]
use std::sync::UniqueArc;
let b = UniqueArc::new(7);
let new = UniqueArc::try_map(b, u32::try_from).unwrap();
assert_eq!(*new, 7);Source§impl<T, A> UniqueArc<T, A>where
A: Allocator,
impl<T, A> UniqueArc<T, A>where
A: Allocator,
Sourcepub fn new_in(data: T, alloc: A) -> UniqueArc<T, A>
🔬This is a nightly-only experimental API. (unique_rc_arc)
pub fn new_in(data: T, alloc: A) -> UniqueArc<T, A>
unique_rc_arc)Creates a new UniqueArc in the provided allocator.
Weak references to this UniqueArc can be created with UniqueArc::downgrade. Upgrading
these weak references will fail before the UniqueArc has been converted into an Arc.
After converting the UniqueArc into an Arc, any weak references created beforehand will
point to the new Arc.
Source§impl<T, A> UniqueArc<T, A>
impl<T, A> UniqueArc<T, A>
Trait Implementations§
Source§impl<T, A> BorrowMut<T> for UniqueArc<T, A>
impl<T, A> BorrowMut<T> for UniqueArc<T, A>
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T, A> Ord for UniqueArc<T, A>
impl<T, A> Ord for UniqueArc<T, A>
Source§fn cmp(&self, other: &UniqueArc<T, A>) -> Ordering
fn cmp(&self, other: &UniqueArc<T, A>) -> Ordering
Comparison for two UniqueArcs.
The two are compared by calling cmp() on their inner values.
§Examples
#![feature(unique_rc_arc)]
use std::sync::UniqueArc;
use std::cmp::Ordering;
let five = UniqueArc::new(5);
assert_eq!(Ordering::Less, five.cmp(&UniqueArc::new(6)));1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T, A> PartialEq for UniqueArc<T, A>
impl<T, A> PartialEq for UniqueArc<T, A>
Source§impl<T, A> PartialOrd for UniqueArc<T, A>
impl<T, A> PartialOrd for UniqueArc<T, A>
Source§fn partial_cmp(&self, other: &UniqueArc<T, A>) -> Option<Ordering>
fn partial_cmp(&self, other: &UniqueArc<T, A>) -> Option<Ordering>
Partial comparison for two UniqueArcs.
The two are compared by calling partial_cmp() on their inner values.
§Examples
#![feature(unique_rc_arc)]
use std::sync::UniqueArc;
use std::cmp::Ordering;
let five = UniqueArc::new(5);
assert_eq!(Some(Ordering::Less), five.partial_cmp(&UniqueArc::new(6)));Source§fn lt(&self, other: &UniqueArc<T, A>) -> bool
fn lt(&self, other: &UniqueArc<T, A>) -> bool
Less-than comparison for two UniqueArcs.
The two are compared by calling < on their inner values.
§Examples
#![feature(unique_rc_arc)]
use std::sync::UniqueArc;
let five = UniqueArc::new(5);
assert!(five < UniqueArc::new(6));Source§fn le(&self, other: &UniqueArc<T, A>) -> bool
fn le(&self, other: &UniqueArc<T, A>) -> bool
‘Less than or equal to’ comparison for two UniqueArcs.
The two are compared by calling <= on their inner values.
§Examples
#![feature(unique_rc_arc)]
use std::sync::UniqueArc;
let five = UniqueArc::new(5);
assert!(five <= UniqueArc::new(5));Source§fn gt(&self, other: &UniqueArc<T, A>) -> bool
fn gt(&self, other: &UniqueArc<T, A>) -> bool
Greater-than comparison for two UniqueArcs.
The two are compared by calling > on their inner values.
§Examples
#![feature(unique_rc_arc)]
use std::sync::UniqueArc;
let five = UniqueArc::new(5);
assert!(five > UniqueArc::new(4));Source§fn ge(&self, other: &UniqueArc<T, A>) -> bool
fn ge(&self, other: &UniqueArc<T, A>) -> bool
‘Greater than or equal to’ comparison for two UniqueArcs.
The two are compared by calling >= on their inner values.
§Examples
#![feature(unique_rc_arc)]
use std::sync::UniqueArc;
let five = UniqueArc::new(5);
assert!(five >= UniqueArc::new(5));impl<T, U, A> CoerceUnsized<UniqueArc<U, A>> for UniqueArc<T, A>
impl<T, A> DerefPure for UniqueArc<T, A>
impl<T, U> DispatchFromDyn<UniqueArc<U>> for UniqueArc<T>
impl<T, A> Eq for UniqueArc<T, A>
impl<T> PinCoerceUnsized for UniqueArc<T>where
T: ?Sized,
impl<T, A> Send for UniqueArc<T, A>
impl<T, A> Sync for UniqueArc<T, A>
impl<T, A> Unpin for UniqueArc<T, A>
Auto Trait Implementations§
impl<T, A> Freeze for UniqueArc<T, A>
impl<T, A> RefUnwindSafe for UniqueArc<T, A>
impl<T, A> UnwindSafe for UniqueArc<T, A>
Blanket Implementations§
§impl<T, A, P> Access<T> for P
impl<T, A, P> Access<T> for P
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<T, A> DynAccess<T> for Awhere
A: Access<T>,
<A as Access<T>>::Guard: 'static,
impl<T, A> DynAccess<T> for Awhere
A: Access<T>,
<A as Access<T>>::Guard: 'static,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<K, V, T> Expiry<K, V> for T
impl<K, V, T> Expiry<K, V> for T
§fn expire_after_create(
&self,
key: &K,
value: &V,
created_at: Instant,
) -> Option<Duration>
fn expire_after_create( &self, key: &K, value: &V, created_at: Instant, ) -> Option<Duration>
insert and get_with but only when the key
was not present in the cache. Read more§fn expire_after_read(
&self,
key: &K,
value: &V,
read_at: Instant,
duration_until_expiry: Option<Duration>,
last_modified_at: Instant,
) -> Option<Duration>
fn expire_after_read( &self, key: &K, value: &V, read_at: Instant, duration_until_expiry: Option<Duration>, last_modified_at: Instant, ) -> Option<Duration>
get and get_with but only when the key is present in
the cache. Read more§fn expire_after_update(
&self,
key: &K,
value: &V,
updated_at: Instant,
duration_until_expiry: Option<Duration>,
) -> Option<Duration>
fn expire_after_update( &self, key: &K, value: &V, updated_at: Instant, duration_until_expiry: Option<Duration>, ) -> Option<Duration>
insert but only when the key is
already present in the cache. Read more§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more§impl<T, U> RamaTryFrom<T> for Uwhere
U: TryFrom<T>,
impl<T, U> RamaTryFrom<T> for Uwhere
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
fn rama_try_from(value: T) -> Result<U, <U as RamaTryFrom<T>>::Error>
§impl<T, U, CrateMarker> RamaTryInto<U, CrateMarker> for Twhere
U: RamaTryFrom<T, CrateMarker>,
impl<T, U, CrateMarker> RamaTryInto<U, CrateMarker> for Twhere
U: RamaTryFrom<T, CrateMarker>,
type Error = <U as RamaTryFrom<T, CrateMarker>>::Error
fn rama_try_into(self) -> Result<U, <U as RamaTryFrom<T, CrateMarker>>::Error>
Source§impl<R> Rng for R
impl<R> Rng for R
Source§fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn random<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
StandardUniform distribution. Read moreSource§fn random_iter<T>(self) -> Iter<StandardUniform, Self, T> ⓘ
fn random_iter<T>(self) -> Iter<StandardUniform, Self, T> ⓘ
Source§fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn random_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
Source§fn random_bool(&mut self, p: f64) -> bool
fn random_bool(&mut self, p: f64) -> bool
p of being true. Read moreSource§fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool
numerator/denominator of being
true. Read moreSource§fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
fn sample<T, D>(&mut self, distr: D) -> Twhere
D: Distribution<T>,
Source§fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T> ⓘwhere
D: Distribution<T>,
Self: Sized,
fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T> ⓘwhere
D: Distribution<T>,
Self: Sized,
Source§fn gen<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
fn gen<T>(&mut self) -> Twhere
StandardUniform: Distribution<T>,
random to avoid conflict with the new gen keyword in Rust 2024.Rng::random.Source§fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
fn gen_range<T, R>(&mut self, range: R) -> Twhere
T: SampleUniform,
R: SampleRange<T>,
random_rangeRng::random_range.Source§impl<T> ToHex for T
impl<T> ToHex for T
Source§fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Lower case
letters are used (e.g. f9b4ca)Source§fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
fn encode_hex_upper<U>(&self) -> Uwhere
U: FromIterator<char>,
self into the result. Upper case
letters are used (e.g. F9B4CA)§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.
Source§impl<R> TryRngCore for R
impl<R> TryRngCore for R
Source§type Error = Infallible
type Error = Infallible
Source§fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>
u32.Source§fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>
u64.Source§fn try_fill_bytes(
&mut self,
dst: &mut [u8],
) -> Result<(), <R as TryRngCore>::Error>
fn try_fill_bytes( &mut self, dst: &mut [u8], ) -> Result<(), <R as TryRngCore>::Error>
dest entirely with random data.Source§fn unwrap_err(self) -> UnwrapErr<Self>where
Self: Sized,
fn unwrap_err(self) -> UnwrapErr<Self>where
Self: Sized,
UnwrapErr wrapper.Source§fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>
UnwrapMut wrapper.Source§fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>where
Self: Sized,
std only.RngCore to a RngReadAdapter.