pub struct UniqueRc<T, A = Global>where
A: Allocator,
T: ?Sized,{ /* private fields */ }
unique_rc_arc #112566)
A uniquely owned Rc.
This represents an Rc 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 UniqueRc they point to has been converted into a regular Rc.
Because they are uniquely owned, the contents of a UniqueRc 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 Rc.
This can be used as a flexible way to create cyclic data structures, as in the example below.
#![feature(unique_rc_arc)]
use std::rc::{Rc, Weak, UniqueRc};
struct Gadget {
#[allow(dead_code)]
me: Weak<Gadget>,
}
fn create_gadget() -> Option<Rc<Gadget>> {
let mut rc = UniqueRc::new(Gadget {
me: Weak::new(),
});
rc.me = UniqueRc::downgrade(&rc);
Some(UniqueRc::into_rc(rc))
}
create_gadget().unwrap();An advantage of using UniqueRc over Rc::new_cyclic to build cyclic data structures is that Rc::new_cyclic’s data_fn parameter cannot be async or return a Result. As shown in the previous example, UniqueRc allows for more flexibility in the construction of cyclic data, including fallible or async constructors.
impl<T> UniqueRc<T>
pub fn new(value: T) -> UniqueRc<T>
unique_rc_arc #112566)
Creates a new UniqueRc.
Weak references to this UniqueRc can be created with UniqueRc::downgrade. Upgrading these weak references will fail before the UniqueRc has been converted into an Rc. After converting the UniqueRc into an Rc, any weak references created beforehand will point to the new Rc.
pub fn map<U>(this: UniqueRc<T>, f: impl FnOnce(T) -> U) -> UniqueRc<U>
smart_pointer_try_map #144419)
Maps the value in a UniqueRc, reusing the allocation if possible.
f is called on a reference to the value in the UniqueRc, and the result is returned, also in a UniqueRc.
Note: this is an associated function, which means that you have to call it as UniqueRc::map(u, f) instead of u.map(f). This is so that there is no conflict with a method on the inner type.
#![feature(smart_pointer_try_map)] #![feature(unique_rc_arc)] use std::rc::UniqueRc; let r = UniqueRc::new(7); let new = UniqueRc::map(r, |i| i + 7); assert_eq!(*new, 14);
pub fn try_map<R>(
this: UniqueRc<T>,
f: impl FnOnce(T) -> R,
) -> <<R as Try>::Residual as Residual<UniqueRc<<R as Try>::Output>>>::TryTypewhere
R: Try,
<R as Try>::Residual: Residual<UniqueRc<<R as Try>::Output>>,smart_pointer_try_map #144419)
Attempts to map the value in a UniqueRc, reusing the allocation if possible.
f is called on a reference to the value in the UniqueRc, and if the operation succeeds, the result is returned, also in a UniqueRc.
Note: this is an associated function, which means that you have to call it as UniqueRc::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.
#![feature(smart_pointer_try_map)] #![feature(unique_rc_arc)] use std::rc::UniqueRc; let b = UniqueRc::new(7); let new = UniqueRc::try_map(b, u32::try_from).unwrap(); assert_eq!(*new, 7);
impl<T, A> UniqueRc<T, A>where
A: Allocator,pub fn new_in(value: T, alloc: A) -> UniqueRc<T, A>
unique_rc_arc #112566)
Creates a new UniqueRc in the provided allocator.
Weak references to this UniqueRc can be created with UniqueRc::downgrade. Upgrading these weak references will fail before the UniqueRc has been converted into an Rc. After converting the UniqueRc into an Rc, any weak references created beforehand will point to the new Rc.
impl<T, A> UniqueRc<T, A>where
A: Allocator,
T: ?Sized,pub fn into_rc(this: UniqueRc<T, A>) -> Rc<T, A>
unique_rc_arc #112566)
impl<T, A> UniqueRc<T, A>where
A: Allocator + Clone,
T: ?Sized,pub fn downgrade(this: &UniqueRc<T, A>) -> Weak<T, A>
unique_rc_arc #112566)
Creates a new weak reference to the UniqueRc.
Attempting to upgrade this weak reference will fail before the UniqueRc has been converted to a Rc using UniqueRc::into_rc.
impl<T: AsFd + ?Sized> AsFd for UniqueRc<T>Available on Unix or HermitCore or target_os=trusty or WASI or target_os=motor only.
impl<T: AsHandle + ?Sized> AsHandle for UniqueRc<T>Available on Windows only.
impl<T, A> AsMut<T> for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,fn as_mut(&mut self) -> &mut T
impl<T: AsRawFd + ?Sized> AsRawFd for UniqueRc<T>Available on Unix or HermitCore or target_os=trusty or WASI or target_os=motor only.
impl<T, A> AsRef<T> for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,fn as_ref(&self) -> &T
impl<T: AsSocket + ?Sized> AsSocket for UniqueRc<T>Available on Windows only.
fn as_socket(&self) -> BorrowedSocket<'_>
impl<T, A> Borrow<T> for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,impl<T, A> BorrowMut<T> for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,impl<T, A> Debug for UniqueRc<T, A>where
T: Debug + ?Sized,
A: Allocator,fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
impl<T, A> Deref for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,type Target = T
fn deref(&self) -> &T
impl<T, A> DerefMut for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,fn deref_mut(&mut self) -> &mut T
impl<T, A> Display for UniqueRc<T, A>where
T: Display + ?Sized,
A: Allocator,fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
impl<T, A> Drop for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,impl<T, A> Hash for UniqueRc<T, A>where
T: Hash + ?Sized,
A: Allocator,fn hash<H>(&self, state: &mut H)where
H: Hasher,fn hash_slice<H>(data: &[Self], state: &mut H)where
H: Hasher,
Self: Sized,impl<T, A> Ord for UniqueRc<T, A>where
T: Ord + ?Sized,
A: Allocator,fn cmp(&self, other: &UniqueRc<T, A>) -> Ordering
Comparison for two UniqueRcs.
The two are compared by calling cmp() on their inner values.
#![feature(unique_rc_arc)] use std::rc::UniqueRc; use std::cmp::Ordering; let five = UniqueRc::new(5); assert_eq!(Ordering::Less, five.cmp(&UniqueRc::new(6)));
fn max(self, other: Self) -> Selfwhere
Self: Sized,fn min(self, other: Self) -> Selfwhere
Self: Sized,fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized,impl<T, A> PartialEq for UniqueRc<T, A>where
T: PartialEq + ?Sized,
A: Allocator,fn eq(&self, other: &UniqueRc<T, A>) -> bool
Equality for two UniqueRcs.
Two UniqueRcs are equal if their inner values are equal.
#![feature(unique_rc_arc)] use std::rc::UniqueRc; let five = UniqueRc::new(5); assert!(five == UniqueRc::new(5));
fn ne(&self, other: &UniqueRc<T, A>) -> bool
Inequality for two UniqueRcs.
Two UniqueRcs are not equal if their inner values are not equal.
#![feature(unique_rc_arc)] use std::rc::UniqueRc; let five = UniqueRc::new(5); assert!(five != UniqueRc::new(6));
impl<T, A> PartialOrd for UniqueRc<T, A>where
T: PartialOrd + ?Sized,
A: Allocator,fn partial_cmp(&self, other: &UniqueRc<T, A>) -> Option<Ordering>
Partial comparison for two UniqueRcs.
The two are compared by calling partial_cmp() on their inner values.
#![feature(unique_rc_arc)] use std::rc::UniqueRc; use std::cmp::Ordering; let five = UniqueRc::new(5); assert_eq!(Some(Ordering::Less), five.partial_cmp(&UniqueRc::new(6)));
fn lt(&self, other: &UniqueRc<T, A>) -> bool
Less-than comparison for two UniqueRcs.
The two are compared by calling < on their inner values.
#![feature(unique_rc_arc)] use std::rc::UniqueRc; let five = UniqueRc::new(5); assert!(five < UniqueRc::new(6));
fn le(&self, other: &UniqueRc<T, A>) -> bool
‘Less than or equal to’ comparison for two UniqueRcs.
The two are compared by calling <= on their inner values.
#![feature(unique_rc_arc)] use std::rc::UniqueRc; let five = UniqueRc::new(5); assert!(five <= UniqueRc::new(5));
fn gt(&self, other: &UniqueRc<T, A>) -> bool
Greater-than comparison for two UniqueRcs.
The two are compared by calling > on their inner values.
#![feature(unique_rc_arc)] use std::rc::UniqueRc; let five = UniqueRc::new(5); assert!(five > UniqueRc::new(4));
fn ge(&self, other: &UniqueRc<T, A>) -> bool
‘Greater than or equal to’ comparison for two UniqueRcs.
The two are compared by calling >= on their inner values.
#![feature(unique_rc_arc)] use std::rc::UniqueRc; let five = UniqueRc::new(5); assert!(five >= UniqueRc::new(5));
impl<T, A> Pointer for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
impl<T, U, A> CoerceUnsized<UniqueRc<U, A>> for UniqueRc<T, A>where
T: Unsize<U> + ?Sized,
A: Allocator,
U: ?Sized,impl<T, A> DerefPure for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,impl<T, U> DispatchFromDyn<UniqueRc<U>> for UniqueRc<T>where
T: Unsize<U> + ?Sized,
U: ?Sized,impl<T, A> Eq for UniqueRc<T, A>where
T: Eq + ?Sized,
A: Allocator,impl<T, A> PinCoerceUnsized for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,impl<T, A> !Send for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,impl<T, A> !Sync for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,impl<T, A> Unpin for UniqueRc<T, A>where
A: Allocator,
T: ?Sized,impl<T, A> Freeze for UniqueRc<T, A>where
A: Freeze,
T: ?Sized,impl<T, A = Global> !RefUnwindSafe for UniqueRc<T, A>
impl<T, A = Global> !UnwindSafe for UniqueRc<T, A>
impl<T> Any for Twhere
T: 'static + ?Sized,impl<T> Borrow<T> for Twhere
T: ?Sized,impl<T> BorrowMut<T> for Twhere
T: ?Sized,impl<T> From<T> for T
fn from(t: T) -> T
Returns the argument unchanged.
impl<T, U> Into<U> for Twhere
U: From<T>,fn into(self) -> U
Calls U::from(self).
That is, this conversion is whatever the implementation of From<T> for U chooses to do.
impl<P, T> Receiver for Pwhere
P: Deref<Target = T> + ?Sized,
T: ?Sized,type Target = T
arbitrary_self_types #44874)
impl<T> ToString for Twhere
T: Display + ?Sized,impl<T, U> TryFrom<U> for Twhere
U: Into<T>,type Error = Infallible
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/rc/struct.UniqueRc.html