Skip to content

Commit

Permalink
Add flatten_left and flatten_right
Browse files Browse the repository at this point in the history
  • Loading branch information
CMDJojo committed May 2, 2023
1 parent b0d9a95 commit e86493d
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,26 @@
#[cfg(any(test, feature = "use_std"))]
extern crate std;

#[cfg(feature = "serde")]
pub mod serde_untagged;

#[cfg(feature = "serde")]
pub mod serde_untagged_optional;

use core::convert::{AsMut, AsRef};
use core::convert::{identity, AsMut, AsRef};
use core::fmt;
use core::future::Future;
use core::iter;
use core::ops::Deref;
use core::ops::DerefMut;
use core::pin::Pin;

#[cfg(any(test, feature = "use_std"))]
use std::error::Error;
#[cfg(any(test, feature = "use_std"))]
use std::io::{self, BufRead, Read, Seek, SeekFrom, Write};

pub use crate::Either::{Left, Right};

#[cfg(feature = "serde")]
pub mod serde_untagged;

#[cfg(feature = "serde")]
pub mod serde_untagged_optional;

/// The enum `Either` with variants `Left` and `Right` is a general purpose
/// sum type with two cases.
///
Expand Down Expand Up @@ -858,6 +857,38 @@ impl<T, L, R> Either<(L, T), (R, T)> {
}
}

impl<L, R> Either<Either<L, R>, R> {
/// Flattens a nested structure where the left value type is an [`Either`].
///
/// ```
/// use either::*;
/// let left: Either<Either<&str, u64>, u64> = Left(Left("foo"));
/// assert_eq!(left.flatten_left(), Left("foo"));
///
/// let right: Either<Either<&str, u64>, u64> = Right(30);
/// assert_eq!(right.flatten_left(), Right(30));
/// ```
pub fn flatten_left(self) -> Either<L, R> {
self.either(identity, Right)
}
}

impl<L, R> Either<L, Either<L, R>> {
/// Flattens a nested structure where the right value type is an [`Either`].
///
/// ```
/// use either::*;
/// let right: Either<&str, Either<&str, u64>> = Right(Right(10));
/// assert_eq!(right.flatten_right(), Right(10));
///
/// let left: Either<&str, Either<&str, u64>> = Left("foo");
/// assert_eq!(left.flatten_right(), Left("foo"));
/// ```
pub fn flatten_right(self) -> Either<L, R> {
self.either(Left, identity)
}
}

impl<T> Either<T, T> {
/// Extract the value of an either over two equivalent types.
///
Expand Down

0 comments on commit e86493d

Please sign in to comment.