Skip to content

Commit f60b960

Browse files
committed
Use our own either type
1 parent 03fb434 commit f60b960

File tree

12 files changed

+193
-166
lines changed

12 files changed

+193
-166
lines changed

futures-core/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ name = "futures_core"
1616

1717
[features]
1818
default = ["std"]
19-
std = ["alloc", "either/use_std"]
19+
std = ["alloc"]
2020
nightly = []
2121
cfg-target-has-atomic = []
2222
alloc = []
2323

2424
[dependencies]
25-
either = { version = "1.4", default-features = false, optional = true }
2625

2726
[dev-dependencies]
2827
futures-preview = { path = "../futures", version = "=0.3.0-alpha.14" }

futures-core/src/stream/mod.rs

-20
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ use core::ops;
44
use core::pin::Pin;
55
use core::task::{Context, Poll};
66

7-
#[cfg(feature = "either")]
8-
use either::Either;
9-
107
mod stream_obj;
118
pub use self::stream_obj::{StreamObj,LocalStreamObj,UnsafeStreamObj};
129

@@ -84,23 +81,6 @@ where
8481
}
8582
}
8683

87-
#[cfg(feature = "either")]
88-
impl<A, B> Stream for Either<A, B>
89-
where A: Stream,
90-
B: Stream<Item = A::Item>
91-
{
92-
type Item = A::Item;
93-
94-
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<A::Item>> {
95-
unsafe {
96-
match Pin::get_unchecked_mut(self) {
97-
Either::Left(a) => Pin::new_unchecked(a).poll_next(cx),
98-
Either::Right(b) => Pin::new_unchecked(b).poll_next(cx),
99-
}
100-
}
101-
}
102-
}
103-
10484
/// A `Stream` or `TryStream` which tracks whether or not the underlying stream
10585
/// should no longer be polled.
10686
///

futures-sink/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ The asynchronous `Sink` trait for the futures-rs library.
1515
name = "futures_sink"
1616

1717
[features]
18-
std = ["alloc", "either/use_std", "futures-core-preview/std", "futures-channel-preview/std"]
18+
std = ["alloc", "futures-core-preview/std", "futures-channel-preview/std"]
1919
default = ["std"]
2020
nightly = ["futures-core-preview/nightly"]
2121
alloc = ["futures-core-preview/alloc"]
2222

2323
[dependencies]
24-
either = { version = "1.4", default-features = false, optional = true }
2524
futures-core-preview = { path = "../futures-core", version = "=0.3.0-alpha.14", default-features = false }
2625
futures-channel-preview = { path = "../futures-channel", version = "=0.3.0-alpha.14", default-features = false }

futures-sink/src/lib.rs

