Content-Length: 27503 | pFad | https://docs.rs/either/1/src/either/into_either.rs.html#29

into_either.rs - source

either/
into_either.rs

1//! The trait [`IntoEither`] provides methods for converting a type `Self`, whose
2//! size is constant and known at compile-time, into an [`Either`] variant.
3
4use super::{Either, Left, Right};
5
6//docs.rs/ Provides methods for converting a type `Self` into either a [`Left`] or [`Right`]
7//docs.rs/ variant of [`Either<Self, Self>`](Either).
8//docs.rs/
9//docs.rs/ The [`into_either`](IntoEither::into_either) method takes a [`bool`] to determine
10//docs.rs/ whether to convert to [`Left`] or [`Right`].
11//docs.rs/
12//docs.rs/ The [`into_either_with`](IntoEither::into_either_with) method takes a
13//docs.rs/ [predicate function](FnOnce) to determine whether to convert to [`Left`] or [`Right`].
14pub trait IntoEither: Sized {
15    //docs.rs/ Converts `self` into a [`Left`] variant of [`Either<Self, Self>`](Either)
16    //docs.rs/ if `into_left` is `true`.
17    //docs.rs/ Converts `self` into a [`Right`] variant of [`Either<Self, Self>`](Either)
18    //docs.rs/ otherwise.
19    //docs.rs/
20    //docs.rs/ # Examples
21    //docs.rs/
22    //docs.rs/ ```
23    //docs.rs/ use either::{IntoEither, Left, Right};
24    //docs.rs/
25    //docs.rs/ let x = 0;
26    //docs.rs/ assert_eq!(x.into_either(true), Left(x));
27    //docs.rs/ assert_eq!(x.into_either(false), Right(x));
28    //docs.rs/ ```
29    fn into_either(self, into_left: bool) -> Either<Self, Self> {
30        if into_left {
31            Left(self)
32        } else {
33            Right(self)
34        }
35    }
36
37    //docs.rs/ Converts `self` into a [`Left`] variant of [`Either<Self, Self>`](Either)
38    //docs.rs/ if `into_left(&self)` returns `true`.
39    //docs.rs/ Converts `self` into a [`Right`] variant of [`Either<Self, Self>`](Either)
40    //docs.rs/ otherwise.
41    //docs.rs/
42    //docs.rs/ # Examples
43    //docs.rs/
44    //docs.rs/ ```
45    //docs.rs/ use either::{IntoEither, Left, Right};
46    //docs.rs/
47    //docs.rs/ fn is_even(x: &u8) -> bool {
48    //docs.rs/     x % 2 == 0
49    //docs.rs/ }
50    //docs.rs/
51    //docs.rs/ let x = 0;
52    //docs.rs/ assert_eq!(x.into_either_with(is_even), Left(x));
53    //docs.rs/ assert_eq!(x.into_either_with(|x| !is_even(x)), Right(x));
54    //docs.rs/ ```
55    fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
56    where
57        F: FnOnce(&Self) -> bool,
58    {
59        let into_left = into_left(&self);
60        self.into_either(into_left)
61    }
62}
63
64impl<T> IntoEither for T {}








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://docs.rs/either/1/src/either/into_either.rs.html#29

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy