summaryrefslogtreecommitdiffstats
path: root/vendor/itertools/src/either_or_both.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-19 09:26:03 +0000
commit9918693037dce8aa4bb6f08741b6812923486c18 (patch)
tree21d2b40bec7e6a7ea664acee056eb3d08e15a1cf /vendor/itertools/src/either_or_both.rs
parentReleasing progress-linux version 1.75.0+dfsg1-5~progress7.99u1. (diff)
downloadrustc-9918693037dce8aa4bb6f08741b6812923486c18.tar.xz
rustc-9918693037dce8aa4bb6f08741b6812923486c18.zip
Merging upstream version 1.76.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/itertools/src/either_or_both.rs')
-rw-r--r--vendor/itertools/src/either_or_both.rs29
1 files changed, 27 insertions, 2 deletions
diff --git a/vendor/itertools/src/either_or_both.rs b/vendor/itertools/src/either_or_both.rs
index cf65fe788..9dbc880d3 100644
--- a/vendor/itertools/src/either_or_both.rs
+++ b/vendor/itertools/src/either_or_both.rs
@@ -6,7 +6,7 @@ use either::Either;
/// Value that either holds a single A or B, or both.
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
-pub enum EitherOrBoth<A, B> {
+pub enum EitherOrBoth<A, B = A> {
/// Both values are present.
Both(A, B),
/// Only the left value of type `A` is present.
@@ -65,6 +65,14 @@ impl<A, B> EitherOrBoth<A, B> {
}
}
+ /// Return tuple of options corresponding to the left and right value respectively
+ ///
+ /// If `Left` return `(Some(..), None)`, if `Right` return `(None,Some(..))`, else return
+ /// `(Some(..),Some(..))`
+ pub fn left_and_right(self) -> (Option<A>, Option<B>) {
+ self.map_any(Some, Some).or_default()
+ }
+
/// If `Left`, return `Some` with the left value. If `Right` or `Both`, return `None`.
///
/// # Examples
@@ -464,13 +472,21 @@ impl<A, B> EitherOrBoth<A, B> {
impl<T> EitherOrBoth<T, T> {
/// Return either value of left, right, or apply a function `f` to both values if both are present.
/// The input function has to return the same type as both Right and Left carry.
- ///
+ ///
+ /// This function can be used to preferrably extract the left resp. right value,
+ /// but fall back to the other (i.e. right resp. left) if the preferred one is not present.
+ ///
/// # Examples
/// ```
/// # use itertools::EitherOrBoth;
/// assert_eq!(EitherOrBoth::Both(3, 7).reduce(u32::max), 7);
/// assert_eq!(EitherOrBoth::Left(3).reduce(u32::max), 3);
/// assert_eq!(EitherOrBoth::Right(7).reduce(u32::max), 7);
+ ///
+ /// // Extract the left value if present, fall back to the right otherwise.
+ /// assert_eq!(EitherOrBoth::Left("left").reduce(|l, _r| l), "left");
+ /// assert_eq!(EitherOrBoth::Right("right").reduce(|l, _r| l), "right");
+ /// assert_eq!(EitherOrBoth::Both("left", "right").reduce(|l, _r| l), "left");
/// ```
pub fn reduce<F>(self, f: F) -> T
where
@@ -493,3 +509,12 @@ impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B> {
}
}
}
+
+impl<A, B> From<Either<A, B>> for EitherOrBoth<A, B> {
+ fn from(either: Either<A, B>) -> Self {
+ match either {
+ Either::Left(l) => EitherOrBoth::Left(l),
+ Either::Right(l) => EitherOrBoth::Right(l),
+ }
+ }
+}