Struct alloc::rc::Rc [] [src]

pub struct Rc<T: ?Sized> {
    _ptr: Shared<RcBox<T>>,
}

A reference-counted pointer type over an immutable value.

See the module level documentation for more details.

Fields

_ptr

Methods

impl<T> Rc<T>

fn new(value: T) -> Rc<T>

Constructs a new Rc<T>.

Examples

use std::rc::Rc;

let five = Rc::new(5);

fn try_unwrap(this: Self) -> Result<T, Self>

Unwraps the contained value if the Rc<T> has only one strong reference. This will succeed even if there are outstanding weak references.

Otherwise, an Err is returned with the same Rc<T>.

Examples

use std::rc::Rc;

let x = Rc::new(3);
assert_eq!(Rc::try_unwrap(x), Ok(3));

let x = Rc::new(4);
let _y = x.clone();
assert_eq!(Rc::try_unwrap(x), Err(Rc::new(4)));

fn would_unwrap(this: &Self) -> bool

Unstable (rc_would_unwrap #28356)

: just added for niche usecase

Checks if Rc::try_unwrap would return Ok.

impl<T: ?Sized> Rc<T>

fn downgrade(this: &Self) -> Weak<T>

Downgrades the Rc<T> to a Weak<T> reference.

Examples

use std::rc::Rc;

let five = Rc::new(5);

let weak_five = Rc::downgrade(&five);

fn weak_count(this: &Self) -> usize

Unstable (rc_counts #28356)

: not clearly useful

Get the number of weak references to this value.

fn strong_count(this: &Self) -> usize

Unstable (rc_counts #28356)

: not clearly useful

Get the number of strong references to this value.

fn is_unique(this: &Self) -> bool

Unstable (rc_counts #28356)

: uniqueness has unclear meaning

Returns true if there are no other Rc or Weak<T> values that share the same inner value.

Examples

#![feature(rc_counts)]

use std::rc::Rc;

let five = Rc::new(5);

assert!(Rc::is_unique(&five));

fn get_mut(this: &mut Self) -> Option<&mut T>

Returns a mutable reference to the contained value if the Rc<T> has one strong reference and no weak references.

Returns None if the Rc<T> is not unique.

Examples

use std::rc::Rc;

let mut x = Rc::new(3);
*Rc::get_mut(&mut x).unwrap() = 4;
assert_eq!(*x, 4);

let _y = x.clone();
assert!(Rc::get_mut(&mut x).is_none());

impl<T: Clone> Rc<T>

fn make_mut(this: &mut Self) -> &mut T

Make a mutable reference into the given Rc<T> by cloning the inner data if the Rc<T> doesn't have one strong reference and no weak references.

This is also referred to as a copy-on-write.

Examples

use std::rc::Rc;

let mut data = Rc::new(5);

*Rc::make_mut(&mut data) += 1;             // Won't clone anything
let mut other_data = data.clone(); // Won't clone inner data
*Rc::make_mut(&mut data) += 1;             // Clones inner data
*Rc::make_mut(&mut data) += 1;             // Won't clone anything
*Rc::make_mut(&mut other_data) *= 2;       // Won't clone anything

// Note: data and other_data now point to different numbers
assert_eq!(*data, 8);
assert_eq!(*other_data, 12);

Trait Implementations

impl<T: ?Sized> !Send for Rc<T>

impl<T: ?Sized> !Sync for Rc<T>

impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Rc<U>> for Rc<T>

impl<T: ?Sized> Deref for Rc<T>

type Target = T

fn deref(&self) -> &T

impl<T: ?Sized> Drop for Rc<T>

fn drop(&mut self)

impl<T: ?Sized> Clone for Rc<T>

fn clone(&self) -> Rc<T>

fn clone_from(&mut self, source: &Self)

impl<T: Default> Default for Rc<T>

fn default() -> Rc<T>

impl<T: ?Sized + PartialEq> PartialEq for Rc<T>

fn eq(&self, other: &Rc<T>) -> bool

fn ne(&self, other: &Rc<T>) -> bool

impl<T: ?Sized + Eq> Eq for Rc<T>

fn assert_receiver_is_total_eq(&self)

impl<T: ?Sized + PartialOrd> PartialOrd for Rc<T>

fn partial_cmp(&self, other: &Rc<T>) -> Option<Ordering>

fn lt(&self, other: &Rc<T>) -> bool

fn le(&self, other: &Rc<T>) -> bool

fn gt(&self, other: &Rc<T>) -> bool

fn ge(&self, other: &Rc<T>) -> bool

impl<T: ?Sized + Ord> Ord for Rc<T>

fn cmp(&self, other: &Rc<T>) -> Ordering

impl<T: ?Sized + Hash> Hash for Rc<T>

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

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

impl<T: ?Sized + Display> Display for Rc<T>

fn fmt(&self, f: &mut Formatter) -> Result

impl<T: ?Sized + Debug> Debug for Rc<T>

fn fmt(&self, f: &mut Formatter) -> Result

impl<T> Pointer for Rc<T>

fn fmt(&self, f: &mut Formatter) -> Result

impl<T> From<T> for Rc<T>

fn from(t: T) -> Self

impl<T: ?Sized> RcBoxPtr<T> for Rc<T>

fn inner(&self) -> &RcBox<T>

fn strong(&self) -> usize

fn inc_strong(&self)

fn dec_strong(&self)

fn weak(&self) -> usize

fn inc_weak(&self)

fn dec_weak(&self)

impl<T: ?Sized> Borrow<T> for Rc<T>

fn borrow(&self) -> &T

impl<T: ?Sized> AsRef<T> for Rc<T>

fn as_ref(&self) -> &T