pub enum Either<A, B> {
Left(A),
Right(B),
}Expand description
A type with two possile states, Left or Right.
It represents a disjunctive union.
Variants§
Implementations§
Source§impl<A, B> Either<A, B>
impl<A, B> Either<A, B>
Sourcepub fn branch(choice: bool) -> Either<A, B>
pub fn branch(choice: bool) -> Either<A, B>
Chooses the branch of the either dependent on the boolean provided.
truecreatesLeft.falsecreatesRight.
Sourcepub fn ok_into_left(result: Result<A, B>) -> Either<A, B>
pub fn ok_into_left(result: Result<A, B>) -> Either<A, B>
Sets the left branch from a result assuming Ok, otherwise if Err sets the right branch instead.
Sourcepub fn ok_into_right(result: Result<B, A>) -> Either<A, B>
pub fn ok_into_right(result: Result<B, A>) -> Either<A, B>
Sets the right branch from a result assuming Ok, otherwise if Err sets the left branch instead.
Sourcepub fn left(self) -> Result<A, B>
pub fn left(self) -> Result<A, B>
Moves out of the either, if left then the result is Ok, otherwise the result is an Err.
Sourcepub fn right(self) -> Result<B, A>
pub fn right(self) -> Result<B, A>
Moves out of the either, if right then the result is Ok, otherwise the result is an Err.
Sourcepub fn as_mut(&mut self) -> Either<&mut A, &mut B>
pub fn as_mut(&mut self) -> Either<&mut A, &mut B>
Converts from &mut Either<A, B> to Either<&mut A, &mut B>
Sourcepub fn map_left<C>(self, left: impl FnOnce(A) -> C) -> Either<C, B>
pub fn map_left<C>(self, left: impl FnOnce(A) -> C) -> Either<C, B>
Maps the left value.
This treats Either as a functor in the left type.
Sourcepub fn map_right<C>(self, right: impl FnOnce(B) -> C) -> Either<A, C>
pub fn map_right<C>(self, right: impl FnOnce(B) -> C) -> Either<A, C>
Maps the left value.
This treats Either as a functor in the right type.
Sourcepub fn map_either<C, D>(
self,
left: impl FnOnce(A) -> C,
right: impl FnOnce(B) -> D,
) -> Either<C, D>
pub fn map_either<C, D>( self, left: impl FnOnce(A) -> C, right: impl FnOnce(B) -> D, ) -> Either<C, D>
Maps both values.
This treats Either as a functor in both the left and right types.
Sourcepub fn choose_map<C, D, F: FnOnce(A) -> C, G: FnOnce(B) -> D>(
self,
choice: impl Choice<Left = F, Right = G>,
) -> Either<C, D>
pub fn choose_map<C, D, F: FnOnce(A) -> C, G: FnOnce(B) -> D>( self, choice: impl Choice<Left = F, Right = G>, ) -> Either<C, D>
Composes an either with a choice of functions to produce an either of values.
This sequences the either and choice in a way that is unique to their structures.
Sourcepub fn compose<C, D>(
self,
choice: impl Choice<Left = C, Right = D>,
) -> Either<(A, C), (B, D)>
pub fn compose<C, D>( self, choice: impl Choice<Left = C, Right = D>, ) -> Either<(A, C), (B, D)>
Composes an either with a choice of values to produce an either of values.
This sequences the either and choice in a way that is unique to their structures.
Sourcepub fn bind_left<C>(self, left: impl FnOnce(A) -> Either<C, B>) -> Either<C, B>
pub fn bind_left<C>(self, left: impl FnOnce(A) -> Either<C, B>) -> Either<C, B>
Maps the left if present to create a new either.
This treats Either as a monad in the left type.
Sourcepub fn bind_right<C>(
self,
right: impl FnOnce(B) -> Either<A, C>,
) -> Either<A, C>
pub fn bind_right<C>( self, right: impl FnOnce(B) -> Either<A, C>, ) -> Either<A, C>
Maps the right if present to create a new either.
This treats Either as a monad in the right type.
Sourcepub fn either<C>(
self,
left: impl FnOnce(A) -> C,
right: impl FnOnce(B) -> C,
) -> C
pub fn either<C>( self, left: impl FnOnce(A) -> C, right: impl FnOnce(B) -> C, ) -> C
Handles each case of the either to produce a single value.
Sourcepub fn choose_either<C, F: FnOnce(A) -> C, G: FnOnce(B) -> C>(
self,
choice: impl Choice<Left = F, Right = G>,
) -> C
pub fn choose_either<C, F: FnOnce(A) -> C, G: FnOnce(B) -> C>( self, choice: impl Choice<Left = F, Right = G>, ) -> C
Composes an either with a choice of functions to produce a single value.
The composition of an either with its linear negative annihilates both.