futures_util_either/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2
3#[cfg(feature = "alloc")]
4extern crate alloc;
5
6#[cfg(any(feature = "futures_io", feature = "tokio_io"))]
7use core::pin::Pin;
8
9#[cfg(feature = "futures_io")]
10mod impl_futures_io;
11#[cfg(feature = "std")]
12mod impl_std_io;
13#[cfg(feature = "tokio_io")]
14mod impl_tokio_io;
15
16//
17// Ref https://github.com/rust-lang/futures-rs/blob/0.3.21/futures-util/src/future/either.rs#L27
18#[derive(Debug, Clone)]
19pub enum Either<A, B> {
20    Left(A),
21    Right(B),
22}
23
24impl<A, B> Either<A, B> {
25    #[cfg(feature = "futures-util")]
26    pub fn into_futures_util_either(self) -> futures_util::future::Either<A, B> {
27        match self {
28            Self::Left(a) => futures_util::future::Either::Left(a),
29            Self::Right(b) => futures_util::future::Either::Right(b),
30        }
31    }
32
33    #[cfg(feature = "futures-util")]
34    pub fn from_futures_util_either(either: futures_util::future::Either<A, B>) -> Self {
35        match either {
36            futures_util::future::Either::Left(a) => Self::Left(a),
37            futures_util::future::Either::Right(b) => Self::Right(b),
38        }
39    }
40
41    #[cfg(feature = "either")]
42    pub fn into_either(self) -> either::Either<A, B> {
43        match self {
44            Self::Left(a) => either::Either::Left(a),
45            Self::Right(b) => either::Either::Right(b),
46        }
47    }
48
49    #[cfg(feature = "either")]
50    pub fn from_either(either: either::Either<A, B>) -> Self {
51        match either {
52            either::Either::Left(a) => Self::Left(a),
53            either::Either::Right(b) => Self::Right(b),
54        }
55    }
56}
57
58// Ref https://github.com/rust-lang/futures-rs/blob/0.3.21/futures-util/src/future/either.rs#L35
59#[cfg(any(feature = "futures_io", feature = "tokio_io"))]
60impl<A, B> Either<A, B> {
61    fn project(self: Pin<&mut Self>) -> Either<Pin<&mut A>, Pin<&mut B>> {
62        unsafe {
63            match self.get_unchecked_mut() {
64                Either::Left(a) => Either::Left(Pin::new_unchecked(a)),
65                Either::Right(b) => Either::Right(Pin::new_unchecked(b)),
66            }
67        }
68    }
69}