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

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

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.

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.

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.

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

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

Gets the number of weak pointers to the value.

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

Gets the number of strong pointers to the value.

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.

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

Converts this type into a shared reference of the (usually inferred) input type.
Immutably borrows from an owned value. Read more

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());
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
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.

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!"
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
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
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
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

Returns the argument unchanged.

Calls U::from(self).

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

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.