either_future/
lib.rs

1#![no_std]
2//! `EitherFuture` is a `no_std` implementation of `Future<Output = Either<Left, Right>>` for [`Either<LeftFuture, RightFuture>`].
3//!
4//! It is both implemented for [`futures::Future`] (0.1) and [`core::future::Future`].
5//!
6//! The minimum supported rust version (MSRV) is 1.15.0 if default features are disabled and only `futures01` is enabled.
7//! See the different features for their respecitive MSRV.
8//!
9//! ## Features
10//! * `futures01`: Implement [`futures::Future`] with version 0.1 of the [`futures`] library
11//!     * MSRV: 1.15.0 (MSRV of [`futures`] `0.1`)
12//! * `futures03`: Implement Conversions to and from [`futures_util::future::Either`]
13//!     * MSRV: 1.41.0 (MSRV of [futures_util`] `0.3`)
14//! * `std_future`: Implement [`core::future::Future`], enabled by default
15//!     * MSRV: 1.36.0 (where [`core::future::Future`] was introduced to the standard library)
16
17extern crate either;
18
19use core::ops::{Deref, DerefMut};
20use either::Either;
21
22#[cfg(feature = "std_future")]
23mod future;
24#[cfg(feature = "futures01")]
25mod futures01;
26#[cfg(feature = "futures03")]
27mod futures03;
28
29pub struct EitherFuture<Left, Right>(pub Either<Left, Right>);
30
31impl<Left, Right> EitherFuture<Left, Right> {
32	pub fn left(left_future: Left) -> Self {
33		EitherFuture(Either::Left(left_future))
34	}
35
36	pub fn right(right_future: Right) -> Self {
37		EitherFuture(Either::Right(right_future))
38	}
39}
40
41impl<Left, Right> Deref for EitherFuture<Left, Right> {
42	type Target = Either<Left, Right>;
43
44	fn deref(&self) -> &Self::Target {
45		&self.0
46	}
47}
48
49impl<Left, Right> DerefMut for EitherFuture<Left, Right> {
50	fn deref_mut(&mut self) -> &mut Self::Target {
51		&mut self.0
52	}
53}
54
55impl<Left, Right> From<Either<Left, Right>> for EitherFuture<Left, Right> {
56	fn from(either: Either<Left, Right>) -> Self {
57		EitherFuture(either)
58	}
59}
60
61#[cfg(not(feature = "futures03"))]
62impl<Left, Right> Into<Either<Left, Right>> for EitherFuture<Left, Right> {
63	fn into(self) -> Either<Left, Right> {
64		self.0
65	}
66}
67
68#[cfg(feature = "futures03")]
69impl<Left, Right> From<EitherFuture<Left, Right>> for Either<Left, Right> {
70	fn from(either_future: EitherFuture<Left, Right>) -> Either<Left, Right> {
71		either_future.0
72	}
73}