[][src]Struct static_alloc::rc::Rc

pub struct Rc<'a, T> { /* fields omitted */ }

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 Slab to perform them or manually allocate guided by the necessary layout.

Methods

impl<'a, T> Rc<'a, T>[src]

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

Constructs a new Rc<T>.

See also Slab::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 static_alloc::{Slab, rc::Rc};

struct Foo(u32);

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

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

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.

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

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 static_alloc::{Slab, rc::Rc};

struct HotPotato;

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

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

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

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

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.

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

Create a new Weak pointer to the value.

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

Example

use static_alloc::{Slab, rc::Rc};

struct Foo;

let slab: Slab<[u8; 1024]> = Slab::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);

impl<'_, T> Rc<'_, T>[src]

pub fn layout() -> Layout[src]

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 static_alloc::rc::Rc;

struct Foo(u32);
struct Empty;

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

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

Gets the number of weak pointers to the value.

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

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

Gets the number of strong pointers to the value.

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

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.

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

Check if two Rcs point to the same data.

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

Example

use static_alloc::{Slab, rc::Rc};

struct Foo;

let slab: Slab<[u8; 1024]> = Slab::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

impl<'_, T> Deref for Rc<'_, T>[src]

type Target = T

The resulting type after dereferencing.

impl<'_, T: Debug> Debug for Rc<'_, T>[src]

impl<'_, T: Display> Display for Rc<'_, T>[src]

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

impl<'_, T: Eq> Eq for Rc<'_, T>[src]

impl<'_, T: Ord> Ord for Rc<'_, T>[src]

fn max(self, other: Self) -> Self1.21.0[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self1.21.0[src]

Compares and returns the minimum of two values. Read more

fn clamp(self, min: Self, max: Self) -> Self[src]

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

Restrict a value to a certain interval. Read more

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

impl<'_, T> Drop for Rc<'_, T>[src]

fn drop(&mut self)[src]

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 static_alloc::{Slab, rc::Rc};

struct Foo;

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

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

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

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

impl<'_, T: Hash> Hash for Rc<'_, T>[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

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

impl<'_, T> AsRef<T> for Rc<'_, T>[src]

impl<'_, T> Pointer for Rc<'_, T>[src]

impl<'_, T> Clone for Rc<'_, T>[src]

fn clone(&self) -> Self[src]

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 static_alloc::{Slab, rc::Rc};

struct Foo;

let slab: Slab<[u8; 1024]> = Slab::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());

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<'_, T> Borrow<T> for Rc<'_, T>[src]

Auto Trait Implementations

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

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

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

Blanket Implementations

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]