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.