EitherOrBoth

Enum EitherOrBoth 

Source
pub enum EitherOrBoth<L, R = L> {
    Both(L, R),
    Left(L),
    Right(R),
}
Expand description

Represent values that have either a Left or Right value or Both values

Variants§

§

Both(L, R)

Represents a value from both sides

§

Left(L)

Represents a value from the left side

§

Right(R)

Represents a value from the right side

Implementations§

Source§

impl<L, R> EitherOrBoth<L, R>

Source

pub const fn has_left(&self) -> bool

Returns true if EitherOrBoth is a Left or Both value.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.has_left(), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.has_left(), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.has_left(), false);
Source

pub fn has_left_and<F>(self, f: F) -> bool
where F: FnOnce(L) -> bool,

Returns true if this is a Left or Both and the left value satisfies a predicate.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.has_left_and(|l| l == 1), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.has_left_and(|l| l == 1), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.has_left_and(|l| l == 1), false);
Source

pub fn has_left_or<F>(self, f: F) -> bool
where F: FnOnce(R) -> bool,

Returns true if this is a Left or Both or the Right value satisfies a predicate.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.has_left_or(|r| r == 'c'), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.has_left_or(|r| r == 'c'), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.has_left_or(|r| r != 'c'), false);
Source

pub const fn has_right(&self) -> bool

Returns true if EitherOrBoth is a Right or Both value.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.has_right(), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.has_right(), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.has_right(), true);
Source

pub fn has_right_and<F>(self, f: F) -> bool
where F: FnOnce(R) -> bool,

Returns true if this is a Right or Both and the right value satisfies a predicate.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.has_right_and(|r| r == 'c'), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.has_right_and(|r| r == 'm'), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.has_right_and(|r| r == 'c'), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.has_right_and(|r| r == 'c'), true);
Source

pub fn has_right_or<F>(self, f: F) -> bool
where F: FnOnce(L) -> bool,

Returns true if this is a Right or Both or the Left value satisfies a predicate.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.has_right_or(|l| l == 1), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.has_right_or(|l| l == 1), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.has_right_or(|l| l == 2), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.has_right_or(|l| l == 2), true);
Source

pub const fn is_both(&self) -> bool

Returns true if EitherOrBoth is a Both value.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.is_both(), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.is_both(), false);
Source

pub fn is_both_and<F>(self, f: F) -> bool
where F: FnOnce(L, R) -> bool,

Returns true if this is a Both and both values satisfy a predicate.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.is_both_and(|l, r| l == 1 && r == 'c'), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.is_both_and(|l, r| l == 1 && r == 'c'), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.is_both_and(|l, _| l == 2), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.is_both_and(|_, r| r == 'c'), false);
Source

pub fn is_both_or<F, G>(self, f: F, g: G) -> bool
where F: FnOnce(L) -> bool, G: FnOnce(R) -> bool,

Returns true if this is a Both or if Left or Right match their respective predicate.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.is_both_or(|l| l == 2, |r| r == 'm'), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.is_both_or(|l| l == 1, |r| r == 'c'), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.is_both_or(|l| l == 2, |r| r == 'c'), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.is_both_or(|l| l == 1, |r| r == 'm'), false);
Source

pub const fn is_left(&self) -> bool

Returns true if EitherOrBoth is a Left value.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.is_left(), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.is_left(), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.is_left(), false);
Source

pub fn is_left_and<F>(self, f: F) -> bool
where F: FnOnce(L) -> bool,

Returns true if this is a Left and the inner value satisfies a predicate.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.is_left_and(|l| l == 1), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.is_left_and(|l| l == 1), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.is_left_and(|l| l == 1), false);
Source

pub fn is_left_or<F>(self, f: F) -> bool
where F: FnOnce(R) -> bool,

Returns true if this is a Left or the right value satisfies a predicate.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.is_left_or(|r| r == 'c'), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.is_left_or(|r| r == 'c'), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.is_left_or(|r| r == 'm'), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.is_left_or(|r| r == 'c'), true);
Source

pub const fn is_right(&self) -> bool

Returns true EitherOrBoth is a Right value.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.is_right(), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.is_right(), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.is_right(), false);
Source

pub fn is_right_and<F>(self, f: F) -> bool
where F: FnOnce(R) -> bool,

Returns true if this is a Right and the inner value satisfies a predicate.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.is_right_and(|r| r == 'c'), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.is_right_and(|r| r == 'm'), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.is_right_and(|r| r == 'c'), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.is_right_and(|r| r == 'c'), false);
Source

pub fn is_right_or<F>(self, f: F) -> bool
where F: FnOnce(L) -> bool,

Returns true if this is a Right or the left value satisfies a predicate.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.is_right_or(|l| l == 1), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.is_right_or(|l| l == 1), true);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.is_right_or(|l| l == 2), false);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.is_right_or(|l| l == 1), true);
Source

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

Converts from &EitherOrBoth<L, R> to EitherOrBoth<&L, &R>.

§Examples

Calculate the length of the strings without moving the Strings.

use either_or_both::EitherOrBoth;

let text: EitherOrBoth<String> = EitherOrBoth::Left("left value".to_owned());

// Cast `EitherOrBoth<String>` to `EitherOrBoth<&String>` and then apply a map consuming the
// result of `as_ref` instead of `text` itself
let length: EitherOrBoth<usize> = text.as_ref().map(String::len);
assert_eq!(length, EitherOrBoth::Left(10));

println!("`text` has not been moved: {:?}", &text);
Source

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

Converts from &mut EitherOrBoth<L, R> to EitherOrBoth<&mut L, &mut R>.

§Examples
use either_or_both::EitherOrBoth;

let mut both: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');

match both.as_mut() {
    EitherOrBoth::Both(left, right) => {
        *left = 2;
        *right = 'm';
    }
    EitherOrBoth::Left(left) => *left = 3,
    EitherOrBoth::Right(right) => *right = 'x',
}

assert_eq!(both, EitherOrBoth::Both(2, 'm'))
Source

pub fn as_deref( &self, ) -> EitherOrBoth<&<L as Deref>::Target, &<R as Deref>::Target>
where L: Deref, R: Deref,

Converts from EitherOrBoth<L, R> to EitherOrBoth<&L::Target, &R::Target>.

This method keeps the original EitherOrBoth unchanged, while creating a new instance that holds a reference to the original. It also coerces the inner values through the Deref trait.

use either_or_both::EitherOrBoth;

let values: EitherOrBoth<String> = EitherOrBoth::Both("left".to_owned(), "right".to_owned());
let deref: EitherOrBoth<&str> = values.as_deref();
assert_eq!(deref.both(), Some(("left", "right")));

let values: EitherOrBoth<String> = EitherOrBoth::Left("left".to_owned());
let deref: EitherOrBoth<&str> = values.as_deref();
assert_eq!(deref.left(), Some("left"));
Source

pub fn as_deref_mut( &mut self, ) -> EitherOrBoth<&mut <L as Deref>::Target, &mut <R as Deref>::Target>
where L: DerefMut, R: DerefMut,

Converts from EitherOrBoth<L, R> to EitherOrBoth<&mut L::Target, &mut R::Target>.

This method keeps the original EitherOrBoth unchanged, while creating a new instance that holds a mutable reference to the inner type’s Deref::Target type.

use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<String> = EitherOrBoth::Both("left".to_owned(), "right".to_owned());
let upper_case: EitherOrBoth<&mut str> = value.as_deref_mut().map(|v| {
    v.make_ascii_uppercase();
    v
});

assert_eq!(
    upper_case.both(),
    Some((
        "LEFT".to_owned().as_mut_str(),
        "RIGHT".to_owned().as_mut_str()
    ))
);
Source

pub fn as_pin_ref(self: Pin<&Self>) -> EitherOrBoth<Pin<&L>, Pin<&R>>

Converts from Pin<&EitherOrBoth<L, R>> to EitherOrBoth<Pin<&L>, Pin<&R>>.

Source

pub fn as_pin_mut( self: Pin<&mut Self>, ) -> EitherOrBoth<Pin<&mut L>, Pin<&mut R>>

Converts from Pin<&mut EitherOrBoth<L, R>> to EitherOrBoth<Pin<&mut L>, Pin<&mut R>>.

Source

pub fn expect_both(self, msg: &str) -> (L, R)

Returns the contained Both values as tuple consuming self.

§Panics

Panics with a custom panic message provided by msg if only a Left or Right value is present

§Examples
use either_or_both::EitherOrBoth;

let value = EitherOrBoth::Both("left", "right");
assert_eq!(value.expect_both("should be both"), ("left", "right"));

This example panics with the message should be both

use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
value.expect_both("should be both");
Source

pub fn expect_left(self, msg: &str) -> L

Returns the contained left value if Left or Both consuming self.

§Panics

Panics with a custom panic message provided by msg if there is no left value present

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
assert_eq!(value.expect_left("should be left"), "left");

let value: EitherOrBoth<&str> = EitherOrBoth::Both("left", "right");
assert_eq!(value.expect_left("should be left"), "left");

This example panics with the message should be left

use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Right("right");
value.expect_left("should be left");
Source

pub fn expect_only_left(self, msg: &str) -> L

Returns the contained Left value consuming self.

§Panics

Panics with a custom panic message provided by msg if EitherOrBoth is not Left

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
assert_eq!(value.expect_only_left("should be left"), "left");

The following examples panic with the message should be left

use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Both("left", "right");
value.expect_only_left("should be left");
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Right("right");
value.expect_only_left("should be left"); // panics with the message `should be left`
Source

pub fn expect_right(self, msg: &str) -> R

Returns the contained right value if Right or Both consuming self.

§Panics

Panics with a custom panic message provided by msg if there is no right value present

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Right("right");
assert_eq!(value.expect_right("should be right"), "right");

let value: EitherOrBoth<&str> = EitherOrBoth::Both("left", "right");
assert_eq!(value.expect_right("should be right"), "right");

The following example panics with the message should be right

use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
value.expect_right("should be right");
Source

pub fn expect_only_right(self, msg: &str) -> R

Returns the contained Right value consuming self.

§Panics

Panics with a custom panic message provided by msg if there is no Right value present

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Right("right");
assert_eq!(value.expect_only_right("should be right"), "right");

The following examples panic with the message should be right

use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Both("left", "right");
value.expect_only_right("should be right");
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
value.expect_only_right("should be right"); // panics with the message `should be right`
Source

pub fn unwrap_both(self) -> (L, R)

Returns the contained Both values as tuple consuming self.

§Panics

Panics if only a Left or Right value is present

