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>
impl<L, R> EitherOrBoth<L, R>
Sourcepub const fn has_left(&self) -> bool
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);
Sourcepub fn has_left_and<F>(self, f: F) -> bool
pub fn has_left_and<F>(self, f: F) -> 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);
Sourcepub fn has_left_or<F>(self, f: F) -> bool
pub fn has_left_or<F>(self, f: F) -> 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);
Sourcepub const fn has_right(&self) -> bool
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);
Sourcepub fn has_right_and<F>(self, f: F) -> bool
pub fn has_right_and<F>(self, f: F) -> 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);
Sourcepub fn has_right_or<F>(self, f: F) -> bool
pub fn has_right_or<F>(self, f: F) -> 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);
Sourcepub fn is_both_and<F>(self, f: F) -> bool
pub fn is_both_and<F>(self, f: F) -> 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);
Sourcepub fn is_both_or<F, G>(self, f: F, g: G) -> bool
pub fn is_both_or<F, G>(self, f: F, g: G) -> 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);
Sourcepub const fn is_left(&self) -> bool
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);
Sourcepub fn is_left_and<F>(self, f: F) -> bool
pub fn is_left_and<F>(self, f: F) -> 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);
Sourcepub fn is_left_or<F>(self, f: F) -> bool
pub fn is_left_or<F>(self, f: F) -> 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);
Sourcepub const fn is_right(&self) -> bool
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);
Sourcepub fn is_right_and<F>(self, f: F) -> bool
pub fn is_right_and<F>(self, f: F) -> 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);
Sourcepub fn is_right_or<F>(self, f: F) -> bool
pub fn is_right_or<F>(self, f: F) -> 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);
Sourcepub const fn as_ref(&self) -> EitherOrBoth<&L, &R>
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);
Sourcepub fn as_mut(&mut self) -> EitherOrBoth<&mut L, &mut R>
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'))
Sourcepub fn as_deref(
&self,
) -> EitherOrBoth<&<L as Deref>::Target, &<R as Deref>::Target>
pub fn as_deref( &self, ) -> EitherOrBoth<&<L as Deref>::Target, &<R as Deref>::Target>
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"));
Sourcepub fn as_deref_mut(
&mut self,
) -> EitherOrBoth<&mut <L as Deref>::Target, &mut <R as Deref>::Target>
pub fn as_deref_mut( &mut self, ) -> EitherOrBoth<&mut <L as Deref>::Target, &mut <R as Deref>::Target>
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()
))
);
Sourcepub fn as_pin_ref(self: Pin<&Self>) -> EitherOrBoth<Pin<&L>, Pin<&R>>
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>>
.
Sourcepub fn as_pin_mut(
self: Pin<&mut Self>,
) -> EitherOrBoth<Pin<&mut L>, Pin<&mut R>>
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>>
.
Sourcepub fn expect_both(self, msg: &str) -> (L, R)
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");
Sourcepub fn expect_left(self, msg: &str) -> L
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");
Sourcepub fn expect_only_left(self, msg: &str) -> L
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`
Sourcepub fn expect_right(self, msg: &str) -> R
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");
Sourcepub fn expect_only_right(self, msg: &str) -> R
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`
Sourcepub fn unwrap_both(self) -> (L, R)
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
Sourcepub unsafe fn unwrap_both_unchecked(self) -> (L, R)
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"));
Sourcepub fn unwrap_left(self) -> L
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
Sourcepub unsafe fn unwrap_left_unchecked(self) -> L
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");
Sourcepub fn unwrap_only_left(self) -> L
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
Sourcepub unsafe fn unwrap_only_left_unchecked(self) -> L
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");
Sourcepub fn unwrap_right(self) -> R
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
Sourcepub unsafe fn unwrap_right_unchecked(self) -> R
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");
Sourcepub fn unwrap_only_right(self) -> R
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
Sourcepub unsafe fn unwrap_only_right_unchecked(self) -> R
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");
Sourcepub fn both(self) -> Option<(L, R)>
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);
Sourcepub fn both_and(self, other: Self) -> Self
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'));
Sourcepub fn both_and_then<F>(self, f: F) -> Selfwhere
F: FnOnce(L, R) -> Self,
pub fn both_and_then<F>(self, f: F) -> Selfwhere
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'));
Sourcepub fn left(self) -> Option<L>
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);
Sourcepub fn left_and<T>(self, other: EitherOrBoth<T, R>) -> EitherOrBoth<T, R>
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'));
Sourcepub fn left_and_then<F, T>(self, f: F) -> EitherOrBoth<T, R>where
F: FnOnce(L) -> EitherOrBoth<T, R>,
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'));
Sourcepub fn only_left(self) -> Option<L>
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);
Sourcepub fn right(self) -> Option<R>
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);
Sourcepub fn right_and<T>(self, other: EitherOrBoth<L, T>) -> EitherOrBoth<L, T>
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));
Sourcepub fn right_and_then<F, T>(self, f: F) -> EitherOrBoth<L, T>where
F: FnOnce(R) -> EitherOrBoth<L, T>,
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));
Sourcepub fn only_right(self) -> Option<R>
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);
Sourcepub fn unzip(self) -> (Option<L>, Option<R>)
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')));
Sourcepub fn into_iter_swap(
self,
) -> SwapIterEitherOrBoth<<L as IntoIterator>::IntoIter, <R as IntoIterator>::IntoIter> ⓘwhere
L: IntoIterator,
R: IntoIterator,
pub fn into_iter_swap(
self,
) -> SwapIterEitherOrBoth<<L as IntoIterator>::IntoIter, <R as IntoIterator>::IntoIter> ⓘwhere
L: IntoIterator,
R: IntoIterator,
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);
Sourcepub fn iter_swap(
&self,
) -> SwapIterEitherOrBoth<<&L as IntoIterator>::IntoIter, <&R as IntoIterator>::IntoIter> ⓘ
pub fn iter_swap( &self, ) -> SwapIterEitherOrBoth<<&L as IntoIterator>::IntoIter, <&R as IntoIterator>::IntoIter> ⓘ
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`
Sourcepub fn iter_swap_mut(
&mut self,
) -> SwapIterEitherOrBoth<<&mut L as IntoIterator>::IntoIter, <&mut R as IntoIterator>::IntoIter> ⓘ
pub fn iter_swap_mut( &mut self, ) -> SwapIterEitherOrBoth<<&mut L as IntoIterator>::IntoIter, <&mut R as IntoIterator>::IntoIter> ⓘ
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`
Sourcepub fn flip(self) -> EitherOrBoth<R, L>
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));
Sourcepub fn bimap<F, G, T, U>(self, f: F, g: G) -> EitherOrBoth<T, U>
pub fn bimap<F, G, T, U>(self, f: F, g: G) -> EitherOrBoth<T, 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())
);
Sourcepub fn map_left<F, T>(self, f: F) -> EitherOrBoth<T, R>where
F: FnOnce(L) -> T,
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'));
Sourcepub fn map_left_or<F, T>(self, default: T, f: F) -> Twhere
F: FnOnce(L) -> T,
pub fn map_left_or<F, T>(self, default: T, f: F) -> Twhere
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);
Sourcepub fn map_left_or_default<T, F>(self, f: F) -> T
pub fn map_left_or_default<T, F>(self, f: F) -> 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);
Sourcepub fn map_left_or_else<D, F, T>(self, default: D, f: F) -> T
pub fn map_left_or_else<D, F, T>(self, default: D, f: F) -> 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()
);
Sourcepub fn map_right<T>(self, f: fn(R) -> T) -> EitherOrBoth<L, T>
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));
Sourcepub fn map_right_or<U, F>(self, default: U, f: F) -> Uwhere
F: FnOnce(R) -> U,
pub fn map_right_or<U, F>(self, default: U, f: F) -> Uwhere
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);
Sourcepub fn map_right_or_default<T, F>(self, f: F) -> T
pub fn map_right_or_default<T, F>(self, f: F) -> 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);
Sourcepub fn map_right_or_else<U, D, F>(self, default: D, f: F) -> U
pub fn map_right_or_else<U, D, F>(self, default: D, f: F) -> 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")
);
Sourcepub fn biinspect<F, G>(self, f: F, g: G) -> Self
pub fn biinspect<F, G>(self, f: F, g: G) -> Self
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'));
Sourcepub fn inspect_left<F>(self, f: F) -> Self
pub fn inspect_left<F>(self, f: F) -> Self
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');
Sourcepub fn inspect_right<F>(self, f: F) -> Self
pub fn inspect_right<F>(self, f: F) -> Self
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);
Sourcepub fn biapply<F, G>(self, f: F, g: G)
pub fn biapply<F, G>(self, f: F, g: G)
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));
Sourcepub fn biapply_with<F, G, Acc>(self, acc: &mut Acc, f: F, g: G)
pub fn biapply_with<F, G, Acc>(self, acc: &mut Acc, f: F, g: G)
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]);
Sourcepub fn apply_left<F>(self, f: F)where
F: FnMut(L),
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);
Sourcepub fn apply_right<F>(self, f: F)where
F: FnMut(R),
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);
Sourcepub fn reduce_left<F>(self, f: F) -> Lwhere
F: FnOnce(R) -> L,
pub fn reduce_left<F>(self, f: F) -> Lwhere
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);
Sourcepub fn reduce_map_left<F, G, T>(self, f: F, g: G) -> T
pub fn reduce_map_left<F, G, T>(self, f: F, g: G) -> 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()
);
Sourcepub fn reduce_right<F>(self, f: F) -> Rwhere
F: FnOnce(L) -> R,
pub fn reduce_right<F>(self, f: F) -> Rwhere
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');
Sourcepub fn reduce_map_right<F, G, T>(self, f: F, g: G) -> T
pub fn reduce_map_right<F, G, T>(self, f: F, g: G) -> 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()
);
Sourcepub fn ok(self) -> Result<R, L>
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"));
Sourcepub fn ok_or<E>(self, error: E) -> Result<R, E>
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"));
Sourcepub fn ok_or_else<F, E>(self, error: F) -> Result<R, E>where
F: FnOnce() -> E,
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"))
);
Sourcepub fn or(self, left: L, right: R) -> (L, R)
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'));
Sourcepub fn or_default(self) -> (L, R)
pub fn or_default(self) -> (L, R)
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'));
Sourcepub fn or_else<F, G>(self, f: F, g: G) -> (L, R)
pub fn or_else<F, G>(self, f: F, g: G) -> (L, 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'));
Sourcepub fn into_left<F>(self, f: F) -> Selfwhere
F: FnOnce(R) -> L,
pub fn into_left<F>(self, f: F) -> Selfwhere
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));
Sourcepub fn into_right<F>(self, f: F) -> Selfwhere
F: FnOnce(L) -> R,
pub fn into_right<F>(self, f: F) -> Selfwhere
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'));
Sourcepub fn insert_left(&mut self, left: L) -> &mut L
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));
Sourcepub fn insert_right(&mut self, right: R) -> &mut R
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'));
Sourcepub fn left_or_insert(&mut self, value: L) -> &mut L
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'));
Sourcepub fn left_or_insert_default(&mut self) -> &mut Lwhere
L: Default,
pub fn left_or_insert_default(&mut self) -> &mut Lwhere
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'));
Sourcepub fn left_or_insert_with<F>(&mut self, f: F) -> &mut Lwhere
F: FnOnce() -> L,
pub fn left_or_insert_with<F>(&mut self, f: F) -> &mut Lwhere
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'));
Sourcepub fn right_or_insert(&mut self, value: R) -> &mut R
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'));
Sourcepub fn right_or_insert_default(&mut self) -> &mut Rwhere
R: Default,
pub fn right_or_insert_default(&mut self) -> &mut Rwhere
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'));
Sourcepub fn right_or_insert_with<F>(&mut self, f: F) -> &mut Rwhere
F: FnOnce() -> R,
pub fn right_or_insert_with<F>(&mut self, f: F) -> &mut Rwhere
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'));
Sourcepub fn replace_any(&mut self, left: L, right: R) -> Self
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'));
Sourcepub fn replace_left(&mut self, value: L) -> Option<L>
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'));
Sourcepub fn replace_right(&mut self, value: R) -> Option<R>
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>
impl<T> EitherOrBoth<T, T>
Sourcepub fn apply<F>(self, f: F)where
F: FnMut(T),
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']);
Sourcepub fn inspect<F>(self, f: F) -> Self
pub fn inspect<F>(self, f: F) -> Self
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'));
Sourcepub fn map<F, U>(self, f: F) -> EitherOrBoth<U, U>where
F: Fn(T) -> U,
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())
);
Sourcepub fn reduce<F>(self, f: F) -> Twhere
F: FnOnce(T, T) -> T,
pub fn reduce<F>(self, f: F) -> Twhere
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);
Sourcepub fn iter(&self) -> IterEitherOrBoth<'_, T> ⓘ
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);
Sourcepub fn iter_mut(&mut self) -> IterMutEitherOrBoth<'_, T> ⓘ
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);
Sourcepub fn into_iter_chain(
self,
) -> ChainedIterEitherOrBoth<<T as IntoIterator>::IntoIter> ⓘwhere
T: IntoIterator,
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']);
Sourcepub fn iter_chain(
&self,
) -> ChainedIterEitherOrBoth<<&T as IntoIterator>::IntoIter> ⓘwhere
for<'a> &'a T: IntoIterator,
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']);
Sourcepub fn iter_chain_mut(
&mut self,
) -> ChainedIterEitherOrBoth<<&mut T as IntoIterator>::IntoIter> ⓘwhere
for<'a> &'a mut T: IntoIterator,
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>
impl<L, R> EitherOrBoth<&L, &R>
Sourcepub fn cloned(self) -> EitherOrBoth<L, R>
pub fn cloned(self) -> EitherOrBoth<L, R>
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));
Sourcepub const fn copied(self) -> EitherOrBoth<L, R>
pub const fn copied(self) -> EitherOrBoth<L, R>
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>
impl<L, R> EitherOrBoth<&mut L, &mut R>
Sourcepub fn cloned(self) -> EitherOrBoth<L, R>
pub fn cloned(self) -> EitherOrBoth<L, R>
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));
Sourcepub fn copied(self) -> EitherOrBoth<L, R>
pub fn copied(self) -> EitherOrBoth<L, R>
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)>
impl<L1, L2, R1, R2> EitherOrBoth<(L1, R1), (L2, R2)>
Sourcepub fn transpose(self) -> (EitherOrBoth<L1, L2>, EitherOrBoth<R1, R2>)
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)>
impl<L, R1, R2> EitherOrBoth<(L, R1), (L, R2)>
Sourcepub fn transpose_left<F>(self, f: F) -> (L, EitherOrBoth<R1, R2>)where
F: FnOnce(L, L) -> L,
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)>
impl<L1, L2, R> EitherOrBoth<(L1, R), (L2, R)>
Sourcepub fn transpose_right<F>(self, f: F) -> (EitherOrBoth<L1, L2>, R)where
F: FnOnce(R, R) -> R,
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>>
impl<L, R> EitherOrBoth<Option<L>, Option<R>>
Sourcepub fn transpose(self) -> Option<EitherOrBoth<L, R>>
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>>
impl<L, R, E1, E2> EitherOrBoth<Result<L, E1>, Result<R, E2>>
Sourcepub fn transpose(self) -> Result<EitherOrBoth<L, R>, EitherOrBoth<E1, E2>>
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>>
impl<L, R, E> EitherOrBoth<Result<L, E>, Result<R, E>>
Sourcepub fn transpose_err<F>(self, f: F) -> Result<EitherOrBoth<L, R>, E>where
F: FnOnce(E, E) -> E,
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>>
impl<T, E1, E2> EitherOrBoth<Result<T, E1>, Result<T, E2>>
Sourcepub fn transpose_ok<F>(self, f: F) -> Result<T, EitherOrBoth<E1, E2>>where
F: FnOnce(T, T) -> T,
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>
impl<L: Clone, R: Clone> Clone for EitherOrBoth<L, R>
Source§fn clone(&self) -> EitherOrBoth<L, R>
fn clone(&self) -> EitherOrBoth<L, R>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<'de, L, R> Deserialize<'de> for EitherOrBoth<L, R>where
L: Deserialize<'de>,
R: Deserialize<'de>,
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>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<L, R> From<(L, Option<R>)> for EitherOrBoth<L, R>
impl<L, R> From<(L, Option<R>)> for EitherOrBoth<L, R>
Source§impl<L, R> From<(L, R)> for EitherOrBoth<L, R>
impl<L, R> From<(L, R)> for EitherOrBoth<L, R>
Source§impl<L, R> From<(Option<L>, R)> for EitherOrBoth<L, R>
impl<L, R> From<(Option<L>, R)> for EitherOrBoth<L, R>
Source§impl<L, R> From<Either<L, R>> for EitherOrBoth<L, R>
Available on crate feature either
only.
impl<L, R> From<Either<L, R>> for EitherOrBoth<L, R>
either
only.Source§impl<K1, K2, V1, V2, S1, S2> FromIterator<Either<(K1, V1), (K2, V2)>> for EitherOrBoth<HashMap<K1, V1, S1>, HashMap<K2, V2, S2>>
Available on crate features either
and std
only.
impl<K1, K2, V1, V2, S1, S2> FromIterator<Either<(K1, V1), (K2, V2)>> for EitherOrBoth<HashMap<K1, V1, S1>, HashMap<K2, V2, S2>>
either
and std
only.Source§fn from_iter<T>(iter: T) -> Self
fn from_iter<T>(iter: T) -> Self
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 leftHashMap
. - If only right values are present, returns
Right
with the rightHashMap
. - If none or both left and right values are present, returns
Both
with bothHashMaps
.
§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>>
Available on crate features either
and indexmap
only.
impl<K1, K2, V1, V2, S1, S2> FromIterator<Either<(K1, V1), (K2, V2)>> for EitherOrBoth<IndexMap<K1, V1, S1>, IndexMap<K2, V2, S2>>
either
and indexmap
only.Source§fn from_iter<T>(iter: T) -> Self
fn from_iter<T>(iter: T) -> Self
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 leftIndexMap
. - If only right values are present, returns
Right
with the rightIndexMap
. - If none or both left and right values are present, returns
Both
with bothIndexMaps
.
§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>>
Available on crate features either
and std
only.
impl<K1, K2, S1, S2> FromIterator<Either<K1, K2>> for EitherOrBoth<HashSet<K1, S1>, HashSet<K2, S2>>
either
and std
only.Source§fn from_iter<T>(iter: T) -> Selfwhere
T: IntoIterator<Item = Either<K1, K2>>,
fn from_iter<T>(iter: T) -> Selfwhere
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 leftHashSet
. - If only right values are present, returns
Right
with the rightHashSet
. - If none or both left and right values are present, returns
Both
with bothHashSets
.
§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>>
Available on crate features either
and indexmap
only.
impl<K1, K2, S1, S2> FromIterator<Either<K1, K2>> for EitherOrBoth<IndexSet<K1, S1>, IndexSet<K2, S2>>
either
and indexmap
only.Source§fn from_iter<T>(iter: T) -> Selfwhere
T: IntoIterator<Item = Either<K1, K2>>,
fn from_iter<T>(iter: T) -> Selfwhere
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 leftIndexSet
. - If only right values are present, returns
Right
with the rightIndexSet
. - If none or both left and right values are present, returns
Both
with bothIndexSets
.
§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.
impl<L, R> FromIterator<Either<L, R>> for EitherOrBoth<Vec<L>, Vec<R>>
either
and std
only.Source§fn from_iter<T: IntoIterator<Item = Either<L, R>>>(iter: T) -> Self
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 leftVec
. - If only right values are present, returns
Right
with the rightVec
. - If none or both left and right values are present, returns
Both
with bothVec
s.
§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>>
Available on crate feature std
only.
impl<K1, K2, V1, V2, S1, S2> FromIterator<EitherOrBoth<(K1, V1), (K2, V2)>> for EitherOrBoth<HashMap<K1, V1, S1>, HashMap<K2, V2, S2>>
std
only.Source§fn from_iter<T>(iter: T) -> Self
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 leftHashMap
. - If only right values are present, returns
Right
with the rightHashMap
. - If none or both left and right values are present, returns
Both
with bothHashMaps
.
§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>>
Available on crate feature indexmap
only.
impl<K1, K2, V1, V2, S1, S2> FromIterator<EitherOrBoth<(K1, V1), (K2, V2)>> for EitherOrBoth<IndexMap<K1, V1, S1>, IndexMap<K2, V2, S2>>
indexmap
only.Source§fn from_iter<T>(iter: T) -> Self
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 leftIndexMap
. - If only right values are present, returns
Right
with the rightIndexMap
. - If none or both left and right values are present, returns
Both
with bothIndexMaps
.
§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>>
Available on crate feature std
only.
impl<K1, K2, S1, S2> FromIterator<EitherOrBoth<K1, K2>> for EitherOrBoth<HashSet<K1, S1>, HashSet<K2, S2>>
std
only.Source§fn from_iter<T>(iter: T) -> Selfwhere
T: IntoIterator<Item = EitherOrBoth<K1, K2>>,
fn from_iter<T>(iter: T) -> Selfwhere
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 leftHashSet
. - If only right values are present, returns
Right
with the rightHashSet
. - If none or both left and right values are present, returns
Both
with bothHashSets
.
§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>>
Available on crate feature indexmap
only.
impl<K1, K2, S1, S2> FromIterator<EitherOrBoth<K1, K2>> for EitherOrBoth<IndexSet<K1, S1>, IndexSet<K2, S2>>
indexmap
only.Source§fn from_iter<T>(iter: T) -> Selfwhere
T: IntoIterator<Item = EitherOrBoth<K1, K2>>,
fn from_iter<T>(iter: T) -> Selfwhere
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 leftIndexSet
. - If only right values are present, returns
Right
with the rightIndexSet
. - If none or both left and right values are present, returns
Both
with bothIndexSets
.
§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.
impl<L, R> FromIterator<EitherOrBoth<L, R>> for EitherOrBoth<Vec<L>, Vec<R>>
std
only.Source§fn from_iter<T: IntoIterator<Item = EitherOrBoth<L, R>>>(iter: T) -> Self
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 leftVec
. - If only right values are present, returns
Right
with the rightVec
. - If none or both left and right values are present, returns
Both
with bothVec
s.
§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<'a, T> IntoIterator for &'a EitherOrBoth<T, T>
impl<'a, T> IntoIterator for &'a EitherOrBoth<T, T>
Source§impl<'a, T> IntoIterator for &'a mut EitherOrBoth<T, T>
impl<'a, T> IntoIterator for &'a mut EitherOrBoth<T, T>
Source§impl<T> IntoIterator for EitherOrBoth<T, T>
impl<T> IntoIterator for EitherOrBoth<T, T>
Source§fn into_iter(self) -> Self::IntoIter
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 IntoIter = IntoIterEitherOrBoth<T>
type IntoIter = IntoIterEitherOrBoth<T>
Source§impl<L, R> JsonSchema for EitherOrBoth<L, R>where
L: JsonSchema,
R: JsonSchema,
impl<L, R> JsonSchema for EitherOrBoth<L, R>where
L: JsonSchema,
R: JsonSchema,
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn inline_schema() -> bool
fn inline_schema() -> bool
$ref
keyword. Read moreSource§impl<L, R> Serialize for EitherOrBoth<L, R>
impl<L, R> Serialize for EitherOrBoth<L, R>
impl<L: Copy, R: Copy> Copy for EitherOrBoth<L, R>
impl<L: Eq, R: Eq> Eq for EitherOrBoth<L, R>
impl<L, R> StructuralPartialEq for EitherOrBoth<L, R>
Auto Trait Implementations§
impl<L, R> Freeze for EitherOrBoth<L, R>
impl<L, R> RefUnwindSafe for EitherOrBoth<L, R>where
L: RefUnwindSafe,
R: RefUnwindSafe,
impl<L, R> Send for EitherOrBoth<L, R>
impl<L, R> Sync for EitherOrBoth<L, R>
impl<L, R> Unpin for EitherOrBoth<L, R>
impl<L, R> UnwindSafe for EitherOrBoth<L, R>where
L: UnwindSafe,
R: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.