genrc::genrc

Struct Genrc

Source
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>

Source

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

Source

pub fn new(value: T) -> Self

Constructs a new GenRc<T> with the given value.

Source

pub fn new_cyclic<F>(data_fn: F) -> Genrc<'a, T, C, Global>
where F: FnOnce(&Weak<'a, T, C>) -> T, T: 'a,

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

pub fn pin(value: T) -> Pin<Self>

Constructs a new Pin<Rc<T>>. If T does not implement Unpin, then value will be pinned in memory and unable to be moved.

Source§

impl<'a, T: ?Sized, C: Atomicity> Genrc<'a, T, C, Global, false>

Source

pub fn from_ref(value: &'a T) -> Self

Constructs a new Genrc<T, ...> from a reference without copying.

use genrc::Rcl;
let x = 5;
let p : Rcl<i32> = Rcl::from_ref(&x);
assert!(std::ptr::eq(&*p, &x));
Source§

impl<'a, T, C: Atomicity, A: Allocator, const UNIQ: bool> Genrc<'a, T, C, A, UNIQ>

Source

pub fn new_in(value: T, alloc: A) -> Self
where A: Allocator,

Available on crate feature allocator_api only.

Constructs a new Rc<T, A> with the given value

Source

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.

Source

pub fn allocator(this: &Self) -> &A

Source§

impl<'a, T: ?Sized, C: Atomicity, A: Allocator> Genrc<'a, T, C, A>

Source

pub fn from_box(value: Box<T, A>) -> Self
where A: Clone + 'a,

Available on crate feature 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>

Source

pub fn cast<U>(this: Genrc<'a, T, C, A>) -> Genrc<'a, U, C, A>
where T: 'a, U: 'a + ?Sized, for<'u> &'u U: From<&'u T>,

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.

Source

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

pub fn root_ptr_eq<T2: ?Sized, A2: Allocator>( this: &Self, other: &Genrc<'_, T2, C, A2>, ) -> bool

Returns true if two Genrc pointers point to the same allocation, i.e. they share reference counts. Note that they may point to different subobjects within that allocation due to project().

Source§

impl<'a, T: ?Sized, C: Atomicity, A: Allocator> Genrc<'a, T, C, A, true>

Source

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.

Source

pub fn shared(this: Genrc<'a, T, C, A, true>) -> Genrc<'a, T, C, A, false>

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>

Source

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>

Source

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.

Source

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().

Source

pub fn downgrade(this: &Genrc<'_, T, C, A, UNIQ>) -> Weak<'a, T, C, A>

Return a Weak pointer to this object.

Source

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.

Source

pub fn get_mut(this: &mut Self) -> Option<&mut T>

Source

pub fn strong_count(p: &Self) -> usize

Source

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>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> Borrow<T> for Genrc<'a, T, C, A, UNIQ>

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<'a, T: ?Sized, C: Atomicity, A: Allocator> Clone for Genrc<'a, T, C, A>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a, T: 'a + ?Sized + Debug, C: Atomicity, A: Allocator, const UNIQ: bool> Debug for Genrc<'a, T, C, A, UNIQ>

Source§

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

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

impl<'a, T: Default, C: Atomicity, const UNIQ: bool> Default for Genrc<'a, T, C, Global, UNIQ>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> Deref for Genrc<'a, T, C, A, UNIQ>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
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.

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'a, T: 'a + ?Sized + Display, C: Atomicity, A: Allocator, const UNIQ: bool> Display for Genrc<'a, T, C, A, UNIQ>

Source§

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

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

impl<'a, T: ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> Drop for Genrc<'a, T, C, A, UNIQ>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
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.

Source§

fn from(uniq: Genrc<'a, T, C, A, true>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T: 'a + ?Sized + Ord, C: Atomicity, A: Allocator, const UNIQ: bool> Ord for Genrc<'a, T, C, A, UNIQ>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a, T: ?Sized + PartialEq, C: Atomicity, A: Allocator, const Q1: bool, const Q2: bool> PartialEq<Genrc<'a, T, C, A, Q2>> for Genrc<'a, T, C, A, Q1>

Source§

fn eq(&self, other: &Genrc<'_, T, C, A, Q2>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a, T: ?Sized + PartialOrd, C: Atomicity, A: Allocator, const Q1: bool, const Q2: bool> PartialOrd<Genrc<'a, T, C, A, Q2>> for Genrc<'a, T, C, A, Q1>

Source§

fn partial_cmp(&self, other: &Genrc<'_, T, C, A, Q2>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
Source§

fn lt(&self, other: &Genrc<'_, T, C, A, Q2>) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
Source§

fn le(&self, other: &Genrc<'_, T, C, A, Q2>) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
Source§

fn gt(&self, other: &Genrc<'_, T, C, A, Q2>) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
Source§

fn ge(&self, other: &Genrc<'_, T, C, A, Q2>) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a, T: 'a + ?Sized, C: Atomicity, A: Allocator, const UNIQ: bool> Pointer for Genrc<'a, T, C, A, UNIQ>

Source§

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

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

impl<'a, T: 'a + ?Sized + Eq, C: Atomicity, A: Allocator, const UNIQ: bool> Eq for Genrc<'a, T, C, A, UNIQ>

Auto Trait Implementations§

§

impl<'a, T, C, A, const UNIQ: bool> Freeze for Genrc<'a, T, C, A, UNIQ>
where T: ?Sized,

§

impl<'a, T, C, A, const UNIQ: bool> RefUnwindSafe for Genrc<'a, T, C, A, UNIQ>

§

impl<'a, T, C, A = Global, const UNIQ: bool = false> !Send for Genrc<'a, T, C, A, UNIQ>

§

impl<'a, T, C, A = Global, const UNIQ: bool = false> !Sync for Genrc<'a, T, C, A, UNIQ>

§

impl<'a, T, C, A, const UNIQ: bool> Unpin for Genrc<'a, T, C, A, UNIQ>
where A: Unpin, T: Unpin + ?Sized,

§

impl<'a, T, C, A, const UNIQ: bool> UnwindSafe for Genrc<'a, T, C, A, UNIQ>

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.