§Examples
use either_or_both::EitherOrBoth;

let value = EitherOrBoth::Both("left", "right");
assert_eq!(value.unwrap_both(), ("left", "right"));
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
value.unwrap_both(); // panics
Source

pub unsafe fn unwrap_both_unchecked(self) -> (L, R)

Returns the contained Both values as tuple consuming self, without checking that the value is not Both.

§SAFETY

Calling this method on a Right or Left variant is undefined behavior.

§Examples
use either_or_both::EitherOrBoth;

let value = EitherOrBoth::Both("left", "right");
assert_eq!(unsafe { value.unwrap_both_unchecked() }, ("left", "right"));

The following example introduces undefined behavior

use either_or_both::EitherOrBoth;

let value = EitherOrBoth::Left("left");
assert_eq!(unsafe { value.unwrap_both_unchecked() }, ("left", "right"));
Source

pub fn unwrap_left(self) -> L

Returns the contained left value of a Left or Both variant consuming self.

§Panics

Panics if EitherOrBoth is a Right variant

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
assert_eq!(value.unwrap_left(), "left");

let value: EitherOrBoth<&str> = EitherOrBoth::Both("left", "right");
assert_eq!(value.unwrap_left(), "left");
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Right("right");
value.unwrap_left(); // panics
Source

pub unsafe fn unwrap_left_unchecked(self) -> L

Returns the contained left value of a Left or Both variant consuming self, without checking that the value is not Left or Both.

§SAFETY

Calling this method on a Right variant is undefined behavior.

§Examples
use either_or_both::EitherOrBoth;

let value = EitherOrBoth::Both("left", "right");
assert_eq!(unsafe { value.unwrap_left_unchecked() }, "left");

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
assert_eq!(unsafe { value.unwrap_left_unchecked() }, "left");

The following example introduces undefined behavior

use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Right("right");
assert_eq!(unsafe { value.unwrap_left_unchecked() }, "left");
Source

pub fn unwrap_only_left(self) -> L

Returns the contained Left value consuming self.

§Panics

Panics if EitherOrBoth is a Right or Both variant

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
assert_eq!(value.unwrap_only_left(), "left");

The following examples panic

use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Both("left", "right");
value.unwrap_only_left(); // panics
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Right("right");
value.unwrap_only_left(); // panics
Source

pub unsafe fn unwrap_only_left_unchecked(self) -> L

Returns the contained Left value consuming self, without checking that the value is not Left.

§SAFETY

Calling this method on a Right or Both variant is undefined behavior.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
assert_eq!(unsafe { value.unwrap_only_left_unchecked() }, "left");

The following example introduces undefined behavior

use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Right("right");
assert_eq!(unsafe { value.unwrap_only_left_unchecked() }, "left");
Source

pub fn unwrap_right(self) -> R

Returns the contained right value of a Right or Both variant consuming self.

§Panics

Panics if EitherOrBoth is a Left variant

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Right("right");
assert_eq!(value.unwrap_right(), "right");

let value: EitherOrBoth<&str> = EitherOrBoth::Both("left", "right");
assert_eq!(value.unwrap_right(), "right");
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
value.unwrap_right(); // panics
Source

pub unsafe fn unwrap_right_unchecked(self) -> R

Returns the contained right value of a Right or Both variant consuming self, without checking that the value is not Right or Both.

§SAFETY

Calling this method on a Left variant is undefined behavior.

§Examples
use either_or_both::EitherOrBoth;

let value = EitherOrBoth::Both("left", "right");
assert_eq!(unsafe { value.unwrap_right_unchecked() }, "right");

let value: EitherOrBoth<&str> = EitherOrBoth::Right("right");
assert_eq!(unsafe { value.unwrap_right_unchecked() }, "right");

The following example introduces undefined behavior

use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
assert_eq!(unsafe { value.unwrap_right_unchecked() }, "right");
Source

pub fn unwrap_only_right(self) -> R

Returns the contained Right value consuming self.

§Panics

Panics if EitherOrBoth is a Left or Both variant

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Right("right");
assert_eq!(value.unwrap_only_right(), "right");

The following examples panic

use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Both("left", "right");
value.unwrap_only_right(); // panics
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
value.unwrap_only_right(); // panics
Source

pub unsafe fn unwrap_only_right_unchecked(self) -> R

Returns the contained Right value consuming self, without checking that the value is not Right.

§SAFETY

Calling this method on a Left or Both variant is undefined behavior.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Right("right");
assert_eq!(unsafe { value.unwrap_only_right_unchecked() }, "right");

The following example introduces undefined behavior

use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str> = EitherOrBoth::Left("left");
assert_eq!(unsafe { value.unwrap_only_right_unchecked() }, "right");
Source

pub fn both(self) -> Option<(L, R)>

If both values are present, return Some containing the values otherwise return None.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.both(), Some((1, 'c')));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.both(), None);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.both(), None);
Source

pub fn both_and(self, other: Self) -> Self

Returns Right or Left if the EitherOrBoth is not Both otherwise returns other.

The both_and combinator eagerly evaluates its arguments, which can result in unnecessary computations. When chaining operations that involve function calls, use both_and_then instead. It evaluates the function lazily.

§Examples
use either_or_both::EitherOrBoth;

let x: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let y: EitherOrBoth<u8, char> = EitherOrBoth::Left(2);
assert_eq!(x.both_and(y), EitherOrBoth::Left(2));

let x: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let y: EitherOrBoth<u8, char> = EitherOrBoth::Left(2);
assert_eq!(x.both_and(y), EitherOrBoth::Left(1));

let x: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let y: EitherOrBoth<u8, char> = EitherOrBoth::Left(2);
assert_eq!(x.both_and(y), EitherOrBoth::Right('c'));
Source

pub fn both_and_then<F>(self, f: F) -> Self
where F: FnOnce(L, R) -> Self,

Returns Right or Left if the EitherOrBoth is not Both otherwise calls f with both values and returns the result.

§Examples
use either_or_both::EitherOrBoth;

fn apply_to_both(left: u8, right: char) -> EitherOrBoth<u8, char> {
    EitherOrBoth::Both(left + 1, right.max('m'))
}

let x: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(x.both_and_then(apply_to_both), EitherOrBoth::Both(2, 'm'));

let x: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(x.both_and_then(apply_to_both), EitherOrBoth::Left(1));

let x: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(x.both_and_then(apply_to_both), EitherOrBoth::Right('c'));
Source

pub fn left(self) -> Option<L>

If a left value is present, return Some containing the value otherwise return None.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.left(), Some(1));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.left(), Some(1));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.left(), None);
Source

pub fn left_and<T>(self, other: EitherOrBoth<T, R>) -> EitherOrBoth<T, R>

Returns Right if the EitherOrBoth is Right otherwise returns other.

The left_and combinator eagerly evaluates its arguments, which can result in unnecessary computations. When chaining operations that involve function calls, use left_and_then instead. It evaluates the function lazily.

§Examples
use either_or_both::EitherOrBoth;

let x: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let y: EitherOrBoth<&str, char> = EitherOrBoth::Left("left");
assert_eq!(x.left_and(y), EitherOrBoth::Left("left"));

let x: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let y: EitherOrBoth<&str, char> = EitherOrBoth::Left("left");
assert_eq!(x.left_and(y), EitherOrBoth::Left("left"));

let x: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let y: EitherOrBoth<&str, char> = EitherOrBoth::Left("left");
assert_eq!(x.left_and(y), EitherOrBoth::Right('c'));
Source

pub fn left_and_then<F, T>(self, f: F) -> EitherOrBoth<T, R>
where F: FnOnce(L) -> EitherOrBoth<T, R>,

Returns Right otherwise calls f with the left value and returns the result.

§Examples
use either_or_both::EitherOrBoth;

fn left_to_string(x: u8) -> EitherOrBoth<String, char> {
    EitherOrBoth::Left(x.to_string())
}

let x: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(
    x.left_and_then(left_to_string),
    EitherOrBoth::Left(1.to_string())
);

let x: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(
    x.left_and_then(left_to_string),
    EitherOrBoth::Left(1.to_string())
);

let x: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(x.left_and_then(left_to_string), EitherOrBoth::Right('c'));
Source

pub fn only_left(self) -> Option<L>

Returns Some with the Left value if present otherwise None.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.only_left(), Some(1));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.only_left(), None);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.only_left(), None);
Source

pub fn right(self) -> Option<R>

If a left value is present, return Some containing the value otherwise return None.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.right(), Some('c'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.right(), Some('c'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.right(), None);
Source

pub fn right_and<T>(self, other: EitherOrBoth<L, T>) -> EitherOrBoth<L, T>

Returns Left if the EitherOrBoth is Left otherwise returns other.

The right_and combinator eagerly evaluates its arguments, which can result in unnecessary computations. When chaining operations that involve function calls, use right_and_then instead. It evaluates the function lazily.

§Examples
use either_or_both::EitherOrBoth;

let x: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let y: EitherOrBoth<u8, &str> = EitherOrBoth::Right("right");
assert_eq!(x.right_and(y), EitherOrBoth::Right("right"));

let x: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let y: EitherOrBoth<u8, &str> = EitherOrBoth::Right("right");
assert_eq!(x.right_and(y), EitherOrBoth::Right("right"));

let x: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let y: EitherOrBoth<u8, &str> = EitherOrBoth::Right("right");
assert_eq!(x.right_and(y), EitherOrBoth::Left(1));
Source

pub fn right_and_then<F, T>(self, f: F) -> EitherOrBoth<L, T>
where F: FnOnce(R) -> EitherOrBoth<L, T>,

Returns Left otherwise calls f with the right value and returns the result.

§Examples
use either_or_both::EitherOrBoth;

fn right_to_string(x: char) -> EitherOrBoth<u8, String> {
    EitherOrBoth::Right(x.to_string())
}

let x: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(
    x.right_and_then(right_to_string),
    EitherOrBoth::Right('c'.to_string())
);

let x: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(
    x.right_and_then(right_to_string),
    EitherOrBoth::Right('c'.to_string())
);

let x: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(x.right_and_then(right_to_string), EitherOrBoth::Left(1));
Source

pub fn only_right(self) -> Option<R>

Returns Some with the Right value if present otherwise None.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.only_right(), Some('c'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.only_right(), None);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.only_right(), None);
Source

pub fn unzip(self) -> (Option<L>, Option<R>)

Unzips an EitherOrBoth into a tuple of Options.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.unzip(), (Some(1), Some('c')));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.unzip(), (Some(1), None));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.unzip(), (None, Some('c')));
Source

