Struct cactusref::Rc

source ·
pub struct Rc<T> { /* private fields */ }
Expand description

A single-threaded reference-counting pointer. ‘Rc’ stands for ‘Reference Counted’.

See the module-level documentation for more details.

The inherent methods of Rc are all associated functions, which means that you have to call them as e.g., Rc::get_mut(&mut value) instead of value.get_mut(). This avoids conflicts with methods of the inner type T.

Implementations§

source§

impl<T> Rc<T>

source

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

Constructs a new Rc<T>.

§Examples
use cactusref::Rc;

let five = Rc::new(5);
source

pub fn new_uninit() -> Rc<MaybeUninit<T>>

Constructs a new Rc with uninitialized contents.

§Examples
use cactusref::Rc;

let mut five = Rc::<u32>::new_uninit();

let five = unsafe {
    // Deferred initialization:
    Rc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);

    five.assume_init()
};

assert_eq!(*five, 5)
source

pub fn pin(value: T) -> Pin<Rc<T>>

Constructs a new Pin<Rc<T>>. If T does not implement Unpin, then value will be pinned in memory and unable to be moved.

source

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

Returns the inner value, if the Rc has exactly one strong reference.

Otherwise, an Err is returned with the same Rc that was passed in.

This will succeed even if there are outstanding weak references.

§Examples
use cactusref::Rc;

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

let x = Rc::new(4);
let _y = Rc::clone(&x);
assert_eq!(*Rc::try_unwrap(x).unwrap_err(), 4);
§Errors

If the given Rc does not have exactly one strong reference, it is returned in the Err variant of the returned Result.

source§

impl<T> Rc<MaybeUninit<T>>

source

pub unsafe fn assume_init(self) -> Rc<T>

Converts to Rc<T>.

§Safety

As with MaybeUninit::assume_init, it is up to the caller to guarantee that the inner value really is in an initialized state. Calling this when the content is not yet fully initialized causes immediate undefined behavior.

§Examples
use cactusref::Rc;

let mut five = Rc::<u32>::new_uninit();

let five = unsafe {
    // Deferred initialization:
    Rc::get_mut_unchecked(&mut five).as_mut_ptr().write(5);

    five.assume_init()
};

assert_eq!(*five, 5)
source§

impl<T> Rc<T>

source

pub fn into_raw(this: Self) -> *const T

Consumes the Rc, returning the wrapped pointer.

To avoid a memory leak the pointer must be converted back to an Rc using Rc::from_raw.

§Examples
use cactusref::Rc;

let x = Rc::new("hello".to_owned());
let x_ptr = Rc::into_raw(x);
assert_eq!(unsafe { &*x_ptr }, "hello");
// Reconstruct the `Rc` to avoid a leak.
let _ = unsafe { Rc::from_raw(x_ptr) };
source

pub fn as_ptr(this: &Self) -> *const T

Provides a raw pointer to the data.

The counts are not affected in any way and the Rc is not consumed. The pointer is valid for as long there are strong counts in the Rc.

§Examples
use cactusref::Rc;

let x = Rc::new("hello".to_owned());
let y = Rc::clone(&x);
let x_ptr = Rc::as_ptr(&x);
assert_eq!(x_ptr, Rc::as_ptr(&y));
assert_eq!(unsafe { &*x_ptr }, "hello");
source

pub unsafe fn from_raw(ptr: *const T) -> Self

Constructs an Rc<T> from a raw pointer.

The raw pointer must have been previously returned by a call to Rc<U>::into_raw where U must have the same size and alignment as T. This is trivially true if U is T. Note that if U is not T but has the same size and alignment, this is basically like transmuting references of different types. See mem::transmute for more information on what restrictions apply in this case.

The user of from_raw has to make sure a specific value of T is only dropped once.

This function is unsafe because improper use may lead to memory unsafety, even if the returned Rc<T> is never accessed.

§Examples
use cactusref::Rc;

let x = Rc::new("hello".to_owned());
let x_ptr = Rc::into_raw(x);

unsafe {
    // Convert back to an `Rc` to prevent leak.
    let x = Rc::from_raw(x_ptr);
    assert_eq!(&*x, "hello");

    // Further calls to `Rc::from_raw(x_ptr)` would be memory-unsafe.
}

// The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling!
§Safety

Callers must ensure that ptr points to a live Rc and was created with a call to Rc::into_raw.

source

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

Creates a new Weak pointer to this allocation.

§Examples
use cactusref::Rc;

let five = Rc::new(5);

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

pub fn weak_count(this: &Self) -> usize

Gets the number of Weak pointers to this allocation.

§Examples
use cactusref::Rc;

let five = Rc::new(5);
let _weak_five = Rc::downgrade(&five);

assert_eq!(1, Rc::weak_count(&five));
source

