diff --git a/src/lib.rs b/src/lib.rs index e2265eb..8b666a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -906,6 +906,56 @@ impl Either { } } + /// Calls a function with a reference to the contained value if [`Left`]. + /// + /// Returns the original self. + /// + /// # Examples + /// + /// ``` + /// use either::*; + /// + /// # fn foo() -> Either { Right(2) } + /// let x = foo() + /// .inspect_left(|n| println!("left: {n}")) + /// .left_or(0); + /// ``` + pub fn inspect_left(self, f: F) -> Self + where + F: FnOnce(&L), + { + if let Left(ref left) = self { + f(left); + } + + self + } + + /// Calls a function with a reference to the contained value if [`Right`]. + /// + /// Returns the original self. + /// + /// # Examples + /// + /// ``` + /// use either::*; + /// + /// # fn foo() -> Either { Right(2) } + /// let x = foo() + /// .inspect_right(|n| println!("right: {n}")) + /// .left_or(0); + /// ``` + pub fn inspect_right(self, f: F) -> Self + where + F: FnOnce(&R), + { + if let Right(ref right) = self { + f(right); + } + + self + } + /// Convert the contained value into `T` /// /// # Examples