pub fn into_iter_swap( self, ) -> SwapIterEitherOrBoth<<L as IntoIterator>::IntoIter, <R as IntoIterator>::IntoIter>

Consumes the inner iterators and returns an iterator that yields items of type EitherOrBoth<L, R>, combining the iterators.

This iterator allows traversing inner iterators with different types

§Examples
use either_or_both::EitherOrBoth;

let x = EitherOrBoth::Both(vec![1], vec!['c', 'm']);
let mut iter = x.into_iter_swap(); // moves `x` and the iterators

assert_eq!(iter.next(), Some(EitherOrBoth::Both(1, 'c')));
assert_eq!(iter.next(), Some(EitherOrBoth::Right('m')));
assert_eq!(iter.next(), None);
Source

pub fn iter_swap( &self, ) -> SwapIterEitherOrBoth<<&L as IntoIterator>::IntoIter, <&R as IntoIterator>::IntoIter>
where for<'a> &'a L: IntoIterator, for<'a> &'a R: IntoIterator,

Borrow the inner iterators and returns an iterator that yields items of type EitherOrBoth<&L, &R>, combining the iterators.

This iterator allows traversing inner iterators with different types

§Examples
use either_or_both::EitherOrBoth;

let x = EitherOrBoth::Both(vec![1], vec!['c', 'm']);
let mut iter = x.iter_swap();

assert_eq!(iter.next(), Some(EitherOrBoth::Both(&1, &'c')));
assert_eq!(iter.next(), Some(EitherOrBoth::Right(&'m')));
assert_eq!(iter.next(), None);

println!("{x:?}"); // still can access `x`
Source

pub fn iter_swap_mut( &mut self, ) -> SwapIterEitherOrBoth<<&mut L as IntoIterator>::IntoIter, <&mut R as IntoIterator>::IntoIter>
where for<'a> &'a mut L: IntoIterator, for<'a> &'a mut R: IntoIterator,

Mutably borrows the inner iterators returning an iterator that yields items of type EitherOrBoth<&mut L, &mut R>, combining the iterators.

This iterator allows traversing inner iterators with different types

§Examples
use either_or_both::EitherOrBoth;

let mut x = EitherOrBoth::Both(vec![1], vec!['c', 'm']);
let mut iter = x.iter_swap_mut();

assert_eq!(iter.next(), Some(EitherOrBoth::Both(&mut 1, &mut 'c')));
assert_eq!(iter.next(), Some(EitherOrBoth::Right(&mut 'm')));
assert_eq!(iter.next(), None);

println!("{x:?}"); // still can access `x`
Source

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

Converts EitherOrBoth<L, R> to EitherOrBoth<R, L>.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.flip(), EitherOrBoth::Both('c', 1));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.flip(), EitherOrBoth::Right(1));
Source

pub fn bimap<F, G, T, U>(self, f: F, g: G) -> EitherOrBoth<T, U>
where F: FnOnce(L) -> T, G: FnOnce(R) -> U,

Applies mapping functions to the left and right values returning an EitherOrBoth<T, U>.

§Examples
use either_or_both::EitherOrBoth;

let map_left = |left: u8| left.to_string();
let map_right = |right: char| right as i32;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(
    value.bimap(map_left, map_right),
    EitherOrBoth::Both("1".to_owned(), 99i32)
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(
    value.bimap(map_left, map_right),
    EitherOrBoth::Left("1".to_owned())
);
Source

pub fn map_left<F, T>(self, f: F) -> EitherOrBoth<T, R>
where F: FnOnce(L) -> T,

Applies a mapping function to the left value of Both or Left returning an EitherOrBoth<T, R>.

§Examples
use either_or_both::EitherOrBoth;

let map_left = |left: u8| left.to_string();

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(
    value.map_left(map_left),
    EitherOrBoth::Both("1".to_owned(), 'c')
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.map_left(map_left), EitherOrBoth::Left("1".to_owned()));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.map_left(map_left), EitherOrBoth::Right('c'));
Source

pub fn map_left_or<F, T>(self, default: T, f: F) -> T
where F: FnOnce(L) -> T,

Returns the provided default value if this is a Right or applies a mapping function to the contained left value.

The map_left_or combinator eagerly evaluates its arguments, which can result in unnecessary computations. When chaining operations that involve function calls, use map_left_or_else instead. It evaluates the function lazily.

§Examples
use either_or_both::EitherOrBoth;

let map_left = |left: u8| left as u64 + 1;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.map_left_or(42, map_left), 42u64);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.map_left_or(42, map_left), 2u64);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.map_left_or(42, map_left), 2u64);
Source

pub fn map_left_or_default<T, F>(self, f: F) -> T
where T: Default, F: FnOnce(L) -> T,

Applies the given function to the left value of Left or Both, mapping L to T otherwise returns the default value for type T.

§Examples
use either_or_both::EitherOrBoth;

let map_left = |left: u8| left as u64 + 1;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.map_left_or_default(map_left), 0u64);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.map_left_or_default(map_left), 2u64);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.map_left_or_default(map_left), 2u64);
Source

pub fn map_left_or_else<D, F, T>(self, default: D, f: F) -> T
where D: FnOnce() -> T, F: FnOnce(L) -> T,

Applies the given function to the left value of Left or Both, mapping L to T otherwise applies a different function.

§Examples
use either_or_both::EitherOrBoth;

let map_left = |left: u8| left.to_string();

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(
    value.map_left_or_else(|| String::from("left"), map_left),
    String::from("left")
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(
    value.map_left_or_else(|| String::from("left"), map_left),
    1.to_string()
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(
    value.map_left_or_else(|| String::from("left"), map_left),
    1.to_string()
);
Source

pub fn map_right<T>(self, f: fn(R) -> T) -> EitherOrBoth<L, T>

Applies a mapping function to the right value of Both or Right returning an EitherOrBoth<L, T>.

§Examples
use either_or_both::EitherOrBoth;

let map_right = |right: char| right.to_string();

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(
    value.map_right(map_right),
    EitherOrBoth::Both(1, "c".to_owned())
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(
    value.map_right(map_right),
    EitherOrBoth::Right("c".to_owned())
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.map_right(map_right), EitherOrBoth::Left(1));
Source

pub fn map_right_or<U, F>(self, default: U, f: F) -> U
where F: FnOnce(R) -> U,

Returns the provided default value if this is a Left or applies a mapping function to the contained right value.

The map_right_or combinator eagerly evaluates its arguments, which can result in unnecessary computations. When chaining operations that involve function calls, use map_right_or_else instead. It evaluates the function lazily.

§Examples
use either_or_both::EitherOrBoth;

let map_right = |right: char| right as i32;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.map_right_or(42, map_right), 99i32);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.map_right_or(42, map_right), 42i32);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.map_right_or(42, map_right), 99i32);
Source

pub fn map_right_or_default<T, F>(self, f: F) -> T
where T: Default, F: FnOnce(R) -> T,

Applies the given function to the right value of Right or Both, mapping R to T otherwise returns the default value for type T.

§Examples
use either_or_both::EitherOrBoth;

let map_right = |right: char| right as u64 + 1;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.map_right_or_default(map_right), 100u64);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.map_right_or_default(map_right), 0u64);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.map_right_or_default(map_right), 100u64);
Source

pub fn map_right_or_else<U, D, F>(self, default: D, f: F) -> U
where D: FnOnce() -> U, F: FnOnce(R) -> U,

Applies the given function to the right value of Right or Both, mapping R to T otherwise applies a different function.

§Examples
use either_or_both::EitherOrBoth;

let map_right = |right: char| right.to_string();

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(
    value.map_right_or_else(|| String::from("right"), map_right),
    "c".to_owned()
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(
    value.map_right_or_else(|| String::from("right"), map_right),
    "c".to_owned()
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(
    value.map_right_or_else(|| String::from("right"), map_right),
    String::from("right")
);
Source

pub fn biinspect<F, G>(self, f: F, g: G) -> Self
where for<'a> F: Fn(&'a L), for<'a> G: Fn(&'a R),

Calls functions with a reference to the contained values returning the original EitherOrBoth.

The evaluation order is from left to right if this is a Both variant. To reverse the order use flip.

§Examples
use either_or_both::EitherOrBoth;

// Prints a single line with "Left is: 1"
let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let left = value
    .biinspect(|l| println!("Left is: {l}"), |r| println!("Right is: {r}"))
    .expect_left("should be a left value");
assert_eq!(left, 1);

// Prints two lines:
// Left is: 1
// Right is: c
let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let both = value
    .biinspect(|l| println!("Left is: {l}"), |r| println!("Right is: {r}"))
    .expect_both("both values should be present");

assert_eq!(both, (1, 'c'));
Source

pub fn inspect_left<F>(self, f: F) -> Self
where for<'a> F: Fn(&'a L),

Calls a function with a reference to the contained left value if this is a Left or Both returning the original EitherOrBoth.

§Examples
use either_or_both::EitherOrBoth;

// Prints a single line with "Left is: 1"
let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let left = value
    .inspect_left(|l| println!("Left is: {l}"))
    .expect_left("should be a left value");
assert_eq!(left, 1);

// Prints nothing
let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let right = value
    .inspect_left(|l| println!("Left is: {l}"))
    .expect_right("should be a right value");

assert_eq!(right, 'c');
Source

pub fn inspect_right<F>(self, f: F) -> Self
where for<'a> F: Fn(&'a R),

Calls a function with a reference to the contained right value if this is a Right or Both returning the original EitherOrBoth.

§Examples
use either_or_both::EitherOrBoth;

// Prints a single line with "Right is: c"
let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let right = value
    .inspect_right(|r| println!("Right is: {r}"))
    .expect_right("should be a right value");
assert_eq!(right, 'c');

// Prints nothing
let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let left = value
    .inspect_right(|r| println!("Right is: {r}"))
    .expect_left("should be a left value");

assert_eq!(left, 1);
Source

pub fn biapply<F, G>(self, f: F, g: G)
where F: FnMut(L), G: FnMut(R),

Consumes this EitherOrBoth applying functions to the contained values taking mutable references to capture variables.

The evaluation order is from left to right if this is a Both variant. To reverse the order use flip.

§Examples
use either_or_both::EitherOrBoth;

let mut left = vec![];
let mut right = vec![];

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
value.biapply(|l| left.push(l), |r| right.push(r)); // moves `value`

assert_eq!(left, vec![1]);
assert_eq!(right, vec!['c']);

The following example will not compile with the error: “cannot borrow both as mutable more than once at a time”.

