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>
impl<T> Rc<T>
Sourcepub fn new_uninit() -> Rc<MaybeUninit<T>>
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)
Sourcepub fn pin(value: T) -> Pin<Rc<T>>
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.
Sourcepub fn try_unwrap(this: Self) -> Result<T, Self>
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>>
impl<T> Rc<MaybeUninit<T>>
Sourcepub unsafe fn assume_init(self) -> Rc<T>
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>
impl<T> Rc<T>
Sourcepub fn into_raw(this: Self) -> *const T
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) };
Sourcepub fn as_ptr(this: &Self) -> *const T
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");
Sourcepub unsafe fn from_raw(ptr: *const T) -> Self
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
.
Sourcepub fn weak_count(this: &Self) -> usize
pub fn weak_count(this: &Self) -> usize
Sourcepub fn strong_count(this: &Self) -> usize
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));
Sourcepub unsafe fn increment_strong_count(ptr: *const T)
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);
}
Sourcepub unsafe fn decrement_strong_count(ptr: *const T)
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));
}
Sourcepub fn get_mut(this: &mut Self) -> Option<&mut T>
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());
Sourcepub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T
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§impl<T: Clone> Rc<T>
impl<T: Clone> Rc<T>
Sourcepub fn make_mut(this: &mut Self) -> &mut T
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>
impl<T> Adopt for Rc<T>
Source§unsafe fn adopt_unchecked(this: &Self, other: &Self)
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 Rc
s. 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)
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> Clone for Rc<T>
impl<T> Clone for Rc<T>
Source§fn clone(&self) -> Rc<T>
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)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T> Drop for Rc<T>
impl<T> Drop for Rc<T>
Source§fn drop(&mut self)
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 Rc
s, 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 Rc
s outside of the cycle.
Rc
s 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 Rc
s. 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: Ord> Ord for Rc<T>
impl<T: Ord> Ord for Rc<T>
Source§fn cmp(&self, other: &Rc<T>) -> Ordering
fn cmp(&self, other: &Rc<T>) -> Ordering
Comparison for two Rc
s.
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) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialEq> PartialEq for Rc<T>
impl<T: PartialEq> PartialEq for Rc<T>
Source§fn eq(&self, other: &Rc<T>) -> bool
fn eq(&self, other: &Rc<T>) -> bool
Equality for two Rc
s.
Two Rc
s 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 Rc
s 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
fn ne(&self, other: &Rc<T>) -> bool
Inequality for two Rc
s.
Two Rc
s are unequal if their inner values are unequal.
If T
also implements Eq
(implying reflexivity of equality),
two Rc
s 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>
impl<T: PartialOrd> PartialOrd for Rc<T>
Source§fn partial_cmp(&self, other: &Rc<T>) -> Option<Ordering>
fn partial_cmp(&self, other: &Rc<T>) -> Option<Ordering>
Partial comparison for two Rc
s.
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
fn lt(&self, other: &Rc<T>) -> bool
Less-than comparison for two Rc
s.
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
fn le(&self, other: &Rc<T>) -> bool
‘Less than or equal to’ comparison for two Rc
s.
The two are compared by calling <=
on their inner values.
§Examples
use cactusref::Rc;
let five = Rc::new(5);
assert!(five <= Rc::new(5));