pub fn strong_count(this: &Self) -> usize

Gets the number of strong (Rc) pointers to this allocation.

§Examples
use cactusref::Rc;

let five = Rc::new(5);
let _also_five = Rc::clone(&five);

assert_eq!(2, Rc::strong_count(&five));
source

pub unsafe fn increment_strong_count(ptr: *const T)

Increments the strong reference count on the Rc<T> associated with the provided pointer by one.

§Safety

The pointer must have been obtained through Rc::into_raw, and the associated Rc instance must be valid (i.e. the strong count must be at least 1) for the duration of this method.

§Examples
use cactusref::Rc;

let five = Rc::new(5);

unsafe {
    let ptr = Rc::into_raw(five);
    Rc::increment_strong_count(ptr);

    let five = Rc::from_raw(ptr);
    assert_eq!(2, Rc::strong_count(&five));

    // Decrement the strong count to avoid a leak.
    Rc::decrement_strong_count(ptr);
}
source

pub unsafe fn decrement_strong_count(ptr: *const T)

Decrements the strong reference count on the Rc<T> associated with the provided pointer by one.

§Safety

The pointer must have been obtained through Rc::into_raw, and the associated Rc instance must be valid (i.e. the strong count must be at least 1) when invoking this method. This method can be used to release the final Rc and backing storage, but should not be called after the final Rc has been released.

§Examples
use cactusref::Rc;

let five = Rc::new(5);

unsafe {
    let ptr = Rc::into_raw(five);
    Rc::increment_strong_count(ptr);

    let five = Rc::from_raw(ptr);
    assert_eq!(2, Rc::strong_count(&five));
    Rc::decrement_strong_count(ptr);
    assert_eq!(1, Rc::strong_count(&five));
}
source

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

Returns a mutable reference into the given Rc, if there are no other Rc or Weak pointers to the same allocation.

Returns None otherwise, because it is not safe to mutate a shared value.

See also make_mut, which will clone the inner value when there are other pointers.

§Examples
use cactusref::Rc;

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

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

pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T

Returns a mutable reference into the given Rc, without any check.

See also get_mut, which is safe and does appropriate checks.

§Safety

Any other Rc or Weak pointers to the same allocation must not be dereferenced for the duration of the returned borrow. This is trivially the case if no such pointers exist, for example immediately after Rc::new.

§Examples
use cactusref::Rc;

let mut x = Rc::new(String::new());
unsafe {
    Rc::get_mut_unchecked(&mut x).push_str("foo")
}
assert_eq!(*x, "foo");
source

pub fn ptr_eq(this: &Self, other: &Self) -> bool

Returns true if the two Rcs point to the same allocation (in a vein similar to ptr::eq).

§Examples
use cactusref::Rc;

let five = Rc::new(5);
let same_five = Rc::clone(&five);
let other_five = Rc::new(5);

assert!(Rc::ptr_eq(&five, &same_five));
assert!(!Rc::ptr_eq(&five, &other_five));
source§

impl<T: Clone> Rc<T>

source

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

Makes a mutable reference into the given Rc.

If there are other Rc pointers to the same allocation, then make_mut will clone the inner value to a new allocation to ensure unique ownership. This is also referred to as clone-on-write.

If there are no other Rc pointers to this allocation, then Weak pointers to this allocation will be disassociated.

See also get_mut, which will fail rather than cloning.

§Examples
use cactusref::Rc;

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

*Rc::make_mut(&mut data) += 1;        // Won't clone anything
let mut other_data = Rc::clone(&data);    // 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

// Now `data` and `other_data` point to different allocations.
assert_eq!(*data, 8);
assert_eq!(*other_data, 12);

Weak pointers will be disassociated:

use cactusref::Rc;

let mut data = Rc::new(75);
let weak = Rc::downgrade(&data);

assert!(75 == *data);
assert!(75 == *weak.upgrade().unwrap());

*Rc::make_mut(&mut data) += 1;

assert!(76 == *data);
assert!(weak.upgrade().is_none());

Trait Implementations§

source§

impl<T> Adopt for Rc<T>

Implementation of Adopt for Rc which enables Rcs to form a cycle of strong references that are reaped by Rc’s Drop implementation.

source§

unsafe fn adopt_unchecked(this: &Self, other: &Self)

Perform bookkeeping to record that this has an owned reference to other.

Adoption is a one-way link, or a directed edge in the object graph which means “this owns other”.

adopt can be called multiple times for a pair of Rcs. Each call to adopt indicates that this owns one distinct clone of other.

This is an associated function that needs to be used as Rc::adopt_unchecked(...). A method would interfere with methods of the same name on the contents of a Rc used through Deref.

§Safety

Callers must ensure that this owns a strong reference to other.

