Skip to content

Commit 58623a9

Browse files
taiki-ecramertj
authored andcommitted
Change StreamExt::select to a function
1 parent 4c3acb0 commit 58623a9

File tree

4 files changed

+20
-28
lines changed

4 files changed

+20
-28
lines changed

futures-util/src/stream/mod.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ mod poll_fn;
7979
pub use self::poll_fn::{poll_fn, PollFn};
8080

8181
mod select;
82-
pub use self::select::Select;
82+
pub use self::select::{select, Select};
8383

8484
mod skip;
8585
pub use self::skip::Skip;
@@ -1014,23 +1014,6 @@ pub trait StreamExt: Stream {
10141014
Chunks::new(self, capacity)
10151015
}
10161016

1017-
/// This combinator will attempt to pull items from both streams. Each
1018-
/// stream will be polled in a round-robin fashion, and whenever a stream is
1019-
/// ready to yield an item that item is yielded.
1020-
///
1021-
/// After one of the two input stream completes, the remaining one will be
1022-
/// polled exclusively. The returned stream completes when both input
1023-
/// streams have completed.
1024-
///
1025-
/// Note that this method consumes both streams and returns a wrapped
1026-
/// version of them.
1027-
fn select<St>(self, other: St) -> Select<Self, St>
1028-
where St: Stream<Item = Self::Item>,
1029-
Self: Sized,
1030-
{
1031-
Select::new(self, other)
1032-
}
1033-
10341017
/// A future that completes after the given stream has been fully processed
10351018
/// into the sink and the sink has been flushed and closed.
10361019
///

futures-util/src/stream/select.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::pin::Pin;
33
use futures_core::stream::{FusedStream, Stream};
44
use futures_core::task::{Context, Poll};
55

6-
/// Stream for the [`select`](super::StreamExt::select) method.
6+
/// Stream for the [`select`] function.
77
#[derive(Debug)]
88
#[must_use = "streams do nothing unless polled"]
99
pub struct Select<St1, St2> {
@@ -14,16 +14,24 @@ pub struct Select<St1, St2> {
1414

1515
impl<St1: Unpin, St2: Unpin> Unpin for Select<St1, St2> {}
1616

17-
impl<St1, St2> Select<St1, St2>
17+
/// This function will attempt to pull items from both streams. Each
18+
/// stream will be polled in a round-robin fashion, and whenever a stream is
19+
/// ready to yield an item that item is yielded.
20+
///
21+
/// After one of the two input stream completes, the remaining one will be
22+
/// polled exclusively. The returned stream completes when both input
23+
/// streams have completed.
24+
///
25+
/// Note that this function consumes both streams and returns a wrapped
26+
/// version of them.
27+
pub fn select<St1, St2>(stream1: St1, stream2: St2) -> Select<St1, St2>
1828
where St1: Stream,
1929
St2: Stream<Item = St1::Item>
2030
{
21-
pub(super) fn new(stream1: St1, stream2: St2) -> Select<St1, St2> {
22-
Select {
23-
stream1: stream1.fuse(),
24-
stream2: stream2.fuse(),
25-
flag: false,
26-
}
31+
Select {
32+
stream1: stream1.fuse(),
33+
stream2: stream2.fuse(),
34+
flag: false,
2735
}
2836

2937
/// Acquires a reference to the underlying streams that this combinator is

futures/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,12 +344,13 @@ pub mod stream {
344344
empty, Empty,
345345
once, Once,
346346
poll_fn, PollFn,
347+
select, Select,
347348
unfold, Unfold,
348349

349350
StreamExt,
350351
Chain, Collect, Concat, Enumerate, Filter, FilterMap, Flatten, Fold,
351352
Forward, ForEach, Fuse, StreamFuture, Inspect, Map, Next,
352-
SelectNextSome, Peekable, Select, Skip, SkipWhile, Take, TakeWhile,
353+
SelectNextSome, Peekable, Skip, SkipWhile, Take, TakeWhile,
353354
Then, Zip
354355
};
355356

futures/tests/stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn select() {
1212
fn select_and_compare(a: Vec<u32>, b: Vec<u32>, expected: Vec<u32>) {
1313
let a = stream::iter(a);
1414
let b = stream::iter(b);
15-
let vec = block_on(a.select(b).collect::<Vec<_>>());
15+
let vec = block_on(stream::select(a, b).collect::<Vec<_>>());
1616
assert_eq!(vec, expected);
1717
}
1818

0 commit comments

Comments
 (0)