Struct Rc

Source
pub struct Rc<'a, T> { /* private fields */ }
Expand description

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

The inherent methods are all associated functions. This means you can not call them unexpectedly through deref-coercion the reference itself. Instead, you need to call them as Rc::try_unwrap(rc) etc. .

Compared to the standard library version, this will perform its own allocation. Instead, you can ask Bump to perform them or manually allocate guided by the necessary layout.

Implementations§

Source§

impl<'a, T> Rc<'a, T>

Source

pub fn new(val: T, memory: Uninit<'a, ()>) -> Self

Constructs a new Rc<T>.

See also Bump::rc, which encapsulates the process of allocation and construction in a single method call.

§Panics

This function panics if the memory is not valid for the layout of Rc::layout.

§Examples
use core::convert::TryInto;
use without_alloc::{alloc::LocalAllocLeakExt, rc::Rc};
use static_alloc::Bump;

struct Foo(u32);

let slab: Bump<[u8; 1024]> = Bump::uninit();
let layout = Rc::<Foo>::layout().try_into().unwrap();
let memory = slab.alloc_layout(layout).unwrap();
let rc = Rc::new(Foo(0), memory.uninit);
Source

pub unsafe fn from_raw(init: Uninit<'a, T>) -> Self

Wrap a raw initialized value back into an Rc.

§Safety

The block must originate from a previous call to [into_raw] and only the value must have been modified. The value must still be valid.

Source

pub fn into_raw(rc: Self) -> Result<Uninit<'a, T>, Self>

Try to extract the memory.

This returns Some only when this is the last strong and weak reference to the value. The contained value will be preserved and is not dropped. Use from_raw to reinitialize a new Rc with the old value and memory.

§Example
use without_alloc::{alloc::LocalAllocLeakExt, rc::Rc};
use static_alloc::Bump;

struct HotPotato;

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

let slab: Bump<[u8; 1024]> = Bump::uninit();
let foo = slab.rc(HotPotato).unwrap();

let raw = Rc::into_raw(foo).ok().unwrap();
// No panic. Value has not been dropped.
Source

pub fn try_unwrap(rc: Self) -> Result<(T, Weak<'a, T>), Self>

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

Also returns the managed memory in the form of a Weak. This is unusual but the best choice for potentially recovering it. Returning the memory directly is not possible since other Weak<T> instances may still point to it. If you are not interested in the memory you can simply drop the Weak.

Source

pub fn downgrade(rc: &Self) -> Weak<'a, T>

Create a new Weak pointer to the value.

The weak pointer shares ownership over the memory but not over the value itself.

§Example
use without_alloc::{alloc::LocalAllocLeakExt, rc::Rc};
use static_alloc::Bump;

struct Foo;

let slab: Bump<[u8; 1024]> = Bump::uninit();
let foo = slab.rc(Foo).unwrap();
let weak = Rc::downgrade(&foo);

assert_eq!(Rc::weak_count(&foo), 2);
drop(foo);

assert_eq!(weak.weak_count(), 1);
Source§

impl<T> Rc<'_, T>

Source

pub fn layout() -> Layout

Get the layout for memory passed to Rc::new.

You should not rely on the value returned here. The two guarantees are: the size of the layout is at least as large as the input type and it is never empty.

An Rc does not simply point to a lone instance of a type but instead adds some small metadata (two pointer-sized counters). To keep the implementation details private, this method allows allocation of properly sized regions without exposing the exact type that will be stored on the heap.

§Examples
use without_alloc::rc::Rc;

struct Foo(u32);
struct Empty;

assert!(Rc::<Foo>::layout().size() >= 4);
assert!(Rc::<Empty>::layout().size() > 0);
Source

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

Gets the number of weak pointers to the value.

Note that all Rc to the same value count as one weak pointer in total.

Source

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

Gets the number of strong pointers to the value.

Source

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

Try to retrieve a mutable reference to the value.

This method will only succeed if there are no other pointers to the same value, neither strong ones nor weak ones.

Source

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

Check if two Rcs point to the same data.

This will never compare the values but simply inspect the inner pointers.

§Example
use without_alloc::{alloc::LocalAllocLeakExt, rc::Rc};
use static_alloc::Bump;

struct Foo;

let slab: Bump<[u8; 1024]> = Bump::uninit();

// Two Rc's pointing to the same data.
let foo = slab.rc(Foo).unwrap();
let foo2 = Rc::clone(&foo);

// An unrelated allocation.
let not_foo = slab.rc(Foo).unwrap();

assert!( Rc::ptr_eq(&foo, &foo2));
assert!(!Rc::ptr_eq(&foo, &not_foo));

Trait Implementations§

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

Clone the Rc.

This will increment the strong reference count. Only an Rc pointing to a unique value can unwrap or point to the value mutably.

§Examples
use without_alloc::{alloc::LocalAllocLeakExt, rc::Rc};
use static_alloc::Bump; 

struct Foo;

let slab: Bump<[u8; 1024]> = Bump::uninit();

let mut foo  = slab.rc(Foo).unwrap();
assert!(Rc::get_mut(&mut foo).is_some());

let foo2 = Rc::clone(&foo);
assert!(Rc::get_mut(&mut foo).is_none());
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> Deref for Rc<'_, T>

Source§

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.

§Examples
use without_alloc::{alloc::LocalAllocLeakExt, rc::Rc};
use static_alloc::Bump;

struct Foo;

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

let slab: Bump<[u8; 1024]> = Bump::uninit();

let foo  = slab.rc(Foo).unwrap();
let foo2 = Rc::clone(&foo);

drop(foo);    // Doesn't print anything
drop(foo2);   // Prints "dropped!"
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

This method returns an Ordering between self and other. Read more
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,

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

impl<'a, 'b, T: PartialEq> PartialEq<Rc<'b, T>> for Rc<'a, T>

Source§

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

Tests for self and other values to be equal, and is used by ==.
Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, 'b, T: PartialOrd> PartialOrd<Rc<'b, T>> for Rc<'a, T>

Source§

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

This method returns an ordering between self and other values if one exists. Read more
Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
Source§

impl<T> Pointer for Rc<'_, T>

Source§

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

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

impl<T: Eq> Eq for Rc<'_, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for Rc<'a, T>

§

impl<'a, T> !RefUnwindSafe for Rc<'a, T>

§

impl<'a, T> !Send for Rc<'a, T>

§

impl<'a, T> !Sync for Rc<'a, T>

§

impl<'a, T> Unpin for Rc<'a, T>

§

impl<'a, T> !UnwindSafe for Rc<'a, 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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

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>,

Source§

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.