pub struct UniqueArc<T, A = Global>{ /* private fields */ }
unique_rc_arc
#112566)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
#112566)
pub fn new(value: T) -> UniqueArc<T>
unique_rc_arc
#112566)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
#112566)
pub fn new_in(data: T, alloc: A) -> UniqueArc<T, A>
unique_rc_arc
#112566)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
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
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
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.