Struct Rc

Source
pub struct Rc<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize>
where T: ?Sized,
{ /* private fields */ }
Expand description

A smart pointer holding its value in an IndexAllocator and allowing shared ownership between multiple Rc.

The Rc smart pointer can be obtained by using Rc::try_new.

§Example

use index_alloc::IndexAllocator;
use index_alloc::rc::Rc;

let allocator: IndexAllocator<64, 8> = IndexAllocator::empty();

let test_rc = Rc::try_new([1, 2, 3, 4], &allocator).unwrap();
assert_eq!(*test_rc, [1, 2, 3, 4]);

Implementations§

Source§

impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>
where T: ?Sized,

Source

pub fn try_new<U>( val: U, allocator: &'a IndexAllocator<MEMORY_SIZE, INDEX_SIZE>, ) -> Result<Self, IndexError>
where U: 'a, &'a T: From<&'a U>,

Try to create a new Rc owning a value allocated of type T on a IndexAllocator. The inner memory is reference counted and only freed when every strong reference (Rc) are dropped.

§Errors

The method return an IndexError if the allocation failed.

Examples found in repository?
examples/rc_graph.rs (line 62)
58fn main() {
59    let allocator: IndexAllocator<1024, 64> = IndexAllocator::empty();
60
61    let mut main_node = Node::new("Main");
62    let first_child_node = Rc::try_new(Node::new("First child node"), &allocator).unwrap();
63
64    let second_child_node = {
65        let mut node = Node::new("Second child node");
66        node.add_link(Link::Weak(first_child_node.downgrade()));
67        Rc::try_new(node, &allocator).unwrap()
68    };
69
70    main_node.add_link(Link::new_held(first_child_node.clone()));
71    main_node.add_link(Link::new_held(second_child_node.clone()));
72
73    for l in main_node.iter_links() {
74        println!("Main node is linked to : {}", l.to().unwrap().val);
75    }
76
77    for l in second_child_node.iter_links() {
78        println!("Second child node is linked to : {}", l.to().unwrap().val);
79    }
80}
Source

pub fn downgrade(&self) -> Weak<'a, T, MEMORY_SIZE, INDEX_SIZE>

Create a Weak reference to the value owned by the Rc.

Examples found in repository?
examples/rc_graph.rs (line 66)
58fn main() {
59    let allocator: IndexAllocator<1024, 64> = IndexAllocator::empty();
60
61    let mut main_node = Node::new("Main");
62    let first_child_node = Rc::try_new(Node::new("First child node"), &allocator).unwrap();
63
64    let second_child_node = {
65        let mut node = Node::new("Second child node");
66        node.add_link(Link::Weak(first_child_node.downgrade()));
67        Rc::try_new(node, &allocator).unwrap()
68    };
69
70    main_node.add_link(Link::new_held(first_child_node.clone()));
71    main_node.add_link(Link::new_held(second_child_node.clone()));
72
73    for l in main_node.iter_links() {
74        println!("Main node is linked to : {}", l.to().unwrap().val);
75    }
76
77    for l in second_child_node.iter_links() {
78        println!("Second child node is linked to : {}", l.to().unwrap().val);
79    }
80}
Source

pub fn strong_count(&self) -> usize

Return the number of strong reference (see Rc) to the inner value.

Source

pub fn weak_count(&self) -> usize

Return the number of weak reference (see Weak) to the inner value.

Source

pub fn allocator(&self) -> &'a IndexAllocator<MEMORY_SIZE, INDEX_SIZE>

Get a reference to the IndexAllocator used by the Rc.

Trait Implementations§

Source§

impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> Clone for Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>
where T: ?Sized,

Source§

fn clone(&self) -> Self

Create a new Rc referencing to the same value.

§Example
use index_alloc::IndexAllocator;
use index_alloc::rc::Rc;

let allocator: IndexAllocator<64, 8> = IndexAllocator::empty();

let test_rc = Rc::try_new("Hello World", &allocator).unwrap();

{
    let test_ref = Rc::clone(&test_rc);
    assert_eq!(*test_ref, "Hello World");
}
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> Debug for Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>
where T: ?Sized + Debug,

Source§

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

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

impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> Deref for Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>
where T: ?Sized,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> Drop for Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>
where T: ?Sized,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> Freeze for Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>
where T: ?Sized,

§

impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> !RefUnwindSafe for Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>

§

impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> !Send for Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>

§

impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> !Sync for Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>

§

impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> Unpin for Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>
where T: ?Sized,

§

impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> !UnwindSafe for Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>

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.