diff --git a/src/lib.rs b/src/lib.rs index d7fbf2c..d6e0b18 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -955,6 +955,62 @@ impl Either { } } +impl Either<&L, &R> { + /// Maps an `Either<&L, &R>` to an `Either` by cloning the contents of + /// either branch. + pub fn cloned(self) -> Either + where + L: Clone, + R: Clone, + { + match self { + Self::Left(l) => Either::Left(l.clone()), + Self::Right(r) => Either::Right(r.clone()), + } + } + + /// Maps an `Either<&L, &R>` to an `Either` by copying the contents of + /// either branch. + pub fn copied(self) -> Either + where + L: Copy, + R: Copy, + { + match self { + Self::Left(l) => Either::Left(*l), + Self::Right(r) => Either::Right(*r), + } + } +} + +impl Either<&mut L, &mut R> { + /// Maps an `Either<&mut L, &mut R>` to an `Either` by cloning the contents of + /// either branch. + pub fn cloned(self) -> Either + where + L: Clone, + R: Clone, + { + match self { + Self::Left(l) => Either::Left(l.clone()), + Self::Right(r) => Either::Right(r.clone()), + } + } + + /// Maps an `Either<&L, &R>` to an `Either` by copying the contents of + /// either branch. + pub fn copied(self) -> Either + where + L: Copy, + R: Copy, + { + match self { + Self::Left(l) => Either::Left(*l), + Self::Right(r) => Either::Right(*r), + } + } +} + /// Convert from `Result` to `Either` with `Ok => Right` and `Err => Left`. impl From> for Either { fn from(r: Result) -> Self {