Callers should call unadopt when this no longer holds a strong reference to other to avoid memory leaks, but this is not required for soundness.

Calling adopt does not increment the strong count of other. Callers must ensure that other has been cloned and stored in the T contained by this.

§Examples

The following implements a self-referential array.

use cactusref::{Adopt, Rc};
use std::cell::RefCell;

#[derive(Default)]
struct Array {
    buffer: Vec<Rc<RefCell<Self>>>,
}

let array = Rc::new(RefCell::new(Array::default()));
for _ in 0..10 {
    let item = Rc::clone(&array);
    unsafe {
        Rc::adopt_unchecked(&array, &item);
    }
    array.borrow_mut().buffer.push(item);
}
let weak = Rc::downgrade(&array);
// 1 for the array binding, 10 for the `Rc`s in buffer
assert_eq!(Rc::strong_count(&array), 11);
drop(array);
assert!(weak.upgrade().is_none());
assert_eq!(weak.weak_count(), 0);
source§

fn unadopt(this: &Self, other: &Self)

Perform bookkeeping to record that this has removed an owned reference to other.

Adoption is a one-way link, or a directed edge in the object graph which means “this owns other”.

This is an associated function that needs to be used as Adopt::unadopt(...). A method would interfere with methods of the same name on the contents of a Rc used through Deref.

§Memory Leaks

Failure to call this function when removing an owned Rc from this is safe, but may result in a memory leak.

§Examples

The following implements a self-referential array.

use cactusref::{Adopt, Rc};
use std::cell::RefCell;

#[derive(Default)]
struct Array {
    buffer: Vec<Rc<RefCell<Self>>>,
}

let array = Rc::new(RefCell::new(Array::default()));
for _ in 0..10 {
    let item = Rc::clone(&array);
    unsafe {
        Rc::adopt_unchecked(&array, &item);
    }
    array.borrow_mut().buffer.push(item);
}
let weak = Rc::downgrade(&array);
// 1 for the array binding, 10 for the `Rc`s in buffer
assert_eq!(Rc::strong_count(&array), 11);

let head = array.borrow_mut().buffer.pop().unwrap();
Rc::unadopt(&array, &head);

drop(head);
assert_eq!(Rc::strong_count(&array), 10);
drop(array);
assert!(weak.upgrade().is_none());
assert_eq!(weak.weak_count(), 0);
source§

impl<T> AsRef<T> for Rc<T>

source§

fn as_ref(&self) -> &T

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

impl<T> Borrow<T> for Rc<T>

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> Clone for Rc<T>

source§

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

Makes a clone of the Rc pointer.

This creates another pointer to the same allocation, increasing the strong reference count.

§Examples
use cactusref::Rc;

let five = Rc::new(5);

let _ = Rc::clone(&five);
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Rc<T>

source§

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

Formats the value using the given formatter. Read more
source§

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

source§

fn default() -> Rc<T>

Creates a new Rc<T>, with the Default value for T.

§Examples
use cactusref::Rc;

let x: Rc<i32> = Default::default();
assert_eq!(*x, 0);
source§

impl<T> Deref for Rc<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &T

Dereferences the value.
source§

impl<T: Display> Display for Rc<T>

source§

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

Formats the value using the given formatter. Read more
source§

impl<T> Drop for Rc<T>

source§

fn drop(&mut self)

Drops the Rc.

This will decrement the strong reference count. If the strong reference count reaches zero then the only other references (if any) are Weak, so we drop the inner value.

If this Rc has adopted any other Rcs, drop will trace the reachable object graph and detect if this Rc is part of an orphaned cycle. An orphaned cycle is a cycle in which all members have no owned references held by Rcs outside of the cycle.

Rcs do not pay the cost of the reachability check unless they use Adopt::adopt_unchecked.

§Examples
use cactusref::Rc;

struct Foo;

impl Drop for Foo {
    fn drop(&mut self) {
        println!("dropped!");
    }
}

let foo  = Rc::new(Foo);
let foo2 = Rc::clone(&foo);

drop(foo);    // Doesn't print anything
drop(foo2);   // Prints "dropped!"
use cactusref::{Adopt, Rc};

struct Foo(u8);

impl Drop for Foo {
    fn drop(&mut self) {
        println!("dropped {}!", self.0);
    }
}

let foo  = Rc::new(Foo(10));
let foo2 = Rc::new(Foo(20));

unsafe {
    Rc::adopt_unchecked(&foo, &foo2);
    Rc::adopt_unchecked(&foo2, &foo);
}

drop(foo);    // Doesn't print anything
drop(foo2);   // Prints "dropped 10!" and "dropped 20!"
§Cycle Detection and Deallocation Algorithm

Rc::adopt_unchecked does explicit bookkeeping to store links to adoptee Rcs. These links form a graph of reachable objects which are used to detect cycles.

