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<A, T> AsBits<T> for A
impl<A, T> AsBits<T> for A
Source§impl<A, T> AsMutBits<T> for A
impl<A, T> AsMutBits<T> for A
Source§fn as_mut_bits<O>(&mut self) -> &mut BitSlice<T, O> ⓘwhere
O: BitOrder,
fn as_mut_bits<O>(&mut self) -> &mut BitSlice<T, O> ⓘwhere
O: BitOrder,
self
as a mutable bit-slice region with the O
ordering.Source§fn try_as_mut_bits<O>(&mut self) -> Result<&mut BitSlice<T, O>, BitSpanError<T>>where
O: BitOrder,
fn try_as_mut_bits<O>(&mut self) -> Result<&mut BitSlice<T, O>, BitSpanError<T>>where
O: BitOrder,
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§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.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 moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.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
)