-46
Original file line numberDiff line numberDiff line change
@@ -229,49 +229,3 @@ mod if_alloc {
229229

230230
#[cfg(feature = "alloc")]
231231
pub use self::if_alloc::*;
232-
233-
#[cfg(feature = "either")]
234-
use either::Either;
235-
#[cfg(feature = "either")]
236-
impl<A, B, Item> Sink<Item> for Either<A, B>
237-
where A: Sink<Item>,
238-
B: Sink<Item, SinkError=A::SinkError>,
239-
{
240-
type SinkError = A::SinkError;
241-
242-
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> {
243-
unsafe {
244-
match Pin::get_unchecked_mut(self) {
245-
Either::Left(x) => Pin::new_unchecked(x).poll_ready(cx),
246-
Either::Right(x) => Pin::new_unchecked(x).poll_ready(cx),
247-
}
248-
}
249-
}
250-
251-
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::SinkError> {
252-
unsafe {
253-
match Pin::get_unchecked_mut(self) {
254-
Either::Left(x) => Pin::new_unchecked(x).start_send(item),
255-
Either::Right(x) => Pin::new_unchecked(x).start_send(item),
256-
}
257-
}
258-
}
259-
260-
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> {
261-
unsafe {
262-
match Pin::get_unchecked_mut(self) {
263-
Either::Left(x) => Pin::new_unchecked(x).poll_flush(cx),
264-
Either::Right(x) => Pin::new_unchecked(x).poll_flush(cx),
265-
}
266-
}
267-
}
268-
269-
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> {
270-
unsafe {
271-
match Pin::get_unchecked_mut(self) {
272-
Either::Left(x) => Pin::new_unchecked(x).poll_close(cx),
273-
Either::Right(x) => Pin::new_unchecked(x).poll_close(cx),
274-
}
275-
}
276-
}
277-
}

futures-util/Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Common utilities and extension traits for the futures-rs library.
1515
name = "futures_util"
1616

1717
[features]
18-
std = ["alloc", "futures-core-preview/std", "futures-io-preview/std", "futures-sink-preview/std", "futures-select-macro-preview/std", "either/use_std", "rand", "rand_core", "slab"]
19-
default = ["std", "futures-core-preview/either", "futures-sink-preview/either"]
18+
std = ["alloc", "futures-core-preview/std", "futures-io-preview/std", "futures-sink-preview/std", "futures-select-macro-preview/std", "rand", "rand_core", "slab"]
19+
default = ["std"]
2020
compat = ["std", "futures_01"]
2121
io-compat = ["compat", "tokio-io"]
2222
bench = []
@@ -31,7 +31,6 @@ futures-channel-preview = { path = "../futures-channel", version = "=0.3.0-alpha
3131
futures-io-preview = { path = "../futures-io", version = "=0.3.0-alpha.14", default-features = false }
3232
futures-sink-preview = { path = "../futures-sink", version = "=0.3.0-alpha.14", default-features = false}
3333
futures-select-macro-preview = { path = "../futures-select-macro", version = "=0.3.0-alpha.14", default-features = false }
34-
either = { version = "1.4", default-features = false }
3534
proc-macro-hack = "0.5"
3635
proc-macro-nested = "0.1.2"
3736
rand = { version = "0.6.4", optional = true }

futures-util/src/future/disabled/select.rs

-36
This file was deleted.

futures-util/src/future/either.rs

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
use core::pin::Pin;
2+
use core::task::{Context, Poll};
3+
use futures_core::future::Future;
4+
use futures_core::stream::Stream;
5+
use futures_sink::Sink;
6+
7+
/// Combines two different futures, streams, or sinks having the same associated types into a single
8+
/// type.
9+
#[derive(Debug, Clone)]
10+
pub enum Either<A, B> {
11+
/// First branch of the type
12+
Left(A),
13+
/// Second branch of the type
14+
Right(B),
15+
}
16+
17+
impl<A, B, T> Either<(T, A), (T, B)> {
18+
/// Factor out a homogeneous type from an either of pairs.
19+
///
20+
/// Here, the homogeneous type is the first element of the pairs.
21+
pub fn factor_first(self) -> (T, Either<A, B>) {
22+
match self {
23+
Either::Left((x, a)) => (x, Either::Left(a)),
24+
Either::Right((x, b)) => (x, Either::Right(b)),
25+
}
26+
}
27+
}
28+
29+
impl<A, B, T> Either<(A, T), (B, T)> {
30+
/// Factor out a homogeneous type from an either of pairs.
31+
///
32+
/// Here, the homogeneous type is the second element of the pairs.
33+
pub fn factor_second(self) -> (Either<A, B>, T) {
34+
match self {
35+
Either::Left((a, x)) => (Either::Left(a), x),
36+
Either::Right((b, x)) => (Either::Right(b), x),
37+
}
38+
}
39+
}
40+
41+
impl<T> Either<T, T> {
42+
/// Extract the value of an either over two equivalent types.
43+
pub fn into_inner(self) -> T {
44+
match self {
45+
Either::Left(x) => x,
46+
Either::Right(x) => x,
47+
}
48+
}
49+
}
50+
51+
impl<A, B> Future for Either<A, B>
52+
where
53+
A: Future,
54+
B: Future<Output = A::Output>,
55+
{
56+
type Output = A::Output;
57+
58+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<A::Output> {
59+
unsafe {
60+
match Pin::get_unchecked_mut(self) {
61+
Either::Left(a) => Pin::new_unchecked(a).poll(cx),
62+
Either::Right(b) => Pin::new_unchecked(b).poll(cx),
63+
}
64+
}
65+
}
66+
}
67+
68+
impl<A, B> Stream for Either<A, B>
69+
where
70+
A: Stream,
71+
B: Stream<Item = A::Item>,
72+
{
73+
type Item = A::Item;
74+
75+
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<A::Item>> {
76+
unsafe {
77+
match Pin::get_unchecked_mut(self) {
78+
Either::Left(a) => Pin::new_unchecked(a).poll_next(cx),
79+
Either::Right(b) => Pin::new_unchecked(b).poll_next(cx),
80+
}
81+
}
82+
}
83+
}
84+
85+
impl<A, B, Item> Sink<Item> for Either<A, B>
86+
where
87+
A: Sink<Item>,
88+
B: Sink<Item, SinkError = A::SinkError>,
89+
{
90+
type SinkError = A::SinkError;
91+
92+
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> {
93+
unsafe {
94+
match Pin::get_unchecked_mut(self) {
95+
Either::Left(x) => Pin::new_unchecked(x).poll_ready(cx),
96+
Either::Right(x) => Pin::new_unchecked(x).poll_ready(cx),
97+
}
98+
}
99+
}
100+
101+
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::SinkError> {
102+
unsafe {
103+
match Pin::get_unchecked_mut(self) {
104+
Either::Left(x) => Pin::new_unchecked(x).start_send(item),
105+
Either::Right(x) => Pin::new_unchecked(x).start_send(item),
106+
}
107+
}
108+
}
109+
110+
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> {
111+
unsafe {
112+
match Pin::get_unchecked_mut(self) {
113+
Either::Left(x) => Pin::new_unchecked(x).poll_flush(cx),
114+
Either::Right(x) => Pin::new_unchecked(x).poll_flush(cx),
115+
}
116+
}
117+
}
118+
119+
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> {
120+
unsafe {
121+
match Pin::get_unchecked_mut(self) {
122+
Either::Left(x) => Pin::new_unchecked(x).poll_close(cx),
123+
Either::Right(x) => Pin::new_unchecked(x).poll_close(cx),
124+
}
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)