From e51fb7c1416ed3438d76461dddeab148dd107645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Avi=20=D7=93?= Date: Sun, 23 Dec 2018 09:28:13 -0500 Subject: [PATCH] Add and_then methods for EitherOrBoth --- src/either_or_both.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/either_or_both.rs b/src/either_or_both.rs index f242a7846..5c5b711b6 100644 --- a/src/either_or_both.rs +++ b/src/either_or_both.rs @@ -132,4 +132,28 @@ impl EitherOrBoth { Both(a, b) => Both(f(a), g(b)), } } + + /// Apply the function `f` on the value `b` in `Right(b)` or `Both(a, _)` variants if it is + /// present. + pub fn left_and_then(self, f: F) -> EitherOrBoth + where + F: FnOnce(A) -> EitherOrBoth, + { + match self { + Left(a) | Both(a, _) => f(a), + Right(b) => Right(b), + } + } + + /// Apply the function `f` on the value `a` + /// in `Left(a)` or `Both(a, _)` variants if it is present. + pub fn right_and_then(self, f: F) -> EitherOrBoth + where + F: FnOnce(B) -> EitherOrBoth, + { + match self { + Left(a) => Left(a), + Right(b) | Both(_, b) => f(b), + } + } }