If you need to apply a function that requires mutable access to both elements simultaneously, consider using biapply_with instead.

use either_or_both::EitherOrBoth;

let mut both = vec![];

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
value.biapply(|l| both.push(l), |r| both.push(r as u8));
Source

pub fn biapply_with<F, G, Acc>(self, acc: &mut Acc, f: F, g: G)
where for<'a> F: FnMut(&'a mut Acc, L), for<'a> G: FnMut(&'a mut Acc, R),

Consumes this EitherOrBoth applying functions to the contained values and a given accumulator.

The evaluation order is from left to right if this is a Both variant. To reverse the order use flip.

§Examples
use either_or_both::EitherOrBoth;

let mut both = vec![];

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
value.biapply_with(&mut both, |acc, l| acc.push(l), |acc, r| acc.push(r as u8));

assert_eq!(both, vec![1, 99]);
Source

pub fn apply_left<F>(self, f: F)
where F: FnMut(L),

Consumes this EitherOrBoth applying a function to the contained left value if this is a Left or Both value.

§Examples
use either_or_both::EitherOrBoth;

let mut left = vec![];
let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
value.apply_left(|l| left.push(l));
assert_eq!(left, vec![1]);

let mut left = vec![];
let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
value.apply_left(|l| left.push(l));
assert_eq!(left.is_empty(), true);
Source

pub fn apply_right<F>(self, f: F)
where F: FnMut(R),

Consumes this EitherOrBoth applying a function to the contained right value if this is a Right or Both value.

§Examples
use either_or_both::EitherOrBoth;

let mut right = vec![];
let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
value.apply_right(|r| right.push(r));
assert_eq!(right, vec!['c']);

let mut right = vec![];
let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
value.apply_right(|r| right.push(r));
assert_eq!(right.is_empty(), true);
Source

pub fn reduce_left<F>(self, f: F) -> L
where F: FnOnce(R) -> L,

Returns the left value otherwise applies a function to a Right variant, converting it into an L value.

§Example
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.reduce_left(|r| r as u8), 1);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.reduce_left(|r| r as u8), 99);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.reduce_left(|r| r as u8), 1);
Source

pub fn reduce_map_left<F, G, T>(self, f: F, g: G) -> T
where F: FnOnce(L) -> T, G: FnOnce(R) -> T,

