Skip to main content

try_unwrap

Function try_unwrap 

Source
pub fn try_unwrap<'a, P: RefCountedPointer, T: 'a>(
    ptr: P::CloneableOf<'a, T>,
) -> Result<T, P::CloneableOf<'a, 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 P T. RefCountedPointer P => P T -> Result T (P T)

§Type Parameters

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