pub struct UniqueArc<T, A =Global>{/* private fields */ }unique_rc_arc #112566)Expand description
A uniquely ownedArc.
This represents anArc that is known to be uniquely owned – that is, have exactly one strongreference. Multiple weak pointers can be created, but attempts to upgrade those to strongreferences will fail unless theUniqueArc they point to has been converted into a regularArc.
Because it is uniquely owned, the contents of aUniqueArc can be freely mutated. A commonuse case is to have an object be mutable during its initialization phase but then have it becomeimmutable and converted to a normalArc.
This can be used as a flexible way to create cyclic data structures, as in the example below.
#![feature(unique_rc_arc)]usestd::sync::{Arc, Weak, UniqueArc};structGadget { me: Weak<Gadget>,}fncreate_gadget() ->Option<Arc<Gadget>> {letmutrc = UniqueArc::new(Gadget { me: Weak::new(), }); rc.me = UniqueArc::downgrade(&rc);Some(UniqueArc::into_arc(rc))}create_gadget().unwrap();An advantage of usingUniqueArc overArc::new_cyclic to build cyclic data structures is thatArc::new_cyclic’sdata_fn parameter cannot be async or return aResult. As shown in theprevious 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 fnnew(value: T) ->UniqueArc<T>
🔬This is a nightly-only experimental API. (unique_rc_arc #112566)
pub fnnew(value: T) ->UniqueArc<T>
unique_rc_arc #112566)Creates a newUniqueArc.
Weak references to thisUniqueArc can be created withUniqueArc::downgrade. Upgradingthese weak references will fail before theUniqueArc has been converted into anArc.After converting theUniqueArc into anArc, any weak references created beforehand willpoint to the newArc.
Sourcepub fnmap<U>(this:UniqueArc<T>, f: implFnOnce(T) -> U) ->UniqueArc<U>
🔬This is a nightly-only experimental API. (smart_pointer_try_map #144419)
pub fnmap<U>(this:UniqueArc<T>, f: implFnOnce(T) -> U) ->UniqueArc<U>
smart_pointer_try_map #144419)Maps the value in aUniqueArc, reusing the allocation if possible.
f is called on a reference to the value in theUniqueArc, and the result is returned,also in aUniqueArc.
Note: this is an associated function, which means that you haveto call it asUniqueArc::map(u, f) instead ofu.map(f). Thisis so that there is no conflict with a method on the inner type.
§Examples
Sourcepub fntry_map<R>( this:UniqueArc<T>, f: implFnOnce(T) -> R,) -> <<R asTry>::Residual asResidual<UniqueArc<<R asTry>::Output>>>::TryType
🔬This is a nightly-only experimental API. (smart_pointer_try_map #144419)
pub fntry_map<R>( this:UniqueArc<T>, f: implFnOnce(T) -> R,) -> <<R asTry>::Residual asResidual<UniqueArc<<R asTry>::Output>>>::TryType
smart_pointer_try_map #144419)Attempts to map the value in aUniqueArc, reusing the allocation if possible.
f is called on a reference to the value in theUniqueArc, and if the operation succeeds,the result is returned, also in aUniqueArc.
Note: this is an associated function, which means that you haveto call it asUniqueArc::try_map(u, f) instead ofu.try_map(f). Thisis so that there is no conflict with a method on the inner type.
§Examples
Source§impl<T, A>UniqueArc<T, A>where A:Allocator,
impl<T, A>UniqueArc<T, A>where A:Allocator,
Sourcepub fnnew_in(data: T, alloc: A) ->UniqueArc<T, A>
🔬This is a nightly-only experimental API. (unique_rc_arc #112566)
pub fnnew_in(data: T, alloc: A) ->UniqueArc<T, A>
unique_rc_arc #112566)Creates a newUniqueArc in the provided allocator.
Weak references to thisUniqueArc can be created withUniqueArc::downgrade. Upgradingthese weak references will fail before theUniqueArc has been converted into anArc.After converting theUniqueArc into anArc, any weak references created beforehand willpoint to the newArc.
Source§impl<T, A>UniqueArc<T, A>
impl<T, A>UniqueArc<T, A>
Trait Implementations§
Source§impl<T, A>BorrowMut<T> forUniqueArc<T, A>
impl<T, A>BorrowMut<T> forUniqueArc<T, A>
Source§fnborrow_mut(&mut self) ->&mut T
fnborrow_mut(&mut self) ->&mut T
Source§impl<T, A>Ord forUniqueArc<T, A>
impl<T, A>Ord forUniqueArc<T, A>
Source§fncmp(&self, other: &UniqueArc<T, A>) ->Ordering
fncmp(&self, other: &UniqueArc<T, A>) ->Ordering
1.21.0 ·Source§fnmax(self, other: Self) -> Selfwhere Self:Sized,
fnmax(self, other: Self) -> Selfwhere Self:Sized,
Source§impl<T, A>PartialEq forUniqueArc<T, A>
impl<T, A>PartialEq forUniqueArc<T, A>
Source§impl<T, A>PartialOrd forUniqueArc<T, A>
impl<T, A>PartialOrd forUniqueArc<T, A>
Source§fnpartial_cmp(&self, other: &UniqueArc<T, A>) ->Option<Ordering>
fnpartial_cmp(&self, other: &UniqueArc<T, A>) ->Option<Ordering>
Partial comparison for twoUniqueArcs.
The two are compared by callingpartial_cmp() on their inner values.
§Examples
Source§fnlt(&self, other: &UniqueArc<T, A>) ->bool
fnlt(&self, other: &UniqueArc<T, A>) ->bool
Less-than comparison for twoUniqueArcs.
The two are compared by calling< on their inner values.
§Examples
Source§fnle(&self, other: &UniqueArc<T, A>) ->bool
fnle(&self, other: &UniqueArc<T, A>) ->bool
‘Less than or equal to’ comparison for twoUniqueArcs.
The two are compared by calling<= on their inner values.