Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rayon-rs/either
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.10.0
Choose a base ref
...
head repository: rayon-rs/either
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1.11.0
Choose a head ref
  • 6 commits
  • 4 files changed
  • 4 contributors

Commits on Apr 4, 2024

  1. Enable serde feature on playgrounds

    Playground metadata is explicitly inspired by docs.rs so looks very similar, was implemented at rust-lang/rust-playground#326
    masklinn authored Apr 4, 2024
    Copy the full SHA
    365f674 View commit details
  2. Merge pull request #103 from masklinn/patch-1

    Enable `serde` feature on playgrounds
    jswrenn authored Apr 4, 2024
    Copy the full SHA
    d48ab1b View commit details

Commits on Apr 11, 2024

  1. Feat: Implement trait IntoEither

    Add:
    
    + Trait `IntoEither`
      - `into_either`: Conditionally convert any sized type into any variant of `Either`.
      - `into_either_with`: Like `into_either`, but takes a predicate function instead.
    + Impl `IntoEither` for generic type `T`, where `T` is `Sized`
    
    Close #99
    SFM61319 committed Apr 11, 2024
    Copy the full SHA
    d62efa6 View commit details

Commits on Apr 13, 2024

  1. Merge pull request #101 from SFM61319/feat/into-either

    Feat: Implement trait `IntoEither`
    cuviper authored Apr 13, 2024
    Copy the full SHA
    e385751 View commit details
  2. Release 1.11.0

    cuviper committed Apr 13, 2024
    Copy the full SHA
    0bdb583 View commit details
  3. Merge pull request #104 from cuviper/release-1.11.0

    Release 1.11.0
    cuviper authored Apr 13, 2024
    Copy the full SHA
    864eae4 View commit details
Showing with 77 additions and 2 deletions.
  1. +4 −1 Cargo.toml
  2. +6 −1 README.rst
  3. +64 −0 src/into_either.rs
  4. +3 −0 src/lib.rs
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "either"
version = "1.10.0"
version = "1.11.0"
authors = ["bluss"]
edition = "2018"
rust-version = "1.36"
@@ -33,3 +33,6 @@ tag-name = "{{version}}"

[package.metadata.docs.rs]
features = ["serde"]

[package.metadata.playground]
features = ["serde"]
7 changes: 6 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -25,12 +25,17 @@ __ https://docs.rs/either/
How to use with cargo::

[dependencies]
either = "1.10"
either = "1.11"


Recent Changes
--------------

- 1.11.0

- Add new trait ``IntoEither`` that is useful to convert to ``Either`` in method chains,
by @SFM61319 (#101)

- 1.10.0

- Add new methods ``.factor_iter()``, ``.factor_iter_mut()``, and ``.factor_into_iter()``
64 changes: 64 additions & 0 deletions src/into_either.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//! The trait [`IntoEither`] provides methods for converting a type `Self`, whose
//! size is constant and known at compile-time, into an [`Either`] variant.
use super::{Either, Left, Right};

/// Provides methods for converting a type `Self` into either a [`Left`] or [`Right`]
/// variant of [`Either<Self, Self>`](Either).
///
/// The [`into_either`](IntoEither::into_either) method takes a [`bool`] to determine
/// whether to convert to [`Left`] or [`Right`].
///
/// The [`into_either_with`](IntoEither::into_either_with) method takes a
/// [predicate function](FnOnce) to determine whether to convert to [`Left`] or [`Right`].
pub trait IntoEither: Sized {
/// Converts `self` into a [`Left`] variant of [`Either<Self, Self>`](Either)
/// if `into_left` is `true`.
/// Converts `self` into a [`Right`] variant of [`Either<Self, Self>`](Either)
/// otherwise.
///
/// # Examples
///
/// ```
/// use either::{IntoEither, Left, Right};
///
/// let x = 0;
/// assert_eq!(x.into_either(true), Left(x));
/// assert_eq!(x.into_either(false), Right(x));
/// ```
fn into_either(self, into_left: bool) -> Either<Self, Self> {
if into_left {
Left(self)
} else {
Right(self)
}
}

/// Converts `self` into a [`Left`] variant of [`Either<Self, Self>`](Either)
/// if `into_left(&self)` returns `true`.
/// Converts `self` into a [`Right`] variant of [`Either<Self, Self>`](Either)
/// otherwise.
///
/// # Examples
///
/// ```
/// use either::{IntoEither, Left, Right};
///
/// fn is_even(x: &u8) -> bool {
/// x % 2 == 0
/// }
///
/// let x = 0;
/// assert_eq!(x.into_either_with(is_even), Left(x));
/// assert_eq!(x.into_either_with(|x| !is_even(x)), Right(x));
/// ```
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where
F: FnOnce(&Self) -> bool,
{
let into_left = into_left(&self);
self.into_either(into_left)
}
}

impl<T> IntoEither for T {}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -142,6 +142,9 @@ macro_rules! map_either {
mod iterator;
pub use self::iterator::IterEither;

mod into_either;
pub use self::into_either::IntoEither;

impl<L: Clone, R: Clone> Clone for Either<L, R> {
fn clone(&self) -> Self {
match self {