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
.
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 UniqueArc
s.
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 UniqueArc
s.
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 UniqueArc
s.
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 UniqueArc
s.
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 UniqueArc
s.
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 UniqueArc
s.
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§
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<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
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_range
Rng::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
)Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§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 Rwhere
R: RngCore,
impl<R> TryRngCore for Rwhere
R: RngCore,
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 read_adapter(&mut self) -> RngReadAdapter<'_, Self> ⓘwhere
Self: Sized,
fn read_adapter(&mut self) -> RngReadAdapter<'_, Self> ⓘwhere
Self: Sized,
RngCore
to a RngReadAdapter
.