]>
git.proxmox.com Git - rustc.git/blob - vendor/itertools-0.9.0/src/either_or_both.rs
1 use crate::EitherOrBoth
::*;
5 /// Value that either holds a single A or B, or both.
6 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
7 pub enum EitherOrBoth
<A
, B
> {
8 /// Both values are present.
10 /// Only the left value of type `A` is present.
12 /// Only the right value of type `B` is present.
16 impl<A
, B
> EitherOrBoth
<A
, B
> {
17 /// If `Left`, or `Both`, return true, otherwise, return false.
18 pub fn has_left(&self) -> bool
{
19 self.as_ref().left().is_some()
22 /// If `Right`, or `Both`, return true, otherwise, return false.
23 pub fn has_right(&self) -> bool
{
24 self.as_ref().right().is_some()
27 /// If Left, return true otherwise, return false.
28 /// Exclusive version of [`has_left`].
29 pub fn is_left(&self) -> bool
{
36 /// If Right, return true otherwise, return false.
37 /// Exclusive version of [`has_right`].
38 pub fn is_right(&self) -> bool
{
45 /// If Right, return true otherwise, return false.
46 /// Equivalent to `self.as_ref().both().is_some()`.
47 pub fn is_both(&self) -> bool
{
48 self.as_ref().both().is_some()
51 /// If `Left`, or `Both`, return `Some` with the left value, otherwise, return `None`.
52 pub fn left(self) -> Option
<A
> {
54 Left(left
) | Both(left
, _
) => Some(left
),
59 /// If `Right`, or `Both`, return `Some` with the right value, otherwise, return `None`.
60 pub fn right(self) -> Option
<B
> {
62 Right(right
) | Both(_
, right
) => Some(right
),
67 /// If Both, return `Some` tuple containing left and right.
68 pub fn both(self) -> Option
<(A
, B
)> {
70 Both(a
, b
) => Some((a
, b
)),
75 /// Converts from `&EitherOrBoth<A, B>` to `EitherOrBoth<&A, &B>`.
76 pub fn as_ref(&self) -> EitherOrBoth
<&A
, &B
> {
78 Left(ref left
) => Left(left
),
79 Right(ref right
) => Right(right
),
80 Both(ref left
, ref right
) => Both(left
, right
),
84 /// Converts from `&mut EitherOrBoth<A, B>` to `EitherOrBoth<&mut A, &mut B>`.
85 pub fn as_mut(&mut self) -> EitherOrBoth
<&mut A
, &mut B
> {
87 Left(ref mut left
) => Left(left
),
88 Right(ref mut right
) => Right(right
),
89 Both(ref mut left
, ref mut right
) => Both(left
, right
),
93 /// Convert `EitherOrBoth<A, B>` to `EitherOrBoth<B, A>`.
94 pub fn flip(self) -> EitherOrBoth
<B
, A
> {
98 Both(a
, b
) => Both(b
, a
),
102 /// Apply the function `f` on the value `a` in `Left(a)` or `Both(a, b)` variants. If it is
103 /// present rewrapping the result in `self`'s original variant.
104 pub fn map_left
<F
, M
>(self, f
: F
) -> EitherOrBoth
<M
, B
>
109 Both(a
, b
) => Both(f(a
), b
),
110 Left(a
) => Left(f(a
)),
111 Right(b
) => Right(b
),
115 /// Apply the function `f` on the value `b` in `Right(b)` or `Both(a, b)` variants.
116 /// If it is present rewrapping the result in `self`'s original variant.
117 pub fn map_right
<F
, M
>(self, f
: F
) -> EitherOrBoth
<A
, M
>
123 Right(b
) => Right(f(b
)),
124 Both(a
, b
) => Both(a
, f(b
)),
128 /// Apply the functions `f` and `g` on the value `a` and `b` respectively;
129 /// found in `Left(a)`, `Right(b)`, or `Both(a, b)` variants.
130 /// The Result is rewrapped `self`'s original variant.
131 pub fn map_any
<F
, L
, G
, R
>(self, f
: F
, g
: G
) -> EitherOrBoth
<L
, R
>
137 Left(a
) => Left(f(a
)),
138 Right(b
) => Right(g(b
)),
139 Both(a
, b
) => Both(f(a
), g(b
)),
143 /// Apply the function `f` on the value `b` in `Right(b)` or `Both(a, _)` variants if it is
145 pub fn left_and_then
<F
, L
>(self, f
: F
) -> EitherOrBoth
<L
, B
>
147 F
: FnOnce(A
) -> EitherOrBoth
<L
, B
>,
150 Left(a
) | Both(a
, _
) => f(a
),
151 Right(b
) => Right(b
),
155 /// Apply the function `f` on the value `a`
156 /// in `Left(a)` or `Both(a, _)` variants if it is present.
157 pub fn right_and_then
<F
, R
>(self, f
: F
) -> EitherOrBoth
<A
, R
>
159 F
: FnOnce(B
) -> EitherOrBoth
<A
, R
>,
163 Right(b
) | Both(_
, b
) => f(b
),
168 impl<T
> EitherOrBoth
<T
, T
> {
169 /// Return either value of left, right, or the product of `f` applied where `Both` are present.
170 pub fn reduce
<F
>(self, f
: F
) -> T
172 F
: FnOnce(T
, T
) -> T
,
177 Both(a
, b
) => f(a
, b
),
182 impl<A
, B
> Into
<Option
<Either
<A
, B
>>> for EitherOrBoth
<A
, B
> {
183 fn into(self) -> Option
<Either
<A
, B
>> {
185 EitherOrBoth
::Left(l
) => Some(Either
::Left(l
)),
186 EitherOrBoth
::Right(r
) => Some(Either
::Right(r
)),