Struct without_alloc::rc::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
sourceimpl<'a, T> Rc<'a, T>
impl<'a, T> Rc<'a, T>
sourcepub fn new(val: T, memory: Uninit<'a, ()>) -> Self
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);
sourcepub unsafe fn from_raw(init: Uninit<'a, T>) -> Self
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.
sourcepub fn into_raw(rc: Self) -> Result<Uninit<'a, T>, Self>
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.
sourcepub fn try_unwrap(rc: Self) -> Result<(T, Weak<'a, T>), Self>
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
.
sourcepub fn downgrade(rc: &Self) -> Weak<'a, T>
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);
sourceimpl<T> Rc<'_, T>
impl<T> Rc<'_, T>
sourcepub fn layout() -> Layout
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);
sourcepub fn weak_count(rc: &Self) -> usize
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.
sourcepub fn strong_count(rc: &Self) -> usize
pub fn strong_count(rc: &Self) -> usize
Gets the number of strong pointers to the value.
sourcepub fn get_mut(rc: &mut Self) -> Option<&mut T>
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.
sourcepub fn ptr_eq(this: &Self, other: &Self) -> bool
pub fn ptr_eq(this: &Self, other: &Self) -> bool
Check if two Rc
s 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, ¬_foo));
Trait Implementations
sourceimpl<T> Clone for Rc<'_, T>
impl<T> Clone for Rc<'_, T>
sourcefn clone(&self) -> Self
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 · sourceconst fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read moresourceimpl<T> Drop for Rc<'_, T>
impl<T> Drop for Rc<'_, T>
sourcefn drop(&mut self)
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!"
sourceimpl<T: Ord> Ord for Rc<'_, T>
impl<T: Ord> Ord for Rc<'_, T>
1.21.0 · sourceconst fn max(self, other: Self) -> Self
const fn max(self, other: Self) -> Self
1.21.0 · sourceconst fn min(self, other: Self) -> Self
const fn min(self, other: Self) -> Self
1.50.0 · sourceconst fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
const fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
sourceimpl<'a, 'b, T: PartialEq> PartialEq<Rc<'b, T>> for Rc<'a, T>
impl<'a, 'b, T: PartialEq> PartialEq<Rc<'b, T>> for Rc<'a, T>
sourceimpl<'a, 'b, T: PartialOrd> PartialOrd<Rc<'b, T>> for Rc<'a, T>
impl<'a, 'b, T: PartialOrd> PartialOrd<Rc<'b, T>> for Rc<'a, T>
sourcefn partial_cmp(&self, other: &Rc<'_, T>) -> Option<Ordering>
fn partial_cmp(&self, other: &Rc<'_, T>) -> Option<Ordering>
sourcefn le(&self, other: &Rc<'_, T>) -> bool
fn le(&self, other: &Rc<'_, T>) -> bool
self
and other
) and is used by the <=
operator. Read more