Skip to main content

RefCountedPointer

Trait RefCountedPointer 

Source
pub trait RefCountedPointer: Pointer {
    type CloneableOf<T: ?Sized>: Clone + Deref<Target = T>;

    // Required methods
    fn cloneable_new<T>(value: T) -> Self::CloneableOf<T>
       where Self::CloneableOf<T>: Sized;
    fn try_unwrap<T>(
        ptr: Self::CloneableOf<T>,
    ) -> Result<T, Self::CloneableOf<T>>;
}
Expand description

Extension trait for reference-counted pointers with shared ownership.

Adds CloneableOf associated type which is Clone + Deref. This follows the pattern of SendCloneableFn adding SendOf to CloneableFn.

Required Associated Types§

Source

type CloneableOf<T: ?Sized>: Clone + Deref<Target = T>

The cloneable pointer type constructor.

For Rc/Arc, this is the same as Of<T>.

Required Methods§

Source

fn cloneable_new<T>(value: T) -> Self::CloneableOf<T>
where Self::CloneableOf<T>: Sized,

Wraps a sized value in a cloneable pointer.

§Type Signature

forall self t. t -> self t

§Type Parameters
  • 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<T>(ptr: Self::CloneableOf<T>) -> Result<T, Self::CloneableOf<T>>

Attempts to unwrap the inner value if this is the sole reference.

§Type Signature

forall self t. self t -> Result t (self t)

§Type Parameters
  • 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());

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.

Implementors§