/// Value that either holds a single A or B, or both. #[derive(Clone, PartialEq, Eq, Hash, Debug)] pubenum EitherOrBoth<A, B> { /// Both values are present.
Both(A, B), /// Only the left value of type `A` is present.
Left(A), /// Only the right value of type `B` is present.
Right(B),
}
/// Converts from `&mut EitherOrBoth<A, B>` to `EitherOrBoth<&mut A, &mut B>`. pubfn as_mut(&mutself) -> EitherOrBoth<&mut A, &mut B> { match *self {
Left(refmut left) => Left(left),
Right(refmut right) => Right(right),
Both(refmut left, refmut right) => Both(left, right),
}
}
/// Convert `EitherOrBoth<A, B>` to `EitherOrBoth<B, A>`. pubfn flip(self) -> EitherOrBoth<B, A> { matchself {
Left(a) => Right(a),
Right(b) => Left(b),
Both(a, b) => Both(b, a),
}
}
/// Apply the function `f` on the value `a` in `Left(a)` or `Both(a, b)` variants. If it is /// present rewrapping the result in `self`'s original variant. pubfn map_left<F, M>(self, f: F) -> EitherOrBoth<M, B> where
F: FnOnce(A) -> M,
{ matchself {
Both(a, b) => Both(f(a), b),
Left(a) => Left(f(a)),
Right(b) => Right(b),
}
}
/// Apply the function `f` on the value `b` in `Right(b)` or `Both(a, b)` variants. /// If it is present rewrapping the result in `self`'s original variant. pubfn map_right<F, M>(self, f: F) -> EitherOrBoth<A, M> where
F: FnOnce(B) -> M,
{ matchself {
Left(a) => Left(a),
Right(b) => Right(f(b)),
Both(a, b) => Both(a, f(b)),
}
}
/// Apply the functions `f` and `g` on the value `a` and `b` respectively; /// found in `Left(a)`, `Right(b)`, or `Both(a, b)` variants. /// The Result is rewrapped `self`'s original variant. pubfn map_any<F, L, G, R>(self, f: F, g: G) -> EitherOrBoth<L, R> where
F: FnOnce(A) -> L,
G: FnOnce(B) -> R,
{ matchself {
Left(a) => Left(f(a)),
Right(b) => Right(g(b)),
Both(a, b) => Both(f(a), g(b)),
}
}
/// Apply the function `f` on the value `a` in `Left(a)` or `Both(a, _)` variants if it is /// present. pubfn left_and_then<F, L>(self, f: F) -> EitherOrBoth<L, B> where
F: FnOnce(A) -> EitherOrBoth<L, B>,
{ matchself {
Left(a) | Both(a, _) => f(a),
Right(b) => Right(b),
}
}
/// Apply the function `f` on the value `b` /// in `Right(b)` or `Both(_, b)` variants if it is present. pubfn right_and_then<F, R>(self, f: F) -> EitherOrBoth<A, R> where
F: FnOnce(B) -> EitherOrBoth<A, R>,
{ matchself {
Left(a) => Left(a),
Right(b) | Both(_, b) => f(b),
}
}
/// Returns a tuple consisting of the `l` and `r` in `Both(l, r)`, if present. /// Otherwise, returns the wrapped value for the present element, and the supplied /// value for the other. The first (`l`) argument is used for a missing `Left` /// value. The second (`r`) argument is used for a missing `Right` value. /// /// Arguments passed to `or` are eagerly evaluated; if you are passing /// the result of a function call, it is recommended to use [`or_else`], /// which is lazily evaluated. /// /// [`or_else`]: EitherOrBoth::or_else /// /// # Examples /// /// ``` /// # use itertools::EitherOrBoth; /// assert_eq!(EitherOrBoth::Both("tree", 1).or("stone", 5), ("tree", 1)); /// assert_eq!(EitherOrBoth::Left("tree").or("stone", 5), ("tree", 5)); /// assert_eq!(EitherOrBoth::Right(1).or("stone", 5), ("stone", 1)); /// ``` pubfn or(self, l: A, r: B) -> (A, B) { matchself {
Left(inner_l) => (inner_l, r),
Right(inner_r) => (l, inner_r),
Both(inner_l, inner_r) => (inner_l, inner_r),
}
}
/// Returns a tuple consisting of the `l` and `r` in `Both(l, r)`, if present. /// Otherwise, returns the wrapped value for the present element, and the [`default`](Default::default) /// for the other. pubfn or_default(self) -> (A, B) where
A: Default,
B: Default,
{ matchself {
EitherOrBoth::Left(l) => (l, B::default()),
EitherOrBoth::Right(r) => (A::default(), r),
EitherOrBoth::Both(l, r) => (l, r),
}
}
/// Returns a tuple consisting of the `l` and `r` in `Both(l, r)`, if present. /// Otherwise, returns the wrapped value for the present element, and computes the /// missing value with the supplied closure. The first argument (`l`) is used for a /// missing `Left` value. The second argument (`r`) is used for a missing `Right` value. /// /// # Examples /// /// ``` /// # use itertools::EitherOrBoth; /// let k = 10; /// assert_eq!(EitherOrBoth::Both("tree", 1).or_else(|| "stone", || 2 * k), ("tree", 1)); /// assert_eq!(EitherOrBoth::Left("tree").or_else(|| "stone", || 2 * k), ("tree", 20)); /// assert_eq!(EitherOrBoth::Right(1).or_else(|| "stone", || 2 * k), ("stone", 1)); /// ``` pubfn or_else<L: FnOnce() -> A, R: FnOnce() -> B>(self, l: L, r: R) -> (A, B) { matchself {
Left(inner_l) => (inner_l, r()),
Right(inner_r) => (l(), inner_r),
Both(inner_l, inner_r) => (inner_l, inner_r),
}
}
}
impl<T> EitherOrBoth<T, T> { /// Return either value of left, right, or the product of `f` applied where `Both` are present. pubfn reduce<F>(self, f: F) -> T where
F: FnOnce(T, T) -> T,
{ matchself {
Left(a) => a,
Right(b) => b,
Both(a, b) => f(a, b),
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.