On drop, if an Rc has no links, it is dropped like a normal Rc. If the Rc has links, Drop performs a breadth first search by traversing the forward and backward links stored in each Rc. Deallocating cycles requires correct use of Adopt::adopt_unchecked and Adopt::unadopt to perform the reachability bookkeeping.

After determining all reachable objects, Rc reduces the graph to objects that form a cycle by performing pairwise reachability checks. During this step, for each object in the cycle, Rc counts the number of refs held by other objects in the cycle.

Using the cycle-held references, Rc computes whether the object graph is reachable by any non-cycle nodes by comparing strong counts.

If the cycle is orphaned, Rc busts all the link structures and deallocates each object.

§Performance

Cycle detection uses breadth first search to trace the object graph. The runtime complexity of detecting a cycle is O(links + nodes) where links is the number of adoptions that are alive and nodes is the number of objects in the cycle.

Determining whether the cycle is orphaned builds on cycle detection and iterates over all nodes in the graph to see if their strong count is greater than the number of references in the cycle. The runtime complexity of finding an orphaned cycle is O(links + nodes) where links is the number of adoptions that are alive and nodes is the number objects in the cycle.

source§

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

source§

fn from(v: Box<T>) -> Rc<T>

Move a boxed object to a new, reference counted, allocation.

§Example
let original: Box<i32> = Box::new(1);
let shared: Rc<i32> = Rc::from(original);
assert_eq!(1, *shared);
source§

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

source§

fn from(t: T) -> Self

Converts a generic type T into a Rc<T>

The conversion allocates on the heap and moves t from the stack into it.

§Example
let x = 5;
let rc = Rc::new(5);

assert_eq!(Rc::from(x), rc);
source§

impl<T: Hash> Hash for Rc<T>

source§

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

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

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
source§

impl<T: Ord> Ord for Rc<T>

source§

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

Comparison for two Rcs.

The two are compared by calling cmp() on their inner values.

§Examples
use cactusref::Rc;
use std::cmp::Ordering;

let five = Rc::new(5);

assert_eq!(Ordering::Less, five.cmp(&Rc::new(6)));
1.21.0 · source§

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

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

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

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

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

Restrict a value to a certain interval. Read more
source§

impl<T: PartialEq> PartialEq for Rc<T>

source§

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

Equality for two Rcs.

Two Rcs are equal if their inner values are equal, even if they are stored in different allocation.

If T also implements Eq (implying reflexivity of equality), two Rcs that point to the same allocation are always equal.

§Examples
use cactusref::Rc;

let five = Rc::new(5);

assert!(five == Rc::new(5));
source§

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

Inequality for two Rcs.

Two Rcs are unequal if their inner values are unequal.

If T also implements Eq (implying reflexivity of equality), two Rcs that point to the same allocation are never unequal.

§Examples
use cactusref::Rc;

let five = Rc::new(5);

assert!(five != Rc::new(6));
source§

impl<T: PartialOrd> PartialOrd for Rc<T>

source§

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

Partial comparison for two Rcs.

The two are compared by calling partial_cmp() on their inner values.

§Examples
use cactusref::Rc;
use std::cmp::Ordering;

let five = Rc::new(5);

assert_eq!(Some(Ordering::Less), five.partial_cmp(&Rc::new(6)));
source§

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

Less-than comparison for two Rcs.

The two are compared by calling < on their inner values.

§Examples
use cactusref::Rc;

let five = Rc::new(5);

assert!(five < Rc::new(6));
source§

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

‘Less than or equal to’ comparison for two Rcs.

The two are compared by calling <= on their inner values.

§Examples
use cactusref::Rc;

let five = Rc::new(5);

assert!(five <= Rc::new(5));
source§

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

Greater-than comparison for two Rcs.

The two are compared by calling > on their inner values.

§Examples
use cactusref::Rc;

let five = Rc::new(5);

assert!(five > Rc::new(4));
source§

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

‘Greater than or equal to’ comparison for two Rcs.

The two are compared by calling >= on their inner values.

§Examples
use cactusref::Rc;

let five = Rc::new(5);

assert!(five >= Rc::new(5));
source§

impl<T> Pointer for Rc<T>

source§

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

Formats the value using the given formatter.
source§

impl<T: Eq> Eq for Rc<T>

source§

impl<T> Unpin for Rc<T>

Auto Trait Implementations§

§

impl<T> Freeze for Rc<T>

§

impl<T> !RefUnwindSafe for Rc<T>

§

impl<T> !Send for Rc<T>

§

impl<T> !Sync for Rc<T>

§

impl<T> !UnwindSafe for Rc<T>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

type Error = Infallible

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

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

Performs the conversion.
source§

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.
source§

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

Performs the conversion.