Skip to main content

RefCountedPointer

Trait RefCountedPointer 

Source
pub trait RefCountedPointer {
    type Of<'a, T: ?Sized + 'a>: Clone + Deref<Target = T> + 'a;
    type TakeCellOf<'a, T: 'a>: Clone + 'a;

    // Required methods
    fn new<'a, T: 'a>(value: T) -> Self::Of<'a, T>
       where Self::Of<'a, T>: Sized;
    fn try_unwrap<'a, T: 'a>(ptr: Self::Of<'a, T>) -> Result<T, Self::Of<'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

Trait for reference-counted pointers with shared ownership.

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

This is an independent trait (not a supertrait of Pointer). Both RcBrand and ArcBrand implement Pointer and RefCountedPointer independently.

Required Associated Types§

Source

type Of<'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>.

Source

type TakeCellOf<'a, T: 'a>: Clone + 'a

A cloneable cell that holds an optional value which can be taken exactly once.

For RcBrand, this is Rc<RefCell<Option<T>>>. For ArcBrand, this is Arc<Mutex<Option<T>>>.

Required Methods§

Source

fn new<'a, T: 'a>(value: T) -> Self::Of<'a, T>
where Self::Of<'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);
Source

fn try_unwrap<'a, T: 'a>(ptr: Self::Of<'a, T>) -> Result<T, Self::Of<'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());
Source

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

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".

Implementors§

Source§

impl RefCountedPointer for ArcBrand

Source§

type Of<'a, T: ?Sized + 'a> = Arc<T>

Source§

type TakeCellOf<'a, T: 'a> = Arc<Mutex<Option<T>>>

Source§

impl RefCountedPointer for RcBrand

Source§

type Of<'a, T: ?Sized + 'a> = Rc<T>

Source§

type TakeCellOf<'a, T: 'a> = Rc<RefCell<Option<T>>>