Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flatten_left and flatten_right #84

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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