Like reduce_left but with mapping functions which convert the L and R values to a uniform type.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(
    value.reduce_map_left(|l| l.to_string(), |r| r.to_string()),
    "1".to_owned()
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(
    value.reduce_map_left(|l| l.to_string(), |r| r.to_string()),
    "1".to_owned()
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(
    value.reduce_map_left(|l| l.to_string(), |r| r.to_string()),
    "c".to_owned()
);
Source

pub fn reduce_right<F>(self, f: F) -> R
where F: FnOnce(L) -> R,

Returns the right value otherwise applies a function to a Left variant, converting it into an R value.

§Example
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(0, 'c');
assert_eq!(value.reduce_right(|l| l as char), 'c');

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.reduce_right(|l| l as char), 'c');

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(0);
assert_eq!(value.reduce_right(|l| l as char), '\0');
Source

pub fn reduce_map_right<F, G, T>(self, f: F, g: G) -> T
where F: FnOnce(L) -> T, G: FnOnce(R) -> T,

Like reduce_right but with mapping functions which convert the L and R values to a uniform type.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(
    value.reduce_map_right(|l| l.to_string(), |r| r.to_string()),
    "c".to_owned()
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(
    value.reduce_map_right(|l| l.to_string(), |r| r.to_string()),
    "c".to_owned()
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(
    value.reduce_map_right(|l| l.to_string(), |r| r.to_string()),
    "1".to_owned()
);
Source

pub fn ok(self) -> Result<R, L>

Transforms the EitherOrBoth<L, R> into a Result<R, L>.

Following the convention, the left value represents an error and a right value represents a correct value. Also, the evaluation of error values takes precedence if Both values are present.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<&str, u8> = EitherOrBoth::Right(1);
assert_eq!(value.ok(), Ok(1));

let value: EitherOrBoth<&str, u8> = EitherOrBoth::Both("this is an error", 1);
assert_eq!(value.ok(), Err("this is an error"));

let value: EitherOrBoth<&str, u8> = EitherOrBoth::Left("this is an error");
assert_eq!(value.ok(), Err("this is an error"));
Source

pub fn ok_or<E>(self, error: E) -> Result<R, E>

Transforms the EitherOrBoth<L, R> into a Result<R, L> using the provided error as error value.

Following the convention, the left value represents an error and a right value represents a correct value. Also, the evaluation of the error value takes precedence if Both values are present.

The ok_or combinator eagerly evaluates its arguments, which can result in unnecessary computations. When chaining operations that involve function calls, use ok_or_else instead. It evaluates the function lazily.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.ok_or("error message"), Ok('c'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.ok_or("error message"), Err("error message"));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.ok_or("error message"), Err("error message"));
Source

pub fn ok_or_else<F, E>(self, error: F) -> Result<R, E>
where F: FnOnce() -> E,

Transforms the EitherOrBoth<L, R> into a Result<R, L> using the result of an error function as error value.

Following the convention, the left value represents an error and a right value represents a correct value. Also, the evaluation of the error value takes precedence if Both values are present.

The ok_or combinator eagerly evaluates its arguments, which can result in unnecessary computations. When chaining operations that involve function calls, use ok_or_else instead. It evaluates the function lazily.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.ok_or_else(|| String::from("error message")), Ok('c'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(
    value.ok_or_else(|| String::from("error message")),
    Err(String::from("error message"))
);

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(
    value.ok_or_else(|| String::from("error message")),
    Err(String::from("error message"))
);
Source

pub fn or(self, left: L, right: R) -> (L, R)

Returns a tuple (L, R) with the provided values filling in any missing left or right value.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.or(2, 'm'), (1, 'c'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.or(2, 'm'), (1, 'm'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.or(2, 'm'), (2, 'c'));
Source

pub fn or_default(self) -> (L, R)
where L: Default, R: Default,

Returns a tuple (L, R) where any missing left or right value is replaced with its respective default value.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.or_default(), (1, 'c'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.or_default(), (1, '\0'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.or_default(), (0, 'c'));
Source

pub fn or_else<F, G>(self, f: F, g: G) -> (L, R)
where F: FnOnce() -> L, G: FnOnce() -> R,

Returns a tuple (L, R) where any missing left or right value is computed with the given functions.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.or_else(|| 1 + 1, || char::from(100)), (1, 'c'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.or_else(|| 1 + 1, || char::from(100)), (1, 'd'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.or_else(|| 1 + 1, || char::from(100)), (2, 'c'));
Source

pub fn into_left<F>(self, f: F) -> Self
where F: FnOnce(R) -> L,

Converts into a Left variant, using the contained left value or applying a mapping function to the Right value.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
assert_eq!(value.into_left(|r| r as u8), EitherOrBoth::Left(1));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
assert_eq!(value.into_left(|r| r as u8), EitherOrBoth::Left(1));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.into_left(|r| r as u8), EitherOrBoth::Left(99));
Source

pub fn into_right<F>(self, f: F) -> Self
where F: FnOnce(L) -> R,

Converts into a Right variant, using the contained right value or applying a mapping function to the Left value.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(0, 'c');
assert_eq!(value.into_right(|l| l as char), EitherOrBoth::Right('c'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
assert_eq!(value.into_right(|l| l as char), EitherOrBoth::Right('c'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(0);
assert_eq!(value.into_right(|l| l as char), EitherOrBoth::Right('\0'));
Source

pub fn insert_left(&mut self, left: L) -> &mut L

Inserts left into the EitherOrBoth, then returns a mutable reference to it

The Right variant transforms into a Both variant.

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let x = value.insert_left(2);
assert_eq!(*x, 2);
assert_eq!(value, EitherOrBoth::Both(2, 'c'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let x = value.insert_left(2);
assert_eq!(*x, 2);
assert_eq!(value, EitherOrBoth::Both(2, 'c'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let x = value.insert_left(2);
assert_eq!(*x, 2);
assert_eq!(value, EitherOrBoth::Left(2));
Source

pub fn insert_right(&mut self, right: R) -> &mut R

Inserts right into the EitherOrBoth, then returns a mutable reference to it

The Left variant transforms into a Both variant.

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let x = value.insert_right('m');
assert_eq!(*x, 'm');
assert_eq!(value, EitherOrBoth::Both(1, 'm'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let x = value.insert_right('m');
assert_eq!(*x, 'm');
assert_eq!(value, EitherOrBoth::Both(1, 'm'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let x = value.insert_right('m');
assert_eq!(*x, 'm');
assert_eq!(value, EitherOrBoth::Right('m'));
Source

pub fn left_or_insert(&mut self, value: L) -> &mut L

Returns a mutable reference to the contained left value if present; otherwise, inserts value into the Right variant and returns a mutable reference to it.

The left_or_insert combinator eagerly evaluates its arguments, which can result in unnecessary computations. When chaining operations that involve function calls, use left_or_insert_with instead. It evaluates the function lazily.

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let x: &mut u8 = value.left_or_insert(2);
assert_eq!(*x, 1);
assert_eq!(value, EitherOrBoth::Both(1, 'c'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let x: &mut u8 = value.left_or_insert(2);
assert_eq!(*x, 1);
assert_eq!(value, EitherOrBoth::Left(1));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let x: &mut u8 = value.left_or_insert(2);
assert_eq!(*x, 2);
assert_eq!(value, EitherOrBoth::Both(2, 'c'));
Source

pub fn left_or_insert_default(&mut self) -> &mut L
where L: Default,

Like left_or_insert but inserts the default L value into the Right variant and returns a mutable reference to it.

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let x: &mut u8 = value.left_or_insert_default();
assert_eq!(*x, 1);
assert_eq!(value, EitherOrBoth::Both(1, 'c'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let x: &mut u8 = value.left_or_insert_default();
assert_eq!(*x, 1);
assert_eq!(value, EitherOrBoth::Left(1));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let x: &mut u8 = value.left_or_insert_default();
assert_eq!(*x, 0);
assert_eq!(value, EitherOrBoth::Both(0, 'c'));
Source

pub fn left_or_insert_with<F>(&mut self, f: F) -> &mut L
where F: FnOnce() -> L,

Like left_or_insert but computes a left value from a given function inserting into the Right variant and returning a mutable reference to it.

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let x: &mut u8 = value.left_or_insert_with(|| 1 + 1);
assert_eq!(*x, 1);
assert_eq!(value, EitherOrBoth::Both(1, 'c'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let x: &mut u8 = value.left_or_insert_with(|| 1 + 1);
assert_eq!(*x, 1);
assert_eq!(value, EitherOrBoth::Left(1));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let x: &mut u8 = value.left_or_insert_with(|| 1 + 1);
assert_eq!(*x, 2);
assert_eq!(value, EitherOrBoth::Both(2, 'c'));
Source

pub fn right_or_insert(&mut self, value: R) -> &mut R

Returns a mutable reference to the contained right value if present; otherwise, inserts value into the Left variant and returns a mutable reference to it.

The right_or_insert combinator eagerly evaluates its arguments, which can result in unnecessary computations. When chaining operations that involve function calls, use right_or_insert_with instead. It evaluates the function lazily.

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let x: &mut char = value.right_or_insert('m');
assert_eq!(*x, 'c');
assert_eq!(value, EitherOrBoth::Both(1, 'c'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let x: &mut char = value.right_or_insert('m');
assert_eq!(*x, 'm');
assert_eq!(value, EitherOrBoth::Both(1, 'm'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let x: &mut char = value.right_or_insert('m');
assert_eq!(*x, 'c');
assert_eq!(value, EitherOrBoth::Right('c'));
Source

pub fn right_or_insert_default(&mut self) -> &mut R
where R: Default,

Like right_or_insert but inserts the default R value into the Left variant and returns a mutable reference to it.

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let x: &mut char = value.right_or_insert_default();
assert_eq!(*x, 'c');
assert_eq!(value, EitherOrBoth::Both(1, 'c'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let x: &mut char = value.right_or_insert_default();
assert_eq!(*x, '\0');
assert_eq!(value, EitherOrBoth::Both(1, '\0'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let x: &mut char = value.right_or_insert_default();
assert_eq!(*x, 'c');
assert_eq!(value, EitherOrBoth::Right('c'));
Source

pub fn right_or_insert_with<F>(&mut self, f: F) -> &mut R
where F: FnOnce() -> R,

Like right_or_insert but computes a right value from a given function inserting into the Left variant and returning a mutable reference to it.

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let x: &mut char = value.right_or_insert_with(|| char::from(109));
assert_eq!(*x, 'c');
assert_eq!(value, EitherOrBoth::Both(1, 'c'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let x: &mut char = value.right_or_insert_with(|| char::from(109));
assert_eq!(*x, 'm');
assert_eq!(value, EitherOrBoth::Both(1, 'm'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let x: &mut char = value.right_or_insert_with(|| char::from(109));
assert_eq!(*x, 'c');
assert_eq!(value, EitherOrBoth::Right('c'));
Source

pub fn replace_any(&mut self, left: L, right: R) -> Self

Replaces any left and right values returning the original EitherOrBoth

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let old = value.replace_any(2, 'm');
assert_eq!(old, EitherOrBoth::Both(1, 'c'));
assert_eq!(value, EitherOrBoth::Both(2, 'm'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let old = value.replace_any(2, 'm');
assert_eq!(old, EitherOrBoth::Left(1));
assert_eq!(value, EitherOrBoth::Left(2));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let old = value.replace_any(2, 'm');
assert_eq!(old, EitherOrBoth::Right('c'));
assert_eq!(value, EitherOrBoth::Right('m'));
Source

pub fn replace_left(&mut self, value: L) -> Option<L>

Replaces the left value returning the old value if present

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let old = value.replace_left(2);
assert_eq!(old, Some(1));
assert_eq!(value, EitherOrBoth::Both(2, 'c'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let old = value.replace_left(2);
assert_eq!(old, Some(1));
assert_eq!(value, EitherOrBoth::Left(2));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let old = value.replace_left(2);
assert_eq!(old, None);
assert_eq!(value, EitherOrBoth::Right('c'));
Source

pub fn replace_right(&mut self, value: R) -> Option<R>

Replaces the right value returning the old value if present

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let old = value.replace_right('m');
assert_eq!(old, Some('c'));
assert_eq!(value, EitherOrBoth::Both(1, 'm'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Right('c');
let old = value.replace_right('m');
assert_eq!(old, Some('c'));
assert_eq!(value, EitherOrBoth::Right('m'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let old = value.replace_right('m');
assert_eq!(old, None);
assert_eq!(value, EitherOrBoth::Left(1));
Source§

impl<T> EitherOrBoth<T, T>

Source

pub fn apply<F>(self, f: F)
where F: FnMut(T),

Consumes this EitherOrBoth applying a function to the contained values (of a uniform type) taking mutable references to capture variables.

The evaluation order is from left to right if this is a Both variant. To reverse the order use flip.

§Examples
use either_or_both::EitherOrBoth;

let mut both = vec![];

let value: EitherOrBoth<char> = EitherOrBoth::Both('c', 'a');
value.apply(|c| both.push(c));

assert_eq!(both, vec!['c', 'a']);
Source

pub fn inspect<F>(self, f: F) -> Self
where for<'a> F: Fn(&'a T),

Calls a function with a reference to the contained values (of a uniform type) returning the original EitherOrBoth.

The evaluation order is from left to right if this is a Both variant. To reverse the order use flip.

§Examples
use either_or_both::EitherOrBoth;

// Prints a single line with "The value is: c"
let value: EitherOrBoth<char> = EitherOrBoth::Left('c');
let left = value
    .inspect(|c| println!("The value is: {c}"))
    .expect_left("should be a left value");
assert_eq!(left, 'c');

// Prints two lines:
// The value is: c
// The value is: a
let value: EitherOrBoth<char> = EitherOrBoth::Both('c', 'a');
let both = value
    .inspect(|c| println!("The value is: {c}"))
    .expect_both("both values should be present");

assert_eq!(both, ('c', 'a'));
Source

pub fn map<F, U>(self, f: F) -> EitherOrBoth<U, U>
where F: Fn(T) -> U,

Applies a mapping function to the left and right values (of a uniform type) returning an EitherOrBoth<U, U>.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<char> = EitherOrBoth::Both('c', 'a');
assert_eq!(
    value.map(|c| c.to_string()),
    EitherOrBoth::Both("c".to_owned(), "a".to_owned())
);

let value: EitherOrBoth<char> = EitherOrBoth::Left('c');
assert_eq!(
    value.map(|c| c.to_string()),
    EitherOrBoth::Left("c".to_owned())
);
Source

pub fn reduce<F>(self, f: F) -> T
where F: FnOnce(T, T) -> T,

Returns the contained Left or Right value otherwise applies a function, converting Both into an single value.

§Example
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8> = EitherOrBoth::Both(3, 1);
assert_eq!(value.reduce(|l, r| l + r), 4);

let value: EitherOrBoth<u8> = EitherOrBoth::Right(2);
assert_eq!(value.reduce(|l, r| l + r), 2);

let value: EitherOrBoth<u8> = EitherOrBoth::Left(3);
assert_eq!(value.reduce(|l, r| l + r), 3);
Source

pub fn iter(&self) -> IterEitherOrBoth<'_, T>

Returns an iterator over the contained values of a uniform type

The evaluation order is from left to right if this is a Both variant. To reverse the order use flip.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<char> = EitherOrBoth::Both('c', 'a');
let mut iter = value.iter();
assert_eq!(iter.next(), Some(&'c'));
assert_eq!(iter.next(), Some(&'a'));
assert_eq!(iter.next(), None);

let value: EitherOrBoth<char> = EitherOrBoth::Left('c');
let mut iter = value.iter();
assert_eq!(iter.next(), Some(&'c'));
assert_eq!(iter.next(), None);
Source

pub fn iter_mut(&mut self) -> IterMutEitherOrBoth<'_, T>

Returns an iterator over the contained mutable values of a uniform type

The evaluation order is from left to right if this is a Both variant. To reverse the order use flip.

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<char> = EitherOrBoth::Both('c', 'a');
let mut iter = value.iter_mut();
assert_eq!(iter.next(), Some(&mut 'c'));
assert_eq!(iter.next(), Some(&mut 'a'));
assert_eq!(iter.next(), None);

let mut value: EitherOrBoth<char> = EitherOrBoth::Left('c');
let mut iter = value.iter_mut();
assert_eq!(iter.next(), Some(&mut 'c'));
assert_eq!(iter.next(), None);
Source

pub fn into_iter_chain( self, ) -> ChainedIterEitherOrBoth<<T as IntoIterator>::IntoIter>
where T: IntoIterator,

Consumes the EitherOrBoth, returning a chained iterator over the contained iterators of a uniform type

The evaluation order is from left to right if this is a Both variant. To reverse the order use flip.

For iteration over contained iterators with non-uniform types, you can use into_iter_swap instead.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<Vec<char>> = EitherOrBoth::Both(vec!['c', 'a'], vec!['b']);
let collected: Vec<char> = value.into_iter_chain().collect();
assert_eq!(collected, vec!['c', 'a', 'b']);

let value: EitherOrBoth<Vec<char>> = EitherOrBoth::Left(vec!['c', 'a']);
let collected: Vec<char> = value.into_iter_chain().collect();
assert_eq!(collected, vec!['c', 'a']);
Source

pub fn iter_chain( &self, ) -> ChainedIterEitherOrBoth<<&T as IntoIterator>::IntoIter>
where for<'a> &'a T: IntoIterator,

Returns a chained iterator over the contained iterators of a uniform type

The evaluation order is from left to right if this is a Both variant. To reverse the order use flip.

For iteration over contained iterators with non-uniform types, you can use iter_swap instead.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<Vec<char>> = EitherOrBoth::Both(vec!['c', 'a'], vec!['b']);
let collected: Vec<&char> = value.iter_chain().collect();
assert_eq!(collected, vec![&'c', &'a', &'b']);

let value: EitherOrBoth<Vec<char>> = EitherOrBoth::Left(vec!['c', 'a']);
let collected: Vec<&char> = value.iter_chain().collect();
assert_eq!(collected, vec![&'c', &'a']);
Source

pub fn iter_chain_mut( &mut self, ) -> ChainedIterEitherOrBoth<<&mut T as IntoIterator>::IntoIter>
where for<'a> &'a mut T: IntoIterator,

Returns a chained iterator over the mutable values of the contained iterators of a uniform type

The evaluation order is from left to right if this is a Both variant. To reverse the order use flip.

For iteration over contained iterators with non-uniform types, you can use iter_swap_mut instead.

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<Vec<char>> = EitherOrBoth::Both(vec!['c', 'a'], vec!['b']);
let collected: Vec<&mut char> = value.iter_chain_mut().collect();
assert_eq!(collected, vec![&mut 'c', &mut 'a', &mut 'b']);

let mut value: EitherOrBoth<Vec<char>> = EitherOrBoth::Left(vec!['c', 'a']);
let collected: Vec<&mut char> = value.iter_chain_mut().collect();
assert_eq!(collected, vec![&mut 'c', &mut 'a']);
Source§

impl<L, R> EitherOrBoth<&L, &R>

Source

pub fn cloned(self) -> EitherOrBoth<L, R>
where L: Clone, R: Clone,

Converts an EitherOrBoth<&L, &R> into an EitherOrBoth<L, R> by cloning its contents

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let refs: EitherOrBoth<&u8, &char> = value.as_ref();
assert_eq!(refs.cloned(), EitherOrBoth::Both(1, 'c'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let refs: EitherOrBoth<&u8, &char> = value.as_ref();
assert_eq!(refs.cloned(), EitherOrBoth::Left(1));
Source

pub const fn copied(self) -> EitherOrBoth<L, R>
where L: Copy, R: Copy,

Converts an EitherOrBoth<&L, &R> into an EitherOrBoth<L, R> by copying its contents

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let refs: EitherOrBoth<&u8, &char> = value.as_ref();
assert_eq!(refs.copied(), EitherOrBoth::Both(1, 'c'));

let value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let refs: EitherOrBoth<&u8, &char> = value.as_ref();
assert_eq!(refs.copied(), EitherOrBoth::Left(1));
Source§

impl<L, R> EitherOrBoth<&mut L, &mut R>

Source

pub fn cloned(self) -> EitherOrBoth<L, R>
where L: Clone, R: Clone,

Converts an EitherOrBoth<&mut L, &mut R> into an EitherOrBoth<L, R> by cloning its contents

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let refs: EitherOrBoth<&mut u8, &mut char> = value.as_mut();
assert_eq!(refs.cloned(), EitherOrBoth::Both(1, 'c'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let refs: EitherOrBoth<&mut u8, &mut char> = value.as_mut();
assert_eq!(refs.cloned(), EitherOrBoth::Left(1));
Source

pub fn copied(self) -> EitherOrBoth<L, R>
where L: Copy, R: Copy,

Converts an EitherOrBoth<&mut L, &mut R> into an EitherOrBoth<L, R> by copying its contents

§Examples
use either_or_both::EitherOrBoth;

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Both(1, 'c');
let refs: EitherOrBoth<&mut u8, &mut char> = value.as_mut();
assert_eq!(refs.copied(), EitherOrBoth::Both(1, 'c'));

let mut value: EitherOrBoth<u8, char> = EitherOrBoth::Left(1);
let refs: EitherOrBoth<&mut u8, &mut char> = value.as_mut();
assert_eq!(refs.copied(), EitherOrBoth::Left(1));
Source§

impl<L1, L2, R1, R2> EitherOrBoth<(L1, R1), (L2, R2)>

Source

pub fn transpose(self) -> (EitherOrBoth<L1, L2>, EitherOrBoth<R1, R2>)

Transposes a EitherOrBoth of tuples to a tuple of EitherOrBoths

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<(u8, char), (i32, u64)> = EitherOrBoth::Both((1, 'c'), (-2, 10));
assert_eq!(
    value.transpose(),
    (EitherOrBoth::Both(1, -2), EitherOrBoth::Both('c', 10))
);

let value: EitherOrBoth<(u8, char), (i32, u64)> = EitherOrBoth::Left((1, 'c'));
assert_eq!(
    value.transpose(),
    (EitherOrBoth::Left(1), EitherOrBoth::Left('c'))
);

let value: EitherOrBoth<(u8, char), (i32, u64)> = EitherOrBoth::Right((-2, 10));
assert_eq!(
    value.transpose(),
    (EitherOrBoth::Right(-2), EitherOrBoth::Right(10))
);
Source§

impl<L, R1, R2> EitherOrBoth<(L, R1), (L, R2)>

Source

pub fn transpose_left<F>(self, f: F) -> (L, EitherOrBoth<R1, R2>)
where F: FnOnce(L, L) -> L,

Transposes an EitherOrBoth of tuples to a tuple of a single value and an EitherOrBoth with the left value having a uniform type

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<(u8, char), (u8, i32)> = EitherOrBoth::Both((1, 'c'), (2, -10));
assert_eq!(
    value.transpose_left(|l, r| l + r),
    (3, EitherOrBoth::Both('c', -10))
);

let value: EitherOrBoth<(u8, char), (u8, i32)> = EitherOrBoth::Left((1, 'c'));
assert_eq!(
    value.transpose_left(|l, r| l + r),
    (1, EitherOrBoth::Left('c'))
);

let value: EitherOrBoth<(u8, char), (u8, i32)> = EitherOrBoth::Right((2, -10));
assert_eq!(
    value.transpose_left(|l, r| l + r),
    (2, EitherOrBoth::Right(-10))
);
Source§

impl<L1, L2, R> EitherOrBoth<(L1, R), (L2, R)>

Source

pub fn transpose_right<F>(self, f: F) -> (EitherOrBoth<L1, L2>, R)
where F: FnOnce(R, R) -> R,

Transposes an EitherOrBoth of tuples to a tuple of a single value and an EitherOrBoth with the right value having a uniform type

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<(u8, char), (i32, char)> = EitherOrBoth::Both((1, 'c'), (-2, 'a'));
assert_eq!(
    value.transpose_right(|l, r| l.max(r)),
    (EitherOrBoth::Both(1, -2), 'c')
);

let value: EitherOrBoth<(u8, char), (i32, char)> = EitherOrBoth::Left((1, 'c'));
assert_eq!(
    value.transpose_right(|l, r| l.max(r)),
    (EitherOrBoth::Left(1), 'c')
);

let value: EitherOrBoth<(u8, char), (i32, char)> = EitherOrBoth::Right((-2, 'a'));
assert_eq!(
    value.transpose_right(|l, r| l.max(r)),
    (EitherOrBoth::Right(-2), 'a')
);
Source§

impl<L, R> EitherOrBoth<Option<L>, Option<R>>

Source

pub fn transpose(self) -> Option<EitherOrBoth<L, R>>

Transposes an EitherOrBoth of Options to an option of EitherOrBoths

Per convention, if this variant is Both and at least one of the values is None, the evaluation of the None value takes precedence and results in a None value.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<Option<u8>, Option<char>> = EitherOrBoth::Both(Some(1), Some('c'));
assert_eq!(value.transpose(), Some(EitherOrBoth::Both(1, 'c')));

let value: EitherOrBoth<Option<u8>, Option<char>> = EitherOrBoth::Both(Some(1), None);
assert_eq!(value.transpose(), None);

let value: EitherOrBoth<Option<u8>, Option<char>> = EitherOrBoth::Left(Some(1));
assert_eq!(value.transpose(), Some(EitherOrBoth::Left(1)));

let value: EitherOrBoth<Option<u8>, Option<char>> = EitherOrBoth::Left(None);
assert_eq!(value.transpose(), None);
Source§

impl<L, R, E1, E2> EitherOrBoth<Result<L, E1>, Result<R, E2>>

Source

pub fn transpose(self) -> Result<EitherOrBoth<L, R>, EitherOrBoth<E1, E2>>

Transposes an EitherOrBoth of Results to a Result of EitherOrBoths

Per convention, if this variant is Both and at least one of the values is an error value Err, the evaluation of the Err value takes precedence and results in a Err value.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<Result<u8, char>, Result<i32, u64>> = EitherOrBoth::Both(Ok(1), Ok(-2));
assert_eq!(value.transpose(), Ok(EitherOrBoth::Both(1, -2)));

// If at least one of both is is an error the result is an error
let value: EitherOrBoth<Result<u8, char>, Result<i32, u64>> =
    EitherOrBoth::Both(Err('c'), Ok(-2));
assert_eq!(value.transpose(), Err(EitherOrBoth::Left('c')));

let value: EitherOrBoth<Result<u8, char>, Result<i32, u64>> = EitherOrBoth::Left(Ok(1));
assert_eq!(value.transpose(), Ok(EitherOrBoth::Left(1)));

let value: EitherOrBoth<Result<u8, char>, Result<i32, u64>> = EitherOrBoth::Left(Err('c'));
assert_eq!(value.transpose(), Err(EitherOrBoth::Left('c')));
Source§

impl<L, R, E> EitherOrBoth<Result<L, E>, Result<R, E>>

Source

pub fn transpose_err<F>(self, f: F) -> Result<EitherOrBoth<L, R>, E>
where F: FnOnce(E, E) -> E,

Transposes an EitherOrBoth of Results to a Result with an uniform error type

When Both contains two error values, the provided function merges them into a unified error.

Per convention, if this variant is Both and at least one of the values is an error value Err, the evaluation of the Err value takes precedence and results in a Err value.

§Examples
use either_or_both::EitherOrBoth;

let x: EitherOrBoth<Result<u8, char>, Result<i32, char>> = EitherOrBoth::Both(Ok(1), Ok(-2));
assert_eq!(
    x.transpose_err(|l, r| l.max(r)),
    Ok(EitherOrBoth::Both(1, -2))
);

// both are errors so they need to be merged with a given function
let x: EitherOrBoth<Result<u8, char>, Result<i32, char>> =
    EitherOrBoth::Both(Err('c'), Err('a'));
assert_eq!(x.transpose_err(|l, r| l.max(r)), Err('c'));

// If at least one of the values is an error, the result is an error
let x: EitherOrBoth<Result<u8, char>, Result<i32, char>> = EitherOrBoth::Both(Err('c'), Ok(-2));
assert_eq!(x.transpose_err(|l, r| l.max(r)), Err('c'));

let x: EitherOrBoth<Result<u8, char>, Result<i32, char>> = EitherOrBoth::Left(Ok(1));
assert_eq!(x.transpose_err(|l, r| l.max(r)), Ok(EitherOrBoth::Left(1)));

let x: EitherOrBoth<Result<u8, char>, Result<i32, char>> = EitherOrBoth::Left(Err('c'));
assert_eq!(x.transpose_err(|l, r| l.max(r)), Err('c'));
Source§

impl<T, E1, E2> EitherOrBoth<Result<T, E1>, Result<T, E2>>

Source

pub fn transpose_ok<F>(self, f: F) -> Result<T, EitherOrBoth<E1, E2>>
where F: FnOnce(T, T) -> T,

Transposes an EitherOrBoth of Results to a Result with an uniform correct type

When Both contains two correct values, the provided function merges them into a unified value.

Per convention, if this variant is Both and at least one of the values is an error value Err, the evaluation of the Err value takes precedence and results in a Err value.

§Examples
use either_or_both::EitherOrBoth;

// both are ok so they need to be merged with a given function
let x: EitherOrBoth<Result<u8, char>, Result<u8, i32>> = EitherOrBoth::Both(Ok(1), Ok(2));
assert_eq!(x.transpose_ok(|l, r| l + r), Ok(3));

// If at least one of the values is an error, the result is an error
let x: EitherOrBoth<Result<u8, char>, Result<u8, i32>> = EitherOrBoth::Both(Err('c'), Ok(2));
assert_eq!(x.transpose_ok(|l, r| l + r), Err(EitherOrBoth::Left('c')));

let x: EitherOrBoth<Result<u8, char>, Result<u8, i32>> = EitherOrBoth::Both(Err('c'), Err(-2));
assert_eq!(
    x.transpose_ok(|l, r| l + r),
    Err(EitherOrBoth::Both('c', -2))
);

let x: EitherOrBoth<Result<u8, char>, Result<u8, i32>> = EitherOrBoth::Left(Ok(2));
assert_eq!(x.transpose_ok(|l, r| l + r), Ok(2));

let x: EitherOrBoth<Result<u8, char>, Result<u8, i32>> = EitherOrBoth::Left(Err('c'));
assert_eq!(x.transpose_ok(|l, r| l + r), Err(EitherOrBoth::Left('c')));

Trait Implementations§

Source§

impl<L: Clone, R: Clone> Clone for EitherOrBoth<L, R>

Source§

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

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> Debug for EitherOrBoth<L, R>

Source§

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

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

impl<'de, L, R> Deserialize<'de> for EitherOrBoth<L, R>
where L: Deserialize<'de>, R: Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<L, R> From<(L, Option<R>)> for EitherOrBoth<L, R>

Source§

fn from((left, right): (L, Option<R>)) -> Self

Converts to this type from the input type.
Source§

impl<L, R> From<(L, R)> for EitherOrBoth<L, R>

Source§

fn from((left, right): (L, R)) -> Self

Converts to this type from the input type.
Source§

impl<L, R> From<(Option<L>, R)> for EitherOrBoth<L, R>

Source§

fn from((left, right): (Option<L>, R)) -> Self

Converts to this type from the input type.
Source§

impl<L, R> From<Either<L, R>> for EitherOrBoth<L, R>

Available on crate feature either only.
Source§

fn from(value: Either<L, R>) -> Self

Converts to this type from the input type.
Source§

impl<K1, K2, V1, V2, S1, S2> FromIterator<Either<(K1, V1), (K2, V2)>> for EitherOrBoth<HashMap<K1, V1, S1>, HashMap<K2, V2, S2>>
where K1: Hash + Eq, K2: Hash + Eq, S1: BuildHasher + Default, S2: BuildHasher + Default,

Available on crate features either and std only.
Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = Either<(K1, V1), (K2, V2)>>,

Consumes an Iterator of Either items, collecting all left and right values into separate HashMaps.

  • If only left values are present, returns Left with the left HashMap.
  • If only right values are present, returns Right with the right HashMap.
  • If none or both left and right values are present, returns Both with both HashMaps.
§Examples

This example collects into a Both variant:

use std::collections::HashMap;

use either_or_both::{Either, EitherOrBoth};

let items: Vec<Either<(&str, u8), (&str, char)>> =
    vec![Either::Right(("right", 'c')), Either::Left(("left", 2))];

let collected: EitherOrBoth<HashMap<&str, u8>, HashMap<&str, char>> =
    items.into_iter().collect();

assert_eq!(
    collected,
    EitherOrBoth::Both(
        HashMap::from([("left", 2)]),
        HashMap::from([("right", 'c')])
    )
);

This example collects into a Left variant:

use std::collections::HashMap;

use either_or_both::{Either, EitherOrBoth};

let items: Vec<Either<(&str, u8), (&str, char)>> =
    vec![Either::Left(("left", 2))];
let collected: EitherOrBoth<HashMap<&str, u8>, HashMap<&str, char>> =
    items.into_iter().collect();

assert_eq!(collected, EitherOrBoth::Left(HashMap::from([("left", 2)]),));
Source§

impl<K1, K2, V1, V2, S1, S2> FromIterator<Either<(K1, V1), (K2, V2)>> for EitherOrBoth<IndexMap<K1, V1, S1>, IndexMap<K2, V2, S2>>
where K1: Hash + Eq, K2: Hash + Eq, S1: BuildHasher + Default, S2: BuildHasher + Default,

Available on crate features either and indexmap only.
Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = Either<(K1, V1), (K2, V2)>>,

Consumes an Iterator of Either items, collecting all left and right values into separate IndexMaps.

  • If only left values are present, returns Left with the left IndexMap.
  • If only right values are present, returns Right with the right IndexMap.
  • If none or both left and right values are present, returns Both with both IndexMaps.
§Examples

This example collects into a Both variant:

use either_or_both::{Either, EitherOrBoth};
use indexmap::IndexMap;

let items: Vec<Either<(&str, u8), (&str, char)>> =
    vec![Either::Right(("right", 'c')), Either::Left(("left", 2))];

let collected: EitherOrBoth<IndexMap<&str, u8>, IndexMap<&str, char>> =
    items.into_iter().collect();

assert_eq!(
    collected,
    EitherOrBoth::Both(
        IndexMap::from([("left", 2)]),
        IndexMap::from([("right", 'c')])
    )
);

This example collects into a Left variant:

use either_or_both::{Either, EitherOrBoth};
use indexmap::IndexMap;

let items: Vec<Either<(&str, u8), (&str, char)>> =
    vec![Either::Left(("left", 2))];
let collected: EitherOrBoth<IndexMap<&str, u8>, IndexMap<&str, char>> =
    items.into_iter().collect();

assert_eq!(
    collected,
    EitherOrBoth::Left(IndexMap::from([("left", 2)]),)
);
Source§

impl<K1, K2, S1, S2> FromIterator<Either<K1, K2>> for EitherOrBoth<HashSet<K1, S1>, HashSet<K2, S2>>
where K1: Hash + Eq, K2: Hash + Eq, S1: BuildHasher + Default, S2: BuildHasher + Default,

Available on crate features either and std only.
Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = Either<K1, K2>>,

Consumes an Iterator of Either items, collecting all left and right values into separate HashSets.

  • If only left values are present, returns Left with the left HashSet.
  • If only right values are present, returns Right with the right HashSet.
  • If none or both left and right values are present, returns Both with both HashSets.
§Examples

This example collects into a Both variant:

use std::collections::HashSet;

use either_or_both::{Either, EitherOrBoth};

let items: Vec<Either<u8, char>> = vec![Either::Right('c'), Either::Left(2)];

let collected: EitherOrBoth<HashSet<u8>, HashSet<char>> =
   items.into_iter().collect();

assert_eq!(
    collected,
    EitherOrBoth::Both(HashSet::from([2]), HashSet::from(['c']))
);

This example collects into a Left variant:

use std::collections::HashSet;

use either_or_both::{Either, EitherOrBoth};

let items: Vec<Either<u8, char>> = vec![Either::Left(2)];
let collected: EitherOrBoth<HashSet<u8>, HashSet<char>> =
    items.into_iter().collect();

assert_eq!(collected, EitherOrBoth::Left(HashSet::from([2])));
Source§

impl<K1, K2, S1, S2> FromIterator<Either<K1, K2>> for EitherOrBoth<IndexSet<K1, S1>, IndexSet<K2, S2>>
where K1: Hash + Eq, K2: Hash + Eq, S1: BuildHasher + Default, S2: BuildHasher + Default,

Available on crate features either and indexmap only.
Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = Either<K1, K2>>,

Consumes an Iterator of Either items, collecting all left and right values into separate IndexSets.

  • If only left values are present, returns Left with the left IndexSet.
  • If only right values are present, returns Right with the right IndexSet.
  • If none or both left and right values are present, returns Both with both IndexSets.
§Examples

This example collects into a Both variant:

use either_or_both::{Either, EitherOrBoth};
use indexmap::IndexSet;

let items: Vec<Either<u8, char>> = vec![Either::Right('c'), Either::Left(2)];
let collected: EitherOrBoth<IndexSet<u8>, IndexSet<char>> =
   items.into_iter().collect();

assert_eq!(
    collected,
    EitherOrBoth::Both(IndexSet::from([2]), IndexSet::from(['c']))
);

This example collects into a Left variant:

use either_or_both::{Either, EitherOrBoth};
use indexmap::IndexSet;

let items: Vec<Either<u8, char>> = vec![Either::Left(2)];
let collected: EitherOrBoth<IndexSet<u8>, IndexSet<char>> =
   items.into_iter().collect();

assert_eq!(collected, EitherOrBoth::Left(IndexSet::from([2])));
Source§

impl<L, R> FromIterator<Either<L, R>> for EitherOrBoth<Vec<L>, Vec<R>>

Available on crate features either and std only.
Source§

fn from_iter<T: IntoIterator<Item = Either<L, R>>>(iter: T) -> Self

Consumes an Iterator of Either items, collecting all left and right values into separate vectors.

  • If only left values are present, returns Left with the left Vec.
  • If only right values are present, returns Right with the right Vec.
  • If none or both left and right values are present, returns Both with both Vecs.
§Examples

This example collects into a Both variant:

use either_or_both::{Either, EitherOrBoth};

let items: Vec<Either<u8, char>> = vec![Either::Left(1), Either::Right('c')];
let collected: EitherOrBoth<Vec<u8>, Vec<char>> = items.into_iter().collect();

assert_eq!(collected, EitherOrBoth::Both(vec![1], vec!['c']));

This example collects into a Left variant:

use either_or_both::{Either, EitherOrBoth};

let items: Vec<Either<u8, char>> = vec![Either::Left(1), Either::Right('c')];
let collected: EitherOrBoth<Vec<u8>, Vec<char>> = items.into_iter().collect();

assert_eq!(collected, EitherOrBoth::Both(vec![1], vec!['c']));
Source§

impl<K1, K2, V1, V2, S1, S2> FromIterator<EitherOrBoth<(K1, V1), (K2, V2)>> for EitherOrBoth<HashMap<K1, V1, S1>, HashMap<K2, V2, S2>>
where K1: Hash + Eq, K2: Hash + Eq, S1: BuildHasher + Default, S2: BuildHasher + Default,

Available on crate feature std only.
Source§

fn from_iter<T>(iter: T) -> Self

Consumes an Iterator of EitherOrBoth items, collecting all left and right values into separate HashMaps.

  • If only left values are present, returns Left with the left HashMap.
  • If only right values are present, returns Right with the right HashMap.
  • If none or both left and right values are present, returns Both with both HashMaps.
§Examples

This example collects into a Both variant:

use std::collections::HashMap;

use either_or_both::EitherOrBoth;

let items: Vec<EitherOrBoth<(&str, u8), (&str, char)>> = vec![
    EitherOrBoth::Both(("both_l", 1), ("both_r", 'c')),
    EitherOrBoth::Left(("left", 2)),
];

let collected: EitherOrBoth<HashMap<&str, u8>, HashMap<&str, char>> =
    items.into_iter().collect();

assert_eq!(
    collected,
    EitherOrBoth::Both(
        HashMap::from([("both_l", 1), ("left", 2)]),
        HashMap::from([("both_r", 'c')])
    )
);

This example collects into a Left variant:

use std::collections::HashMap;

use either_or_both::EitherOrBoth;

let items: Vec<EitherOrBoth<(&str, u8), (&str, char)>> =
    vec![EitherOrBoth::Left(("left", 2))];

let collected: EitherOrBoth<HashMap<&str, u8>, HashMap<&str, char>> =
    items.into_iter(). collect();

assert_eq!(collected, EitherOrBoth::Left(HashMap::from([("left", 2)]),));
Source§

impl<K1, K2, V1, V2, S1, S2> FromIterator<EitherOrBoth<(K1, V1), (K2, V2)>> for EitherOrBoth<IndexMap<K1, V1, S1>, IndexMap<K2, V2, S2>>
where K1: Hash + Eq, K2: Hash + Eq, S1: BuildHasher + Default, S2: BuildHasher + Default,

Available on crate feature indexmap only.
Source§

fn from_iter<T>(iter: T) -> Self

Consumes an Iterator of EitherOrBoth items, collecting all left and right values into separate IndexMaps.

  • If only left values are present, returns Left with the left IndexMap.
  • If only right values are present, returns Right with the right IndexMap.
  • If none or both left and right values are present, returns Both with both IndexMaps.
§Examples

This example collects into a Both variant:

use either_or_both::EitherOrBoth;
use indexmap::IndexMap;

let items: Vec<EitherOrBoth<(&str, u8), (&str, char)>> = vec![
    EitherOrBoth::Both(("both_l", 1), ("both_r", 'c')),
    EitherOrBoth::Left(("left", 2)),
];

let collected: EitherOrBoth<IndexMap<&str, u8>, IndexMap<&str, char>> =
    items.into_iter().collect();

assert_eq!(
    collected,
    EitherOrBoth::Both(
        IndexMap::from([("both_l", 1), ("left", 2)]),
        IndexMap::from([("both_r", 'c')])
    )
);

This example collects into a Left variant:

use either_or_both::EitherOrBoth;
use indexmap::IndexMap;

let items: Vec<EitherOrBoth<(&str, u8), (&str, char)>> =
    vec![EitherOrBoth::Left(("left", 2))];
let collected: EitherOrBoth<IndexMap<&str, u8>, IndexMap<&str, char>> =
    items.into_iter().collect();

assert_eq!(
    collected,
    EitherOrBoth::Left(IndexMap::from([("left", 2)]),)
);
Source§

impl<K1, K2, S1, S2> FromIterator<EitherOrBoth<K1, K2>> for EitherOrBoth<HashSet<K1, S1>, HashSet<K2, S2>>
where K1: Hash + Eq, K2: Hash + Eq, S1: BuildHasher + Default, S2: BuildHasher + Default,

Available on crate feature std only.
Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = EitherOrBoth<K1, K2>>,

Consumes an Iterator of EitherOrBoth items, collecting all left and right values into separate HashSets.

  • If only left values are present, returns Left with the left HashSet.
  • If only right values are present, returns Right with the right HashSet.
  • If none or both left and right values are present, returns Both with both HashSets.
§Examples

This example collects into a Both variant:

use std::collections::HashSet;

use either_or_both::EitherOrBoth;

let items: Vec<EitherOrBoth<u8, char>> =
    vec![EitherOrBoth::Both(1, 'c'), EitherOrBoth::Left(2)];

let collected: EitherOrBoth<HashSet<u8>, HashSet<char>> =
    items.into_iter().collect();

assert_eq!(
    collected,
    EitherOrBoth::Both(HashSet::from([1, 2]), HashSet::from(['c']))
);

This example collects into a Left variant:

use std::collections::HashSet;

use either_or_both::EitherOrBoth;

let items: Vec<EitherOrBoth<u8, char>> = vec![EitherOrBoth::Left(2)];
let collected: EitherOrBoth<HashSet<u8>, HashSet<char>> =
    items.into_iter().collect();

assert_eq!(collected, EitherOrBoth::Left(HashSet::from([2])));
Source§

impl<K1, K2, S1, S2> FromIterator<EitherOrBoth<K1, K2>> for EitherOrBoth<IndexSet<K1, S1>, IndexSet<K2, S2>>
where K1: Hash + Eq, K2: Hash + Eq, S1: BuildHasher + Default, S2: BuildHasher + Default,

Available on crate feature indexmap only.
Source§

fn from_iter<T>(iter: T) -> Self
where T: IntoIterator<Item = EitherOrBoth<K1, K2>>,

Consumes an Iterator of EitherOrBoth items, collecting all left and right values into separate IndexSets.

  • If only left values are present, returns Left with the left IndexSet.
  • If only right values are present, returns Right with the right IndexSet.
  • If none or both left and right values are present, returns Both with both IndexSets.
§Examples

This example collects into a Both variant:

use either_or_both::EitherOrBoth;
use indexmap::IndexSet;

let items: Vec<EitherOrBoth<u8, char>> =
    vec![EitherOrBoth::Both(1, 'c'), EitherOrBoth::Left(2)];

let collected: EitherOrBoth<IndexSet<u8>, IndexSet<char>> =
    items.into_iter().collect();

assert_eq!(
    collected,
    EitherOrBoth::Both(IndexSet::from([1, 2]), IndexSet::from(['c']))
);

This example collects into a Left variant:

use either_or_both::EitherOrBoth;
use indexmap::IndexSet;

let items: Vec<EitherOrBoth<u8, char>> = vec![EitherOrBoth::Left(2)];
let collected: EitherOrBoth<IndexSet<u8>, IndexSet<char>> =
   items.into_iter().collect();

assert_eq!(collected, EitherOrBoth::Left(IndexSet::from([2])));
Source§

impl<L, R> FromIterator<EitherOrBoth<L, R>> for EitherOrBoth<Vec<L>, Vec<R>>

Available on crate feature std only.
Source§

fn from_iter<T: IntoIterator<Item = EitherOrBoth<L, R>>>(iter: T) -> Self

Consumes an Iterator of EitherOrBoth items, collecting all left and right values into separate vectors.

  • If only left values are present, returns Left with the left Vec.
  • If only right values are present, returns Right with the right Vec.
  • If none or both left and right values are present, returns Both with both Vecs.
§Examples

This example collects into a Both variant:

use either_or_both::EitherOrBoth;

let items: Vec<EitherOrBoth<u8, char>> =
    vec![EitherOrBoth::Both(1, 'c'), EitherOrBoth::Left(2)];

let collected: EitherOrBoth<Vec<u8>, Vec<char>> = items.into_iter().collect();

assert_eq!(collected, EitherOrBoth::Both(vec![1, 2], vec!['c']));

This example collects into a Left variant:

use either_or_both::EitherOrBoth;

let items: Vec<EitherOrBoth<u8, char>> = vec![EitherOrBoth::Left(2)];
let collected: EitherOrBoth<Vec<u8>, Vec<char>> = items.into_iter().collect();

assert_eq!(collected, EitherOrBoth::Left(vec![2]));
Source§

impl<L: Hash, R: Hash> Hash for EitherOrBoth<L, R>

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<'a, T> IntoIterator for &'a EitherOrBoth<T, T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = IterEitherOrBoth<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut EitherOrBoth<T, T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMutEitherOrBoth<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for EitherOrBoth<T, T>

Source§

fn into_iter(self) -> Self::IntoIter

Returns a consuming iterator over the contained values of a uniform type

The evaluation order is from left to right if this is a Both variant. To reverse the order use flip.

§Examples
use either_or_both::EitherOrBoth;

let value: EitherOrBoth<char> = EitherOrBoth::Both('c', 'a');
let mut iter = value.into_iter();
assert_eq!(iter.next(), Some('c'));
assert_eq!(iter.next(), Some('a'));
assert_eq!(iter.next(), None);

let value: EitherOrBoth<char> = EitherOrBoth::Left('c');
let mut iter = value.into_iter();
assert_eq!(iter.next(), Some('c'));
assert_eq!(iter.next(), None);
Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIterEitherOrBoth<T>

Which kind of iterator are we turning this into?
Source§

impl<L, R> JsonSchema for EitherOrBoth<L, R>
where L: JsonSchema, R: JsonSchema,

Source§

fn schema_name() -> Cow<'static, str>

The name of the generated JSON Schema. Read more
Source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
Source§

fn json_schema(generator: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
Source§

fn inline_schema() -> bool

Whether JSON Schemas generated for this type should be included directly in parent schemas, rather than being re-used where possible using the $ref keyword. Read more
Source§

impl<L: PartialEq, R: PartialEq> PartialEq for EitherOrBoth<L, R>

Source§

fn eq(&self, other: &EitherOrBoth<L, R>) -> 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.
Source§

impl<L, R> Serialize for EitherOrBoth<L, R>
where L: Serialize, R: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<L, R> TryFrom<(Option<L>, Option<R>)> for EitherOrBoth<L, R>

Source§

type Error = TryFromOptionsError

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

fn try_from(value: (Option<L>, Option<R>)) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<L: Copy, R: Copy> Copy for EitherOrBoth<L, R>

Source§

impl<L: Eq, R: Eq> Eq for EitherOrBoth<L, R>

Source§

impl<L, R> StructuralPartialEq for EitherOrBoth<L, R>

Auto Trait Implementations§

§

impl<L, R> Freeze for EitherOrBoth<L, R>
where L: Freeze, R: Freeze,

§

impl<L, R> RefUnwindSafe for EitherOrBoth<L, R>

§

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

§

impl<L, R> Sync for EitherOrBoth<L, R>
where L: Sync, R: Sync,

§

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

§

impl<L, R> UnwindSafe for EitherOrBoth<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> 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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,