Skip to content

Commit 2fd102f

Browse files
committed
Use own either type
1 parent 845c71e commit 2fd102f

File tree

11 files changed

+170
-176
lines changed

11 files changed

+170
-176
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 = []
@@ -30,7 +30,6 @@ futures-channel-preview = { path = "../futures-channel", version = "=0.3.0-alpha
3030
futures-io-preview = { path = "../futures-io", version = "=0.3.0-alpha.14", default-features = false }
3131
futures-sink-preview = { path = "../futures-sink", version = "=0.3.0-alpha.14", default-features = false}
3232
futures-select-macro-preview = { path = "../futures-select-macro", version = "=0.3.0-alpha.14", default-features = false }
33-
either = { version = "1.4", default-features = false }
3433
proc-macro-hack = "0.5"
3534
proc-macro-nested = "0.1.2"
3635
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

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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<A, B> Future for Either<A, B>
42+
where
43+
A: Future,
44+
B: Future<Output = A::Output>,
45+
{
46+
type Output = A::Output;
47+
48+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<A::Output> {
49+
unsafe {
50+
match Pin::get_unchecked_mut(self) {
51+
Either::Left(a) => Pin::new_unchecked(a).poll(cx),
52+
Either::Right(b) => Pin::new_unchecked(b).poll(cx),
53+
}
54+
}
55+
}
56+
}
57+
58+
impl<A, B> Stream for Either<A, B>
59+
where
60+
A: Stream,
61+
B: Stream<Item = A::Item>,
62+
{
63+
type Item = A::Item;
64+
65+
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<A::Item>> {
66+
unsafe {
67+
match Pin::get_unchecked_mut(self) {
68+
Either::Left(a) => Pin::new_unchecked(a).poll_next(cx),
69+
Either::Right(b) => Pin::new_unchecked(b).poll_next(cx),
70+
}
71+
}
72+
}
73+
}
74+
75+
impl<A, B, Item> Sink<Item> for Either<A, B>
76+
where
77+
A: Sink<Item>,
78+
B: Sink<Item, SinkError = A::SinkError>,
79+
{
80+
type SinkError = A::SinkError;
81+
82+
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> {
83+
unsafe {
84+
match Pin::get_unchecked_mut(self) {
85+
Either::Left(x) => Pin::new_unchecked(x).poll_ready(cx),
86+
Either::Right(x) => Pin::new_unchecked(x).poll_ready(cx),
87+
}
88+
}
89+
}
90+
91+
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::SinkError> {
92+
unsafe {
93+
match Pin::get_unchecked_mut(self) {
94+
Either::Left(x) => Pin::new_unchecked(x).start_send(item),
95+
Either::Right(x) => Pin::new_unchecked(x).start_send(item),
96+
}
97+
}
98+
}
99+
100+
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> {
101+
unsafe {
102+
match Pin::get_unchecked_mut(self) {
103+
Either::Left(x) => Pin::new_unchecked(x).poll_flush(cx),
104+
Either::Right(x) => Pin::new_unchecked(x).poll_flush(cx),
105+
}
106+
}
107+
}
108+
109+
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::SinkError>> {
110+
unsafe {
111+
match Pin::get_unchecked_mut(self) {
112+
Either::Left(x) => Pin::new_unchecked(x).poll_close(cx),
113+
Either::Right(x) => Pin::new_unchecked(x).poll_close(cx),
114+
}
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)