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,
impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>where
T: ?Sized,
Sourcepub fn try_new<U>(
val: U,
allocator: &'a IndexAllocator<MEMORY_SIZE, INDEX_SIZE>,
) -> Result<Self, IndexError>
pub fn try_new<U>( val: U, allocator: &'a IndexAllocator<MEMORY_SIZE, INDEX_SIZE>, ) -> Result<Self, IndexError>
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}Sourcepub fn downgrade(&self) -> Weak<'a, T, MEMORY_SIZE, INDEX_SIZE>
pub fn downgrade(&self) -> Weak<'a, T, MEMORY_SIZE, INDEX_SIZE>
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}Sourcepub fn strong_count(&self) -> usize
pub fn strong_count(&self) -> usize
Return the number of strong reference (see Rc) to the inner value.
Sourcepub fn weak_count(&self) -> usize
pub fn weak_count(&self) -> usize
Return the number of weak reference (see Weak) to the inner value.
Sourcepub fn allocator(&self) -> &'a IndexAllocator<MEMORY_SIZE, INDEX_SIZE>
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,
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
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> Debug for Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>
impl<'a, T, const MEMORY_SIZE: usize, const INDEX_SIZE: usize> Debug for Rc<'a, T, MEMORY_SIZE, INDEX_SIZE>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more