Struct std::rc::Rc
[−]
[src]
pub struct Rc<T> where T: ?Sized {
// some fields omitted
}
A reference-counted pointer type over an immutable value.
See the module level documentation for more details.
Methods
impl<T> Rc<T>
fn new(value: T) -> Rc<T>
Constructs a new Rc<T>
.
Examples
fn main() { use std::rc::Rc; let five = Rc::new(5); }use std::rc::Rc; let five = Rc::new(5);
fn try_unwrap(this: Rc<T>) -> Result<T, Rc<T>>
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
fn main() { 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))); }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: &Rc<T>) -> bool
Checks if Rc::try_unwrap
would return Ok
.
impl<T> Rc<T> where T: ?Sized
fn downgrade(this: &Rc<T>) -> Weak<T>
Downgrades the Rc<T>
to a Weak<T>
reference.
Examples
fn main() { use std::rc::Rc; let five = Rc::new(5); let weak_five = Rc::downgrade(&five); }use std::rc::Rc; let five = Rc::new(5); let weak_five = Rc::downgrade(&five);
fn weak_count(this: &Rc<T>) -> usize
Get the number of weak references to this value.
fn strong_count(this: &Rc<T>) -> usize
Get the number of strong references to this value.
fn is_unique(this: &Rc<T>) -> bool
Returns true if there are no other Rc
or Weak<T>
values that share
the same inner value.
Examples
#![feature(rc_counts)] fn main() { use std::rc::Rc; let five = Rc::new(5); assert!(Rc::is_unique(&five)); }#![feature(rc_counts)] use std::rc::Rc; let five = Rc::new(5); assert!(Rc::is_unique(&five));
fn get_mut(this: &mut Rc<T>) -> 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
fn main() { 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()); }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> Rc<T> where T: Clone
fn make_unique(&mut self) -> &mut T
: renamed to Rc::make_mut
fn make_mut(this: &mut Rc<T>) -> &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
fn main() { 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); }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);