pub struct Genrc<'a, T: ?Sized, C: Atomicity, A: Allocator = Global, const UNIQ: bool = false> { /* private fields */ }
Expand description
Generic implementation behind Rc
, Rcl
, RcBox
, Arc
, Arcl
,
and ArcBox
.
Implementations§
Source§impl<'a, T, C: Atomicity, const UNIQ: bool> Genrc<'a, T, C, Global, UNIQ>
impl<'a, T, C: Atomicity, const UNIQ: bool> Genrc<'a, T, C, Global, UNIQ>
Sourcepub fn new_unique(value: T) -> Genrc<'a, T, C, Global, true>
pub fn new_unique(value: T) -> Genrc<'a, T, C, Global, true>
Constructs a new GenRc<T>
with the given value.
The returned pointer is known to be unique, so it
implements std::ops::DerefMut
Sourcepub fn new_cyclic<F>(data_fn: F) -> Genrc<'a, T, C, Global>
pub fn new_cyclic<F>(data_fn: F) -> Genrc<'a, T, C, Global>
Constructs a new Genrc<T, C>
while giving you a Weak<T, C>
to the allocation,
to allow you to construct a T
which holds a weak pointer to itself.
See std::rc::Rc::new_cyclic
for more details.
Note that RcBox<T>
(or ArcBox<T>
) enable easier construction of
cyclic values; this is here mostly for std
compatibility.
See the main crate documentation for examples.
Source§impl<'a, T, C: Atomicity, A: Allocator, const UNIQ: bool> Genrc<'a, T, C, A, UNIQ>
impl<'a, T, C: Atomicity, A: Allocator, const UNIQ: bool> Genrc<'a, T, C, A, UNIQ>
Sourcepub fn new_in(value: T, alloc: A) -> Selfwhere
A: Allocator,
Available on crate feature allocator_api
only.
pub fn new_in(value: T, alloc: A) -> Selfwhere
A: Allocator,
allocator_api
only.Constructs a new Rc<T, A>
with the given value
Sourcepub fn erase_allocator<'b>(this: Self) -> Genrc<'b, T, C, Global, UNIQ>where
A: 'b,
'a: 'b,
pub fn erase_allocator<'b>(this: Self) -> Genrc<'b, T, C, Global, UNIQ>where
A: 'b,
'a: 'b,
Return an Rc<T, Global>
, erasing whatever allocator type this
may
have had before. Calls to allocator()
on the returned Rc
will return
Global
.
Note that the referent still lives in and will be freed from its original allocation; this just hides the allocator from the type signature.
pub fn allocator(this: &Self) -> &A
Source§impl<'a, T: ?Sized, C: Atomicity, A: Allocator> Genrc<'a, T, C, A>
impl<'a, T: ?Sized, C: Atomicity, A: Allocator> Genrc<'a, T, C, A>
Sourcepub fn from_box(value: Box<T, A>) -> Selfwhere
A: Clone + 'a,
Available on crate feature allocator_api
only.
pub fn from_box(value: Box<T, A>) -> Selfwhere
A: Clone + 'a,
allocator_api
only.Return a Genrc<T, C>
for a boxed value. Unlike std::Rc
, this reuses
the original box allocation rather than copying it. However it still has
to do a small allocation for the header with the reference counts.
If the box uses a custom allocator, the same allocator will be used for
the Rc. If that isn’t the desired behavior, you can call Rc::new
or
new_in
to specify the allocator you want, and then project
to hide
the box.
Source§impl<'a, T: ?Sized, C: Atomicity, A: Allocator> Genrc<'a, T, C, A>
impl<'a, T: ?Sized, C: Atomicity, A: Allocator> Genrc<'a, T, C, A>
Sourcepub fn cast<U>(this: Genrc<'a, T, C, A>) -> Genrc<'a, U, C, A>
pub fn cast<U>(this: Genrc<'a, T, C, A>) -> Genrc<'a, U, C, A>
Convert Genrc<T>
to Genrc<U>
, as long as &T converts to &U.
This should be spelled from()
, but that conflicts with the blanket
impl converting T->T.
TODO: this doesn’t work for slices; there’s no blanket impl
for<'a> &'a [T]: From<&'a [T; N]>
and I don’t know why.
So for now you must call project()
explicitly.
Sourcepub fn ptr_eq<A2: Allocator>(this: &Self, other: &Genrc<'_, T, C, A2>) -> bool
pub fn ptr_eq<A2: Allocator>(this: &Self, other: &Genrc<'_, T, C, A2>) -> bool
Returns true if two Genrc
pointers point to the same object. Note that
this is is not the same as sharing the same allocation: e.g. both might
point to the same static object due to project(), or both might point
to different subobjects of the same root pointer.
Source§impl<'a, T: ?Sized, C: Atomicity, A: Allocator> Genrc<'a, T, C, A, true>
impl<'a, T: ?Sized, C: Atomicity, A: Allocator> Genrc<'a, T, C, A, true>
Sourcepub fn project_mut<'b, U, F: FnOnce(&mut T) -> &mut U>(
s: Self,
f: F,
) -> Genrc<'b, U, C, A, true>where
T: 'a,
U: 'b + ?Sized,
'a: 'b,
pub fn project_mut<'b, U, F: FnOnce(&mut T) -> &mut U>(
s: Self,
f: F,
) -> Genrc<'b, U, C, A, true>where
T: 'a,
U: 'b + ?Sized,
'a: 'b,
Return an RcBox<U>
for any type U contained within T, e.g. an element
of a slice, or &dyn view of an object.
A unique (“Box”) pointer can be lowered to a normal shared pointer
Source§impl<'a, T: ?Sized, C: Atomicity> Genrc<'a, T, C, Global, true>
impl<'a, T: ?Sized, C: Atomicity> Genrc<'a, T, C, Global, true>
Sourcepub fn from_mut_ref(value: &'a mut T) -> Self
pub fn from_mut_ref(value: &'a mut T) -> Self
Constructs a new RcBox<T, ...>
from a reference without copying.
use genrc::RcBox;
let mut x = 5;
{
let mut p : RcBox<i32> = RcBox::from_mut_ref(&mut x);
*p = 6;
}
assert_eq!(x, 6);
Source§impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> Genrc<'a, T, C, A, UNIQ>
impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> Genrc<'a, T, C, A, UNIQ>
Sourcepub fn project<'u, U: ?Sized, F: FnOnce(&T) -> &U>(
this: Self,
f: F,
) -> Genrc<'u, U, C, A, false>
pub fn project<'u, U: ?Sized, F: FnOnce(&T) -> &U>( this: Self, f: F, ) -> Genrc<'u, U, C, A, false>
Return a Genrc<U>
for any type U contained within T, e.g. an element of
a slice, or &dyn view of an object.
Calling project()
on an RcBox
or ArcBox
will downgrade it to a
normal Rc
or Arc
. Use project_mut()
if you need to preserve
uniqueness.
Sourcepub fn try_project<'u, U: ?Sized, F: FnOnce(&T) -> Option<&U>>(
this: Self,
f: F,
) -> Option<Genrc<'u, U, C, A, false>>
pub fn try_project<'u, U: ?Sized, F: FnOnce(&T) -> Option<&U>>( this: Self, f: F, ) -> Option<Genrc<'u, U, C, A, false>>
Fallible version of project()
.
Sourcepub fn downgrade(this: &Genrc<'_, T, C, A, UNIQ>) -> Weak<'a, T, C, A>
pub fn downgrade(this: &Genrc<'_, T, C, A, UNIQ>) -> Weak<'a, T, C, A>
Return a Weak
pointer to this object.
Sourcepub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T
pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T
Returns a mutable reference into the given Rc
, without any check.
See also get_mut
, which is safe and does appropriate checks.
§Safety
If any other Rc
or Weak
pointers to the same allocation exist,
then they must not be dereferenced or have active borrows for the
duration of the returned borrow, and their inner type must be exactly
the same as the inner type of this Rc (including lifetimes). This is
trivially the case if no such pointers exist, for example immediately
after Rc::new
.
pub fn get_mut(this: &mut Self) -> Option<&mut T>
pub fn strong_count(p: &Self) -> usize
pub fn weak_count(p: &Self) -> usize
Trait Implementations§
Source§impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> AsRef<T> for Genrc<'a, T, C, A, UNIQ>
impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> AsRef<T> for Genrc<'a, T, C, A, UNIQ>
Source§impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> Borrow<T> for Genrc<'a, T, C, A, UNIQ>
impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> Borrow<T> for Genrc<'a, T, C, A, UNIQ>
Source§impl<'a, T: 'a + ?Sized + Debug, C: Atomicity, A: Allocator, const UNIQ: bool> Debug for Genrc<'a, T, C, A, UNIQ>
impl<'a, T: 'a + ?Sized + Debug, C: Atomicity, A: Allocator, const UNIQ: bool> Debug for Genrc<'a, T, C, A, UNIQ>
Source§impl<'a, T: Default, C: Atomicity, const UNIQ: bool> Default for Genrc<'a, T, C, Global, UNIQ>
impl<'a, T: Default, C: Atomicity, const UNIQ: bool> Default for Genrc<'a, T, C, Global, UNIQ>
Source§impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> Deref for Genrc<'a, T, C, A, UNIQ>
impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> Deref for Genrc<'a, T, C, A, UNIQ>
Source§impl<'a, T: 'a + ?Sized, C: Atomicity, A: Allocator> DerefMut for Genrc<'a, T, C, A, true>
If we still have a unique reference, we can safely mutate the
contents.
impl<'a, T: 'a + ?Sized, C: Atomicity, A: Allocator> DerefMut for Genrc<'a, T, C, A, true>
If we still have a unique reference, we can safely mutate the contents.
Source§impl<'a, T: 'a + ?Sized + Display, C: Atomicity, A: Allocator, const UNIQ: bool> Display for Genrc<'a, T, C, A, UNIQ>
impl<'a, T: 'a + ?Sized + Display, C: Atomicity, A: Allocator, const UNIQ: bool> Display for Genrc<'a, T, C, A, UNIQ>
Source§impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> Drop for Genrc<'a, T, C, A, UNIQ>
impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> Drop for Genrc<'a, T, C, A, UNIQ>
Source§impl<'a, T: 'a, C: Atomicity, A: Allocator> From<Genrc<'a, T, C, A, true>> for Genrc<'a, T, C, A, false>
A unique pointer can be lowered to a shared pointer.
impl<'a, T: 'a, C: Atomicity, A: Allocator> From<Genrc<'a, T, C, A, true>> for Genrc<'a, T, C, A, false>
A unique pointer can be lowered to a shared pointer.