Struct Pair

Source
pub struct Pair<L, R, C = StdConverter> { /* private fields */ }
Expand description

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

This data structure stores a pair or either a left or right value. The left and right values can be converted to each other using the Converter trait. By default, the StdConverter, which converts using From and TryFrom traits is used, but you can use custom closures by using fn_converter(), or you can implement Converter for your own type.

The getter methods like left() and right() use this converter when that side’s value is not present. Once a value is accessed, it is cached and will not be converted again. This caching can happen even in non-mutable methods.

On the other hand, the optional getter methods like left_opt() and right_opt() do not use the converter and always return the original value, thus they can return None.

§Mutable accessors

Note that mutable accessors like left_mut() and right_mut() will erase the other value. This is because this pair is designed so that one side of the pair is a cache of the other side, so once either side is mutated, the other side becomes dirty and should be cleared.

§Example

use cached_pair::{Pair, fn_converter};
use std::convert::Infallible;
use std::num::ParseIntError;

// Define a converter between i32 and String using fn_converter
let converter = fn_converter(
    |s: &String| s.parse::<i32>(),  // String -> i32 (may fail)
    |i: &i32| Ok::<String, Infallible>(i.to_string()),    // i32 -> String (never fails)
);

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

// 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(), &"42".to_string());

// 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);

Implementations§

Source§

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

Source

pub fn from_left(left: L) -> Self
where C: Default,

Creates a new pair from a left value.

Source

pub fn from_right(right: R) -> Self
where C: Default,

Creates a new pair from a right value.

Source

pub fn from_left_conv(left: L, converter: C) -> Self

Creates a new pair from a left value with a custom converter.

Source

pub fn from_right_conv(right: R, converter: C) -> Self

Creates a new pair from a right value with a custom converter.

Source

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

Returns a reference to the left value if available.

Returns None if the left value is not present.

Source

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

Returns a reference to the right value if available.

Returns None if the right value is not present.

Source

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

Returns a mutable reference to the left value if available.

Returns None if the left value is not present. Note: Obtaining a mutable reference will erase the right value.

Source

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

Returns a mutable reference to the right value if available.

Returns None if the right value is not present. Note: Obtaining a mutable reference will erase the left value.

Source

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

Returns a left value if it is available. If the left value is not available, converts the right value using the given closure. The closure must not fail.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

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

Returns a right value if it is available. If the right value is not available, converts the left value using the given closure. The closure must not fail.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

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

Returns a left value if it is available. If the left value is not available, attempts to convert the right value using the given closure.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

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

Returns a right value if it is available. If the right value is not available, attempts to convert the left value using the given closure.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

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

Returns a mutable reference to the left value if it is available. Note: Obtaining a mutable reference will erase the right value. If the left value is not available, converts the right value using the given closure. The closure must not fail.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

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

Returns a mutable reference to the right value if it is available. Note: Obtaining a mutable reference will erase the left value. If the right value is not available, converts the left value using the given closure. The closure must not fail.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

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

Returns a mutable reference to the left value if it is available. Note: Obtaining a mutable reference will erase the right value. If the left value is not available, attempts to convert the right value using the given closure.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

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

Returns a mutable reference to the right value if it is available. Note: Obtaining a mutable reference will erase the left value. If the right value is not available, attempts to convert the left value using the given closure.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

pub unsafe fn into_left_with<F: FnOnce(R) -> L>(self, f: F) -> L

Consumes the pair and returns the left value. If the left value is not available, converts the right value using the given closure. The closure must not fail.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

pub unsafe fn into_right_with<F: FnOnce(L) -> R>(self, f: F) -> R

Consumes the pair and returns the right value. If the right value is not available, converts the left value using the given closure. The closure must not fail.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

pub unsafe fn try_into_left_with<F: FnOnce(R) -> Result<L, E>, E>( self, f: F, ) -> Result<L, E>

Consumes the pair and attempts to return the left value. If the left value is not available, attempts to convert the right value using the given closure.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

pub unsafe fn try_into_right_with<F: FnOnce(L) -> Result<R, E>, E>( self, f: F, ) -> Result<R, E>

Consumes the pair and attempts to return the right value. If the right value is not available, attempts to convert the left value using the given closure.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

pub fn as_ref(&self) -> EitherOrBoth<&L, &R>

Returns a reference to the pair as EitherOrBoth enum.

Provides a view of the pair’s current state using the EitherOrBoth type. If you specify the itertools feature, it uses the itertools::EitherOrBoth type. Otherwise, it uses the EitherOrBoth enum defined in this crate.

Source

pub unsafe fn extract_left_with<F: FnOnce(&L) -> R>( &mut self, f: F, ) -> Option<L>

Clears the left value if it exists and returns it. If the left value is the only value in the pair, converts it to a right value using the given closure before clearing. Returns None if the left value doesn’t exist. The closure must not fail.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

pub unsafe fn extract_right_with<F: FnOnce(&R) -> L>( &mut self, f: F, ) -> Option<R>

Clears the right value if it exists and returns it. If the right value is the only value in the pair, converts it to a left value using the given closure before clearing. Returns None if the right value doesn’t exist. The closure must not fail.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

pub unsafe fn try_extract_left_with<F: FnOnce(&L) -> Result<R, E>, E>( &mut self, f: F, ) -> Result<Option<L>, E>

Clears the left value if it exists and returns it. If the left value is the only value in the pair, attempts to convert it to a right value using the given closure before clearing. Returns None if the left value doesn’t exist. Returns Err if conversion fails when needed.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

