cached_pair

Enum Pair

Source
pub enum Pair<L, R> {
    GivenLeft {
        left: L,
        right_cell: OnceCell<R>,
    },
    GivenRight {
        left_cell: OnceCell<L>,
        right: R,
    },
}
Expand description

A pair of values where one can be converted to the other.

§Example

use cached_pair::Pair;

// Construct a pair from a left value.
let pair: Pair<i32, String> = Pair::from_left(42);

// Left value is present, but right value is not.
assert_eq!(pair.left_opt(), Some(&42));
assert_eq!(pair.right_opt(), None);

// Get a right value by converting the left value.
assert_eq!(pair.right_with(|l| l.to_string()), "42");

// Once we get the right value, it is cached.
assert_eq!(pair.right_opt(), Some(&"42".to_string()));

// mutable access
let mut pair = pair;

// Get a mutable reference to the left value.
*pair.left_opt_mut().unwrap() = 123;

// ...then the right value is cleared.
assert_eq!(pair.right_opt(), None);

Variants§

§

GivenLeft

Fields

§left: L
§right_cell: OnceCell<R>
§

GivenRight

Fields

§left_cell: OnceCell<L>
§right: R

Implementations§

Source§

impl<L, R> Pair<L, R>

Source

pub fn from_left(left: L) -> Self

Constructs a pair from a left value.

Source

pub fn from_right(right: R) -> Self

Constructs a pair from a right value.

Source

pub fn left_with<'a, F: FnOnce(&'a R) -> L>(&'a self, f: F) -> &'a L

Returns the left value. If the left value is not available, it converts the right value using the given closure.

Source

pub fn right_with<'a, F: FnOnce(&'a L) -> R>(&'a self, f: F) -> &'a R

Returns the right value. If the right value is not available, it converts the left value using the given closure.

Source

pub fn try_left_with<'a, F: FnOnce(&'a R) -> Result<L, E>, E>( &'a self, f: F, ) -> Result<&'a L, E>

Returns the left value. If the left value is not available, it converts the right value using the given closure.

Source

pub fn try_right_with<'a, F: FnOnce(&'a L) -> Result<R, E>, E>( &'a self, f: F, ) -> Result<&'a R, E>

Returns the right value. If the right value is not available, it converts the left value using the given closure.

Source

pub fn left_mut_with<F: for<'a> FnOnce(&'a R) -> L>(&mut self, f: F) -> &mut L

Returns the left value as a mutable reference. If the left value is not available, it converts the right value using the given closure.

Source

pub fn right_mut_with<F: for<'a> FnOnce(&'a L) -> R>(&mut self, f: F) -> &mut R

Returns the right value as a mutable reference. If the right value is not available, it converts the left value using the given closure.

Source

pub fn try_left_mut_with<F: for<'a> FnOnce(&'a R) -> Result<L, E>, E>( &mut self, f: F, ) -> Result<&mut L, E>

Returns the left value as a mutable reference. If the left value is not available, it converts the right value using the given closure.

Source

pub fn try_right_mut_with<F: for<'a> FnOnce(&'a L) -> Result<R, E>, E>( &mut self, f: F, ) -> Result<&mut R, E>

Returns the right value as a mutable reference. If the right value is not available, it converts the left value using the given closure.

Source

pub fn left_opt(&self) -> Option<&L>

Returns the left value if it is available. Otherwise, returns None.

Source

pub fn right_opt(&self) -> Option<&R>

Returns the right value if it is available. Otherwise, returns None.

Source

pub fn left_opt_mut(&mut self) -> Option<&mut L>

Returns a left value if it is available. If the left value is available, this method clears the right value.

Source

pub fn right_opt_mut(&mut self) -> Option<&mut R>

Returns a right value if it is available. If the right value is available, this method clears the left value.

Source

pub fn left<'a>(&'a self) -> &'a L
where &'a R: Into<L>,

Returns a left value if it is available. If the left value is not available, it uses the Into trait to convert the right value.

Source

pub fn right<'a>(&'a self) -> &'a R
where &'a L: Into<R>,

Returns a right value if it is available. If the right value is not available, it uses the Into trait to convert the left value.

Source

pub fn try_left<'a, E>(&'a self) -> Result<&'a L, E>
where &'a R: TryInto<L, Error = E>,

Returns a left value if it is available. If the left value is not available, it uses the TryInto trait to convert the right value.

Source

pub fn try_right<'a, E>(&'a self) -> Result<&'a R, E>
where &'a L: TryInto<R, Error = E>,

Returns a right value if it is available. If the right value is not available, it uses the TryInto trait to convert the left value.

Auto Trait Implementations§

§

impl<L, R> !Freeze for Pair<L, R>

§

impl<L, R> !RefUnwindSafe for Pair<L, R>

§

impl<L, R> Send for Pair<L, R>
where L: Send, R: Send,

§

impl<L, R> !Sync for Pair<L, R>

§

impl<L, R> Unpin for Pair<L, R>
where L: Unpin, R: Unpin,

§

impl<L, R> UnwindSafe for Pair<L, R>
where L: UnwindSafe, R: 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> 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, 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.