pub struct UniqueRc<T, A = Global>{ /* private fields */ }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§
Source§impl<T> UniqueRc<T>
impl<T> UniqueRc<T>
Sourcepub fn new(value: T) -> UniqueRc<T>
🔬This is a nightly-only experimental API. (unique_rc_arc)
pub fn new(value: T) -> UniqueRc<T>
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.
Sourcepub fn map<U>(this: UniqueRc<T>, f: impl FnOnce(T) -> U) -> UniqueRc<U>
🔬This is a nightly-only experimental API. (smart_pointer_try_map)
pub fn map<U>(this: UniqueRc<T>, f: impl FnOnce(T) -> U) -> UniqueRc<U>
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);Sourcepub fn try_map<R>(
this: UniqueRc<T>,
f: impl FnOnce(T) -> R,
) -> <<R as Try>::Residual as Residual<UniqueRc<<R as Try>::Output>>>::TryType
🔬This is a nightly-only experimental API. (smart_pointer_try_map)
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
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);Source§impl<T, A> UniqueRc<T, A>where
A: Allocator,
impl<T, A> UniqueRc<T, A>where
A: Allocator,
Sourcepub fn new_in(value: T, alloc: A) -> UniqueRc<T, A>
🔬This is a nightly-only experimental API. (unique_rc_arc)
pub fn new_in(value: T, alloc: A) -> UniqueRc<T, A>
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.
Source§impl<T, A> UniqueRc<T, A>
impl<T, A> UniqueRc<T, A>
Trait Implementations§
Source§impl<T> AsFd for UniqueRc<T>
impl<T> AsFd for UniqueRc<T>
Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Source§impl<T, A> BorrowMut<T> for UniqueRc<T, A>
impl<T, A> BorrowMut<T> for UniqueRc<T, A>
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T, A> Ord for UniqueRc<T, A>
impl<T, A> Ord for UniqueRc<T, A>
Source§fn cmp(&self, other: &UniqueRc<T, A>) -> Ordering
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 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T, A> PartialEq for UniqueRc<T, A>
impl<T, A> PartialEq for UniqueRc<T, A>
Source§impl<T, A> PartialOrd for UniqueRc<T, A>
impl<T, A> PartialOrd for UniqueRc<T, A>
Source§fn partial_cmp(&self, other: &UniqueRc<T, A>) -> Option<Ordering>
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)));Source§fn lt(&self, other: &UniqueRc<T, A>) -> bool
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));Source§fn le(&self, other: &UniqueRc<T, A>) -> bool
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));impl<T, U, A> CoerceUnsized<UniqueRc<U, A>> for UniqueRc<T, A>
impl<T, A> DerefPure for UniqueRc<T, A>
impl<T, U> DispatchFromDyn<UniqueRc<U>> for UniqueRc<T>
impl<T, A> Eq for UniqueRc<T, A>
impl<T, A> PinCoerceUnsized for UniqueRc<T, A>
impl<T, A> !Send for UniqueRc<T, A>
impl<T, A> !Sync for UniqueRc<T, A>
impl<T, A> Unpin for UniqueRc<T, A>
Auto Trait Implementations§
impl<T, A> Freeze for UniqueRc<T, A>
impl<T, A = Global> !RefUnwindSafe for UniqueRc<T, A>
impl<T, A = Global> !UnwindSafe for UniqueRc<T, A>
Blanket Implementations§
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<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);Source§impl<T> Pipe for T
impl<T> Pipe for T
async fn tap_async(self, func: impl AsyncFnOnce(&mut Self)) -> Self
fn tap(self, func: impl FnOnce(&mut Self)) -> Self
fn pipe<R: Sized>(self, func: impl FnOnce(Self) -> R) -> R
fn pipe_as<'a, T: 'a + ?Sized, R: 'a + Sized>(
&'a self,
func: impl FnOnce(&'a T) -> R,
) -> Rwhere
Self: Deref<Target = T>,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.