try_unwrap

Function try_unwrap 

Source
pub fn try_unwrap<P: RefCountedPointer, T>(
    ptr: P::CloneableOf<T>,
) -> Result<T, P::CloneableOf<T>>
Expand description

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

Free function version that dispatches to the type class’ associated function.

§Type Signature

forall a. RefCountedPointer a -> Result a (RefCountedPointer a)

§Type Parameters

  • P: The pointer brand.
  • 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());