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§
Implementations§
Source§impl<L, R> Pair<L, R>
impl<L, R> Pair<L, R>
Sourcepub fn from_right(right: R) -> Self
pub fn from_right(right: R) -> Self
Constructs a pair from a right value.
Sourcepub fn left_with<'a, F: FnOnce(&'a R) -> L>(&'a self, f: F) -> &'a L
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.
Sourcepub fn right_with<'a, F: FnOnce(&'a L) -> R>(&'a self, f: F) -> &'a R
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.
Sourcepub fn try_left_with<'a, F: FnOnce(&'a R) -> Result<L, E>, E>(
&'a self,
f: F,
) -> Result<&'a L, E>
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.
Sourcepub fn try_right_with<'a, F: FnOnce(&'a L) -> Result<R, E>, E>(
&'a self,
f: F,
) -> Result<&'a R, E>
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.
Sourcepub fn left_mut_with<F: for<'a> FnOnce(&'a R) -> L>(&mut self, f: F) -> &mut L
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.
Sourcepub fn right_mut_with<F: for<'a> FnOnce(&'a L) -> R>(&mut self, f: F) -> &mut R
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.
Sourcepub fn try_left_mut_with<F: for<'a> FnOnce(&'a R) -> Result<L, E>, E>(
&mut self,
f: F,
) -> Result<&mut L, E>
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.
Sourcepub fn try_right_mut_with<F: for<'a> FnOnce(&'a L) -> Result<R, E>, E>(
&mut self,
f: F,
) -> Result<&mut R, E>
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.
Sourcepub fn left_opt(&self) -> Option<&L>
pub fn left_opt(&self) -> Option<&L>
Returns the left value if it is available. Otherwise, returns None.
Sourcepub fn right_opt(&self) -> Option<&R>
pub fn right_opt(&self) -> Option<&R>
Returns the right value if it is available. Otherwise, returns None.
Sourcepub fn left_opt_mut(&mut self) -> Option<&mut L>
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.
Sourcepub fn right_opt_mut(&mut self) -> Option<&mut R>
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.
Sourcepub fn left<'a>(&'a self) -> &'a L
pub fn left<'a>(&'a self) -> &'a 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.
Sourcepub fn right<'a>(&'a self) -> &'a R
pub fn right<'a>(&'a self) -> &'a 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.
Sourcepub fn try_left<'a, E>(&'a self) -> Result<&'a L, E>
pub fn try_left<'a, E>(&'a self) -> Result<&'a L, 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.
Sourcepub fn try_right<'a, E>(&'a self) -> Result<&'a R, E>
pub fn try_right<'a, E>(&'a self) -> Result<&'a R, 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.
Sourcepub fn into_left_with<F: FnOnce(R) -> L>(self, f: F) -> L
pub fn into_left_with<F: FnOnce(R) -> L>(self, f: F) -> L
Consumes the pair and turn it into a left value.
Sourcepub fn into_right_with<F: FnOnce(L) -> R>(self, f: F) -> R
pub fn into_right_with<F: FnOnce(L) -> R>(self, f: F) -> R
Consumes the pair and turn it into a right value.
Sourcepub fn try_into_left_with<F: FnOnce(R) -> Result<L, E>, E>(
self,
f: F,
) -> Result<L, E>
pub fn try_into_left_with<F: FnOnce(R) -> Result<L, E>, E>( self, f: F, ) -> Result<L, E>
Consumes the pair and turn it into a left value.
Sourcepub fn try_into_right_with<F: FnOnce(L) -> Result<R, E>, E>(
self,
f: F,
) -> Result<R, E>
pub fn try_into_right_with<F: FnOnce(L) -> Result<R, E>, E>( self, f: F, ) -> Result<R, E>
Consumes the pair and turn it into a right value.
Sourcepub fn into_left(self) -> Lwhere
R: Into<L>,
pub fn into_left(self) -> Lwhere
R: Into<L>,
Consumes the pair and turn it into a left value, using From<R> if it’s needed.
Sourcepub fn into_right(self) -> Rwhere
L: Into<R>,
pub fn into_right(self) -> Rwhere
L: Into<R>,
Consumes the pair and turn it into a right value, using From<L> if it’s needed.
Sourcepub fn try_into_left<E>(self) -> Result<L, E>where
R: TryInto<L, Error = E>,
pub fn try_into_left<E>(self) -> Result<L, E>where
R: TryInto<L, Error = E>,
Consumes the pair and turn it into a left value, using TryInto<L> if it’s needed.
Sourcepub fn try_into_right<E>(self) -> Result<R, E>where
L: TryInto<R, Error = E>,
pub fn try_into_right<E>(self) -> Result<R, E>where
L: TryInto<R, Error = E>,
Consumes the pair and turn it into a right value, using TryInto<R> if it’s needed.
Sourcepub fn as_ref(&self) -> EitherOrBoth<&L, &R>
pub fn as_ref(&self) -> EitherOrBoth<&L, &R>
Returns a reference to the pair as itertools::EitherOrBoth.
Trait Implementations§
Source§impl<L, R> From<Pair<L, R>> for EitherOrBoth<L, R>
impl<L, R> From<Pair<L, R>> for EitherOrBoth<L, R>
impl<L: Eq, R: Eq> Eq for Pair<L, R>
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>
impl<L, R> !Sync for Pair<L, R>
impl<L, R> Unpin for Pair<L, R>
impl<L, R> UnwindSafe for Pair<L, R>where
L: UnwindSafe,
R: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more