Add Either::map

This commit is contained in:
Nikolai Vazquez
2019-07-28 16:46:18 -07:00
parent b50dc90e01
commit 04b184591b
+20
View File
@@ -581,6 +581,26 @@ impl<T> Either<T, T> {
pub fn into_inner(self) -> T {
either!(self, inner => inner)
}
/// Map `f` over the contained value and return the result in the
/// corresponding variant.
///
/// ```
/// use either::*;
///
/// let value: Either<_, i32> = Right(42);
///
/// let other = value.map(|x| x * 2);
/// assert_eq!(other, Right(84));
/// ```
pub fn map<F, M>(self, f: F) -> Either<M, M>
where F: FnOnce(T) -> M
{
match self {
Left(l) => Left(f(l)),
Right(r) => Right(f(r)),
}
}
}
/// Convert from `Result` to `Either` with `Ok => Right` and `Err => Left`.