pub unsafe fn try_extract_right_with<F: FnOnce(&R) -> Result<L, E>, E>( &mut self, f: F, ) -> Result<Option<R>, E>

Clears the right value if it exists and returns it. If the right value is the only value in the pair, attempts to convert it to a left value using the given closure before clearing. Returns None if the right value doesn’t exist. Returns Err if conversion fails when needed.

§Safety

The conversion function must be consistent with the converter’s behavior. Inconsistent conversions may lead to invalid state.

Source

pub fn converter(&self) -> &C

Returns a reference to the converter used by this pair.

Source

pub fn converter_mut(&mut self) -> &mut C

Returns a mutable reference to the converter used by this pair.

Source§

impl<L, R, C> Pair<L, R, C>
where C: Converter<L, R>,

Source

pub fn try_left(&self) -> Result<&L, C::ToLeftError>

Attempts to get a reference to the left value, converting if necessary.

If the left value is not available, attempts to convert the right value using the converter.

Source

pub fn try_right(&self) -> Result<&R, C::ToRightError>

Attempts to get a reference to the right value, converting if necessary.

If the right value is not available, attempts to convert the left value using the converter.

Source

pub fn try_left_mut(&mut self) -> Result<&mut L, C::ToLeftError>

Attempts to get a mutable reference to the left value, converting if necessary.

If the left value is not available, attempts to convert the right value using the converter. Note: Obtaining a mutable reference will erase the right value.

Source

pub fn try_right_mut(&mut self) -> Result<&mut R, C::ToRightError>

Attempts to get a mutable reference to the right value, converting if necessary.

If the right value is not available, attempts to convert the left value using the converter. Note: Obtaining a mutable reference will erase the left value.

Source

pub fn try_into_left(self) -> Result<L, C::ToLeftError>

Attempts to consume the pair and return the left value, converting if necessary.

If the left value is not available, attempts to convert the right value using the converter.

Source

pub fn try_into_right(self) -> Result<R, C::ToRightError>

Attempts to consume the pair and return the right value, converting if necessary.

If the right value is not available, attempts to convert the left value using the converter.

Source

pub fn try_extract_left(&mut self) -> Result<Option<L>, C::ToRightError>

Attempts to extract the left value, converting if necessary.

If the left value is the only value in the pair, attempts to convert it to a right value before clearing. Returns None if the left value doesn’t exist. Returns Err if conversion fails when needed.

Source

pub fn try_extract_right(&mut self) -> Result<Option<R>, C::ToLeftError>

Attempts to extract the right value, converting if necessary.

If the right value is the only value in the pair, attempts to convert it to a left value before clearing. Returns None if the right value doesn’t exist. Returns Err if conversion fails when needed.

Source§

impl<L, R, C> Pair<L, R, C>
where C: Converter<L, R, ToLeftError = Infallible>,

Source

pub fn left(&self) -> &L

Returns a reference to the left value, converting from right if necessary.

If the left value is not available, converts the right value using the converter. This method is only available when the conversion is infallible.

Source

pub fn left_mut(&mut self) -> &mut L

Returns a mutable reference to the left value, converting from right if necessary.

If the left value is not available, converts the right value using the converter. This method is only available when the conversion is infallible. Note: Obtaining a mutable reference will erase the right value.

Source

pub fn into_left(self) -> L

Consumes the pair and returns the left value, converting from right if necessary.

If the left value is not available, converts the right value using the converter. This method is only available when the conversion is infallible.

Source

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

Extracts and returns the right value if it exists.

If the right value is the only value in the pair, attempts to convert it to a left value before clearing. Returns None if the right value doesn’t exist. This method is only available when the conversion is infallible.

Source§

impl<L, R, C> Pair<L, R, C>
where C: Converter<L, R, ToRightError = Infallible>,

Source

pub fn right(&self) -> &R

Returns a reference to the right value, converting from left if necessary.

If the right value is not available, converts the left value using the converter. This method is only available when the conversion is infallible.

Source

pub fn right_mut(&mut self) -> &mut R

Returns a mutable reference to the right value, converting from left if necessary.

If the right value is not available, converts the left value using the converter. This method is only available when the conversion is infallible. Note: Obtaining a mutable reference will erase the left value.

Source

pub fn into_right(self) -> R

Consumes the pair and returns the right value, converting from left if necessary.

If the right value is not available, converts the left value using the converter. This method is only available when the conversion is infallible.

Source

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

Extracts and returns the left value if it exists.

If the left value is the only value in the pair, attempts to convert it to a right value before clearing. Returns None if the left value doesn’t exist. This method is only available when the conversion is infallible.

Trait Implementations§

Source§

impl<L: Clone, R: Clone, C: Clone> Clone for Pair<L, R, C>

Source§

fn clone(&self) -> Pair<L, R, C>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<L: Debug, R: Debug, C> Debug for Pair<L, R, C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<L, R, C> From<Pair<L, R, C>> for EitherOrBoth<L, R>

Source§

fn from(pair: Pair<L, R, C>) -> Self

Converts to this type from the input type.
Source§

impl<L: Hash, R: Hash, C> Hash for Pair<L, R, C>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<L: PartialEq, R: PartialEq, C> PartialEq for Pair<L, R, C>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<L, R, C = StdConverter> !Freeze for Pair<L, R, C>

§

impl<L, R, C = StdConverter> !RefUnwindSafe for Pair<L, R, C>

§

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

§

impl<L, R, C = StdConverter> !Sync for Pair<L, R, C>

§

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

§

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