#[derive(Clone, Copy, Debug)] pub enum Either { Left(L), Right(R), } impl Either { pub fn left(&self) -> Option<&L> { match self { Either::Left(l) => Some(l), _ => None, } } pub fn right(&self) -> Option<&R> { match self { Either::Right(r) => Some(r), _ => None, } } pub fn left_mut(&mut self) -> Option<&mut L> { match self { Either::Left(l) => Some(l), _ => None, } } pub fn right_mut(&mut self) -> Option<&mut R> { match self { Either::Right(r) => Some(r), _ => None, } } pub fn is_left_and bool>(&self, f: F) -> bool { self.left().map(f).unwrap_or(false) } pub fn is_right_and bool>(&self, f: F) -> bool { self.right().map(f).unwrap_or(false) } pub fn into_left(self) -> Option { match self { Either::Left(l) => Some(l), _ => None, } } pub fn into_right(self) -> Option { match self { Either::Right(r) => Some(r), _ => None, } } pub fn left_or_err E>(self, f: F) -> Result { match self { Either::Left(l) => Ok(l), _ => Err(f()), } } pub fn right_or_err E>(self, f: F) -> Result { match self { Either::Right(r) => Ok(r), _ => Err(f()), } } }