pub trait RefCountedPointer: Pointer {
type CloneableOf<'a, T: ?Sized + 'a>: Clone + Deref<Target = T> + 'a;
type TakeCellOf<'a, T: 'a>: Clone + 'a;
// Required methods
fn cloneable_new<'a, T: 'a>(value: T) -> Self::CloneableOf<'a, T>
where Self::CloneableOf<'a, T>: Sized;
fn try_unwrap<'a, T: 'a>(
ptr: Self::CloneableOf<'a, T>,
) -> Result<T, Self::CloneableOf<'a, T>>;
fn take_cell_new<'a, T: 'a>(value: T) -> Self::TakeCellOf<'a, T>;
fn take_cell_take<'a, T: 'a>(cell: &Self::TakeCellOf<'a, T>) -> Option<T>;
}Expand description
Extension trait for reference-counted pointers with shared ownership.
Adds CloneableOf (a cloneable, dereferenceable pointer)
and TakeCellOf (a cloneable cell supporting one-shot value
extraction). The latter pairs the pointer with an appropriate interior mutability
primitive (RefCell for Rc, Mutex for Arc).
Required Associated Types§
Sourcetype CloneableOf<'a, T: ?Sized + 'a>: Clone + Deref<Target = T> + 'a
type CloneableOf<'a, T: ?Sized + 'a>: Clone + Deref<Target = T> + 'a
The cloneable pointer type constructor.
For Rc/Arc, this is the same as Of<'a, T>.
Sourcetype TakeCellOf<'a, T: 'a>: Clone + 'a
type TakeCellOf<'a, T: 'a>: Clone + 'a
Required Methods§
Sourcefn cloneable_new<'a, T: 'a>(value: T) -> Self::CloneableOf<'a, T>where
Self::CloneableOf<'a, T>: Sized,
fn cloneable_new<'a, T: 'a>(value: T) -> Self::CloneableOf<'a, T>where
Self::CloneableOf<'a, T>: Sized,
Wraps a sized value in a cloneable pointer.
§Type Signature
forall T. T -> Self T
§Type Parameters
'a: The lifetime of the value.T: The type of the value to wrap.
§Parameters
value: The value to wrap.
§Returns
The value wrapped in the cloneable pointer type.
§Examples
use fp_library::{
brands::*,
functions::*,
};
let ptr = ref_counted_pointer_new::<RcBrand, _>(42);
assert_eq!(*ptr, 42);Sourcefn try_unwrap<'a, T: 'a>(
ptr: Self::CloneableOf<'a, T>,
) -> Result<T, Self::CloneableOf<'a, T>>
fn try_unwrap<'a, T: 'a>( ptr: Self::CloneableOf<'a, T>, ) -> Result<T, Self::CloneableOf<'a, T>>
Attempts to unwrap the inner value if this is the sole reference.
§Type Signature
forall T. Self T -> Result T (Self T)
§Type Parameters
'a: The lifetime of the wrapped value.T: The type of the wrapped value.
§Parameters
ptr: The pointer to attempt to unwrap.
§Returns
Ok(value) if this is the sole reference, otherwise Err(ptr).
§Examples
use fp_library::{
brands::*,
functions::*,
};
let ptr = ref_counted_pointer_new::<RcBrand, _>(42);
assert_eq!(try_unwrap::<RcBrand, _>(ptr), Ok(42));
let ptr1 = ref_counted_pointer_new::<RcBrand, _>(42);
let ptr2 = ptr1.clone();
assert!(try_unwrap::<RcBrand, _>(ptr1).is_err());Sourcefn take_cell_new<'a, T: 'a>(value: T) -> Self::TakeCellOf<'a, T>
fn take_cell_new<'a, T: 'a>(value: T) -> Self::TakeCellOf<'a, T>
Creates a new take-cell containing the given value.
§Type Signature
forall T. T -> Self T
§Type Parameters
'a: The lifetime of the value.T: The type of the value to store.
§Parameters
value: The value to store in the cell.
§Returns
A new take-cell containing the value.
§Examples
use fp_library::{
brands::*,
functions::*,
};
let cell = take_cell_new::<RcBrand, _>(42);
assert_eq!(take_cell_take::<RcBrand, _>(&cell), Some(42));Sourcefn take_cell_take<'a, T: 'a>(cell: &Self::TakeCellOf<'a, T>) -> Option<T>
fn take_cell_take<'a, T: 'a>(cell: &Self::TakeCellOf<'a, T>) -> Option<T>
Takes the value out of the cell, leaving None behind.
Returns Some(value) the first time, None on subsequent calls.
§Type Signature
forall T. &Self T -> Option T
§Type Parameters
'a: The lifetime of the value.T: The type of the stored value.
§Parameters
cell: The cell to take the value from.
§Returns
Some(value) if the cell still contains a value, None otherwise.
§Examples
use fp_library::{
brands::*,
functions::*,
};
let cell = take_cell_new::<RcBrand, _>(42);
assert_eq!(take_cell_take::<RcBrand, _>(&cell), Some(42));
assert_eq!(take_cell_take::<RcBrand, _>(&cell), None);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.