UniqueRc

Struct UniqueRc 

pub struct UniqueRc<T, A = Global>
where A: Allocator, T: ?Sized,
{ /* private fields */ }
🔬This is a nightly-only experimental API. (unique_rc_arc)
Expand description

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.

Implementations§

§

impl<T> UniqueRc<T>

pub fn new(value: T) -> UniqueRc<T>

🔬This is a nightly-only experimental API. (unique_rc_arc)

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>

🔬This is a nightly-only experimental API. (smart_pointer_try_map)

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.

§Examples
#![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>>>::TryType
where R: Try, <R as Try>::Residual: Residual<UniqueRc<<R as Try>::Output>>,

🔬This is a nightly-only experimental API. (smart_pointer_try_map)

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.

§Examples
#![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>

🔬This is a nightly-only experimental API. (unique_rc_arc)

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>

🔬This is a nightly-only experimental API. (unique_rc_arc)

Converts the UniqueRc into a regular Rc.

This consumes the UniqueRc and returns a regular Rc that contains the value that is passed to into_rc.

Any weak references created before this method is called can now be upgraded to strong references.

§

impl<T, A> UniqueRc<T, A>
where A: Allocator + Clone, T: ?Sized,

pub fn downgrade(this: &UniqueRc<T, A>) -> Weak<T, A>

🔬This is a nightly-only experimental API. (unique_rc_arc)

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.

Trait Implementations§

§

impl<T> AsFd for UniqueRc<T>
where T: AsFd + ?Sized,

§

fn as_fd(&self) -> BorrowedFd<'_>

Borrows the file descriptor. Read more
§

impl<T, A> AsMut<T> for UniqueRc<T, A>
where A: Allocator, T: ?Sized,

§

fn as_mut(&mut self) -> &mut T

Converts this type into a mutable reference of the (usually inferred) input type.
§

impl<T> AsRawFd for UniqueRc<T>
where T: AsRawFd + ?Sized,

§

fn as_raw_fd(&self) -> i32

Extracts the raw file descriptor. Read more
§

impl<T, A> AsRef<T> for UniqueRc<T, A>
where A: Allocator, T: ?Sized,

§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
§

impl<T, A> Borrow<T> for UniqueRc<T, A>
where A: Allocator, T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T, A> BorrowMut<T> for UniqueRc<T, A>
where A: Allocator, T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T, A> Debug for UniqueRc<T, A>
where T: Debug + ?Sized, A: Allocator,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<T, A> Deref for UniqueRc<T, A>
where A: Allocator, T: ?Sized,

§

type Target = T

The resulting type after dereferencing.
§

fn deref(&self) -> &T

Dereferences the value.
§

impl<T, A> DerefMut for UniqueRc<T, A>
where A: Allocator, T: ?Sized,

§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
§

impl<T, A> Display for UniqueRc<T, A>
where T: Display + ?Sized, A: Allocator,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<T, A> Drop for UniqueRc<T, A>
where A: Allocator, T: ?Sized,

§

fn drop(&mut self)

Executes the destructor for this type. Read more
§

impl<T, A> Hash for UniqueRc<T, A>
where T: Hash + ?Sized, A: Allocator,

§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
§

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.

§Examples
#![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)));
1.21.0§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
§

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.

§Examples
#![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.

§Examples
#![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.

§Examples
#![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.

§Examples
#![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.

§Examples
#![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.

§Examples
#![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.

§Examples
#![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>

Formats the value using the given formatter. Read more
§

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,

Auto Trait Implementations§

§

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>

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where 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 P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<R> Rng for R
where R: RngCore + ?Sized,

Source§

fn random<T>(&mut self) -> T

Return a random value via the StandardUniform distribution. Read more
Source§

fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>

Return an iterator over random variates Read more
Source§

fn random_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

Generate a random value in the given range. Read more
Source§

fn random_bool(&mut self, p: f64) -> bool

Return a bool with a probability p of being true. Read more
Source§

fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool

Return a bool with a probability of numerator/denominator of being true. Read more
Source§

fn sample<T, D>(&mut self, distr: D) -> T
where D: Distribution<T>,

Sample a new value, using the given distribution. Read more
Source§

fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>
where D: Distribution<T>, Self: Sized,

Create an iterator that generates values using the given distribution. Read more
Source§

fn fill<T>(&mut self, dest: &mut T)
where T: Fill + ?Sized,

Fill any type implementing Fill with random data Read more
Source§

fn gen<T>(&mut self) -> T

👎Deprecated since 0.9.0: Renamed to random to avoid conflict with the new gen keyword in Rust 2024.
Alias for Rng::random.
Source§

fn gen_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

👎Deprecated since 0.9.0: Renamed to random_range
Source§

fn gen_bool(&mut self, p: f64) -> bool

👎Deprecated since 0.9.0: Renamed to random_bool
Alias for Rng::random_bool.
Source§

fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool

👎Deprecated since 0.9.0: Renamed to random_ratio
Source§

impl<T> RngCore for T
where T: DerefMut, <T as Deref>::Target: RngCore,

Source§

fn next_u32(&mut self) -> u32

Return the next random u32. Read more
Source§

fn next_u64(&mut self) -> u64

Return the next random u64. Read more
Source§

fn fill_bytes(&mut self, dst: &mut [u8])

Fill dest with random data. Read more
§

impl<T> ToString for T
where T: Display + ?Sized,

§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<R> TryRngCore for R
where R: RngCore + ?Sized,

Source§

type Error = Infallible

The type returned in the event of a RNG error.
Source§

fn try_next_u32(&mut self) -> Result<u32, <R as TryRngCore>::Error>

Return the next random u32.
Source§

fn try_next_u64(&mut self) -> Result<u64, <R as TryRngCore>::Error>

Return the next random u64.
Source§

fn try_fill_bytes( &mut self, dst: &mut [u8], ) -> Result<(), <R as TryRngCore>::Error>

Fill dest entirely with random data.
Source§

fn unwrap_err(self) -> UnwrapErr<Self>
where Self: Sized,

Wrap RNG with the UnwrapErr wrapper.
Source§

fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self>

Wrap RNG with the UnwrapMut wrapper.
Source§

fn read_adapter(&mut self) -> RngReadAdapter<'_, Self>
where Self: Sized,

Convert an RngCore to a RngReadAdapter.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> CryptoRng for T
where T: DerefMut, <T as Deref>::Target: CryptoRng,

Source§

impl<R> TryCryptoRng for R
where R: CryptoRng + ?Sized,