Struct cactusref::Rc[][src]

pub struct Rc<T> { /* fields omitted */ }
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

Constructs a new Rc<T>.

Examples

use cactusref::Rc;

let five = Rc::new(5);

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)

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

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.

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)

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) };

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");

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.

Creates a new Weak pointer to this allocation.

Examples

use cactusref::Rc;

let five = Rc::new(5);

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

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));

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));

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);
}

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));
}

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());

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");

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));

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

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

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(...). 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 must call unadopt when this no longer holds a strong reference to other.

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(&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);

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.

Safety

Callers must ensure that this has removed an owned reference to other.

For each call to Adopt::unadopt(&this, &other), callers must ensure that a matching call was made to Adopt::adopt(&this, &other).

This crate makes a best-effort attempt to abort the program if an access to a dangling Rc occurs.

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(&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();
unsafe {
    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);

Performs the conversion.

Immutably borrows from an owned value. Read more

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);

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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);

The resulting type after dereferencing.

Dereferences the value.

Formats the value using the given formatter. Read more

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.

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(&foo, &foo2);
    Rc::adopt(&foo2, &foo);
}

drop(foo);    // Doesn't print anything
drop(foo2);   // Prints "dropped 10!" and "dropped 20!"

Cycle Detection and Deallocation Algorithm

Rc::adopt 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 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.

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);

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);

Feeds this value into the given Hasher. Read more

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

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)));

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

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));

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));

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)));

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));

‘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));

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));

‘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));

Formats the value using the given formatter.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

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

recently added

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.