Struct LazyTransform

Source
pub struct LazyTransform<T, U> { /* private fields */ }
Expand description

LazyTransform<T, U> is a synchronized holder type, that holds a value of type T until it is lazily converted into a value of type U.

Implementations§

Source§

impl<T, U> LazyTransform<T, U>

Source

pub fn new(t: T) -> LazyTransform<T, U>

Construct a new, untransformed LazyTransform<T, U> with an argument of type T.

Source

pub fn into_inner(self) -> Result<U, T>

Unwrap the contained value, returning Ok(U) if the LazyTransform<T, U> has been transformed.

§Errors

Iff this instance has not been transformed yet.

§Panics

Iff this instance has been poisoned during transformation.

Source

pub fn try_into_inner(self) -> Result<U, Option<T>>

Unwrap the contained value, returning Ok(Ok(U)) iff the LazyTransform<T, U> has been transformed.

§Errors

Iff this instance has neither been transformed yet nor poisoned, Err(Some(T)) is returned.

Iff this instance has been poisoned by error during a call to .get_or_create_or_poison, Err(None) is returned.

§Panics

Iff this instance has been poisoned by a panic during transformation.

Source§

impl<T, U> LazyTransform<T, U>

Source

pub fn get_or_create<F>(&self, f: F) -> &U
where F: FnOnce(T) -> U,

Get a reference to the transformed value, invoking f to transform it if the LazyTransform<T, U> has yet to be transformed. It is guaranteed that if multiple calls to get_or_create race, only one will invoke its closure, and every call will receive a reference to the newly transformed value.

The closure can only ever be called once, so think carefully about what transformation you want to apply!

§Panics

This method will panic if the instance has been poisoned during a previous transformation attempt.

The method may panic (or deadlock) upon reentrance.

Source

pub fn try_get_or_create<F, E>(&self, f: F) -> Result<&U, E>
where T: Clone, F: FnOnce(T) -> Result<U, E>,

Try to get a reference to the transformed value, invoking a fallible f to transform it if the LazyTransform<T, U> has yet to be transformed. It is guaranteed that if multiple calls to get_or_create race, only one will successfully invoke its closure, and every call will receive a reference to the newly transformed value.

The closure can only ever be successfully called once, so think carefully about what transformation you want to apply!

§Errors

Iff f returns a Result::Err, this error is returned verbatim.

§Panics

This method will panic if the instance has been poisoned during a previous transformation attempt.

The method may panic (or deadlock) upon reentrance.

Source

pub fn get_or_create_or_poison<F, E>(&self, f: F) -> Result<&U, Option<E>>
where F: FnOnce(T) -> Result<U, E>,

Try to get a reference to the transformed value, invoking a fallible f to transform it if the LazyTransform<T, U> has yet to be transformed. It is guaranteed that if multiple calls to get_or_create race, only one will invoke its closure, and every call will receive a reference to the newly transformed value.

The closure can only ever be called once, so think carefully about what transformation you want to apply!

§Errors

Iff this instance is poisoned, except by panics, Err(None) is returned.

Iff f returns a Result::Err, this error is returned wrapped in Some.

§Panics

This method will panic if the instance has been poisoned due to a panic during a previous transformation attempt.

The method may panic (or deadlock) upon reentrance.

Source

pub fn get(&self) -> Option<&U>

Get a reference to the transformed value, returning Some(&U) if the LazyTransform<T, U> has been transformed or None if it has not. It is guaranteed that if a reference is returned it is to the transformed value inside the the LazyTransform<T>.

Trait Implementations§

Source§

impl<T, U> Clone for LazyTransform<T, U>
where T: Clone, U: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, U> Default for LazyTransform<T, U>
where T: Default,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, U> Sync for LazyTransform<T, U>
where T: Send, U: Send + Sync,

Auto Trait Implementations§

§

impl<T, U> !Freeze for LazyTransform<T, U>

§

impl<T, U> !RefUnwindSafe for LazyTransform<T, U>

§

impl<T, U> Send for LazyTransform<T, U>
where T: Send, U: Send,

§

impl<T, U> Unpin for LazyTransform<T, U>
where T: Unpin, U: Unpin,

§

impl<T, U> UnwindSafe for LazyTransform<T, U>
where T: UnwindSafe, U: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.