forked from mirror/async-std
Merge branch 'master' into fs-stream-find-map
This commit is contained in:
commit
efb8415429
13 changed files with 266 additions and 57 deletions
31
.travis.yml
31
.travis.yml
|
@ -1,6 +1,16 @@
|
||||||
language: rust
|
language: rust
|
||||||
|
|
||||||
env: RUSTFLAGS="-D warnings"
|
env:
|
||||||
|
- RUSTFLAGS="-D warnings"
|
||||||
|
|
||||||
|
# Cache the whole `~/.cargo` directory to keep `~/cargo/.crates.toml`.
|
||||||
|
cache:
|
||||||
|
directories:
|
||||||
|
- /home/travis/.cargo
|
||||||
|
|
||||||
|
# Don't cache the cargo registry because it's too big.
|
||||||
|
before_cache:
|
||||||
|
- rm -rf /home/travis/.cargo/registry
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
fast_finish: true
|
fast_finish: true
|
||||||
|
@ -35,16 +45,15 @@ matrix:
|
||||||
script:
|
script:
|
||||||
- cargo doc --features docs
|
- cargo doc --features docs
|
||||||
|
|
||||||
# TODO(yoshuawuyts): re-enable mdbook
|
- name: book
|
||||||
# - name: book
|
rust: nightly
|
||||||
# rust: nightly
|
os: linux
|
||||||
# os: linux
|
before_script:
|
||||||
# before_script:
|
- test -x $HOME/.cargo/bin/mdbook || ./ci/install-mdbook.sh
|
||||||
# - test -x $HOME/.cargo/bin/mdbook || ./ci/install-mdbook.sh
|
- cargo build # to find 'extern crate async_std' by `mdbook test`
|
||||||
# - cargo build # to find 'extern crate async_std' by `mdbook test`
|
script:
|
||||||
# script:
|
- mdbook build docs
|
||||||
# - mdbook build docs
|
- mdbook test -L ./target/debug/deps docs
|
||||||
# - mdbook test -L ./target/debug/deps docs
|
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- cargo check --features unstable --all --benches --bins --examples --tests
|
- cargo check --features unstable --all --benches --bins --examples --tests
|
||||||
|
|
|
@ -44,6 +44,10 @@ femme = "1.2.0"
|
||||||
surf = "1.0.2"
|
surf = "1.0.2"
|
||||||
tempdir = "0.3.7"
|
tempdir = "0.3.7"
|
||||||
|
|
||||||
|
# These are used by the book for examples
|
||||||
|
futures-channel-preview = "0.3.0-alpha.18"
|
||||||
|
futures-util-preview = "0.3.0-alpha.18"
|
||||||
|
|
||||||
[dev-dependencies.futures-preview]
|
[dev-dependencies.futures-preview]
|
||||||
version = "0.3.0-alpha.18"
|
version = "0.3.0-alpha.18"
|
||||||
features = ["std", "nightly", "async-await"]
|
features = ["std", "nightly", "async-await"]
|
||||||
|
|
|
@ -4,17 +4,16 @@ At this point, we only need to start the broker to get a fully-functioning (in t
|
||||||
|
|
||||||
```rust,edition2018
|
```rust,edition2018
|
||||||
# extern crate async_std;
|
# extern crate async_std;
|
||||||
# extern crate futures;
|
# extern crate futures_channel;
|
||||||
|
# extern crate futures_util;
|
||||||
use async_std::{
|
use async_std::{
|
||||||
io::{self, BufReader},
|
io::{self, BufReader},
|
||||||
net::{TcpListener, TcpStream, ToSocketAddrs},
|
net::{TcpListener, TcpStream, ToSocketAddrs},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
task,
|
task,
|
||||||
};
|
};
|
||||||
use futures::{
|
use futures_channel::mpsc;
|
||||||
channel::mpsc,
|
use futures_util::SinkExt;
|
||||||
SinkExt,
|
|
||||||
};
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::hash_map::{HashMap, Entry},
|
collections::hash_map::{HashMap, Entry},
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
|
|
|
@ -22,17 +22,16 @@ Let's add waiting to the server:
|
||||||
|
|
||||||
```rust,edition2018
|
```rust,edition2018
|
||||||
# extern crate async_std;
|
# extern crate async_std;
|
||||||
# extern crate futures;
|
# extern crate futures_channel;
|
||||||
|
# extern crate futures_util;
|
||||||
# use async_std::{
|
# use async_std::{
|
||||||
# io::{self, BufReader},
|
# io::{self, BufReader},
|
||||||
# net::{TcpListener, TcpStream, ToSocketAddrs},
|
# net::{TcpListener, TcpStream, ToSocketAddrs},
|
||||||
# prelude::*,
|
# prelude::*,
|
||||||
# task,
|
# task,
|
||||||
# };
|
# };
|
||||||
# use futures::{
|
# use futures_channel::mpsc;
|
||||||
# channel::mpsc,
|
# use futures_util::SinkExt;
|
||||||
# SinkExt,
|
|
||||||
# };
|
|
||||||
# use std::{
|
# use std::{
|
||||||
# collections::hash_map::{HashMap, Entry},
|
# collections::hash_map::{HashMap, Entry},
|
||||||
# sync::Arc,
|
# sync::Arc,
|
||||||
|
@ -156,17 +155,16 @@ And to the broker:
|
||||||
|
|
||||||
```rust,edition2018
|
```rust,edition2018
|
||||||
# extern crate async_std;
|
# extern crate async_std;
|
||||||
# extern crate futures;
|
# extern crate futures_channel;
|
||||||
|
# extern crate futures_util;
|
||||||
# use async_std::{
|
# use async_std::{
|
||||||
# io::{self, BufReader},
|
# io::{self, BufReader},
|
||||||
# net::{TcpListener, TcpStream, ToSocketAddrs},
|
# net::{TcpListener, TcpStream, ToSocketAddrs},
|
||||||
# prelude::*,
|
# prelude::*,
|
||||||
# task,
|
# task,
|
||||||
# };
|
# };
|
||||||
# use futures::{
|
# use futures_channel::mpsc;
|
||||||
# channel::mpsc,
|
# use futures_util::SinkExt;
|
||||||
# SinkExt,
|
|
||||||
# };
|
|
||||||
# use std::{
|
# use std::{
|
||||||
# collections::hash_map::{HashMap, Entry},
|
# collections::hash_map::{HashMap, Entry},
|
||||||
# sync::Arc,
|
# sync::Arc,
|
||||||
|
|
|
@ -12,15 +12,16 @@ The order of events "Bob sends message to Alice" and "Alice joins" is determined
|
||||||
|
|
||||||
```rust,edition2018
|
```rust,edition2018
|
||||||
# extern crate async_std;
|
# extern crate async_std;
|
||||||
# extern crate futures;
|
# extern crate futures_channel;
|
||||||
|
# extern crate futures_util;
|
||||||
# use async_std::{
|
# use async_std::{
|
||||||
# io::{Write},
|
# io::{Write},
|
||||||
# net::TcpStream,
|
# net::TcpStream,
|
||||||
# prelude::{Future, Stream},
|
# prelude::{Future, Stream},
|
||||||
# task,
|
# task,
|
||||||
# };
|
# };
|
||||||
# use futures::channel::mpsc;
|
# use futures_channel::mpsc;
|
||||||
# use futures::sink::SinkExt;
|
# use futures_util::sink::SinkExt;
|
||||||
# use std::sync::Arc;
|
# use std::sync::Arc;
|
||||||
#
|
#
|
||||||
# type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
# type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
||||||
|
|
|
@ -19,9 +19,11 @@ First, let's add a shutdown channel to the `client`:
|
||||||
|
|
||||||
```rust,edition2018
|
```rust,edition2018
|
||||||
# extern crate async_std;
|
# extern crate async_std;
|
||||||
# extern crate futures;
|
# extern crate futures_channel;
|
||||||
|
# extern crate futures_util;
|
||||||
# use async_std::net::TcpStream;
|
# use async_std::net::TcpStream;
|
||||||
# use futures::{channel::mpsc, SinkExt};
|
# use futures_channel::mpsc;
|
||||||
|
# use futures_util::SinkExt;
|
||||||
# use std::sync::Arc;
|
# use std::sync::Arc;
|
||||||
#
|
#
|
||||||
# type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
# type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
||||||
|
@ -68,9 +70,11 @@ We use the `select` macro for this purpose:
|
||||||
|
|
||||||
```rust,edition2018
|
```rust,edition2018
|
||||||
# extern crate async_std;
|
# extern crate async_std;
|
||||||
# extern crate futures;
|
# extern crate futures_channel;
|
||||||
|
# extern crate futures_util;
|
||||||
# use async_std::{io::Write, net::TcpStream};
|
# use async_std::{io::Write, net::TcpStream};
|
||||||
use futures::{channel::mpsc, select, FutureExt, StreamExt};
|
use futures_channel::mpsc;
|
||||||
|
use futures_util::{select, FutureExt, StreamExt};
|
||||||
# use std::sync::Arc;
|
# use std::sync::Arc;
|
||||||
|
|
||||||
# type Receiver<T> = mpsc::UnboundedReceiver<T>;
|
# type Receiver<T> = mpsc::UnboundedReceiver<T>;
|
||||||
|
@ -118,15 +122,18 @@ The final code looks like this:
|
||||||
|
|
||||||
```rust,edition2018
|
```rust,edition2018
|
||||||
# extern crate async_std;
|
# extern crate async_std;
|
||||||
# extern crate futures;
|
# extern crate futures_channel;
|
||||||
|
# extern crate futures_util;
|
||||||
use async_std::{
|
use async_std::{
|
||||||
io::{BufReader, BufRead, Write},
|
io::{BufReader, BufRead, Write},
|
||||||
net::{TcpListener, TcpStream, ToSocketAddrs},
|
net::{TcpListener, TcpStream, ToSocketAddrs},
|
||||||
task,
|
task,
|
||||||
};
|
};
|
||||||
use futures::{channel::mpsc, future::Future, select, FutureExt, SinkExt, StreamExt};
|
use futures_channel::mpsc;
|
||||||
|
use futures_util::{select, FutureExt, SinkExt, StreamExt};
|
||||||
use std::{
|
use std::{
|
||||||
collections::hash_map::{Entry, HashMap},
|
collections::hash_map::{Entry, HashMap},
|
||||||
|
future::Future,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -16,13 +16,13 @@ With async, we can just use the `select!` macro.
|
||||||
|
|
||||||
```rust,edition2018
|
```rust,edition2018
|
||||||
# extern crate async_std;
|
# extern crate async_std;
|
||||||
# extern crate futures;
|
# extern crate futures_util;
|
||||||
use async_std::{
|
use async_std::{
|
||||||
io::{stdin, BufRead, BufReader, Write},
|
io::{stdin, BufRead, BufReader, Write},
|
||||||
net::{TcpStream, ToSocketAddrs},
|
net::{TcpStream, ToSocketAddrs},
|
||||||
task,
|
task,
|
||||||
};
|
};
|
||||||
use futures::{select, FutureExt, StreamExt};
|
use futures_util::{select, FutureExt, StreamExt};
|
||||||
|
|
||||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
||||||
|
|
||||||
|
|
|
@ -13,14 +13,15 @@ if Alice and Charley send two messages to Bob at the same time, Bob will see the
|
||||||
|
|
||||||
```rust,edition2018
|
```rust,edition2018
|
||||||
# extern crate async_std;
|
# extern crate async_std;
|
||||||
# extern crate futures;
|
# extern crate futures_channel;
|
||||||
|
# extern crate futures_util;
|
||||||
# use async_std::{
|
# use async_std::{
|
||||||
# io::Write,
|
# io::Write,
|
||||||
# net::TcpStream,
|
# net::TcpStream,
|
||||||
# prelude::Stream,
|
# prelude::Stream,
|
||||||
# };
|
# };
|
||||||
use futures::channel::mpsc; // 1
|
use futures_channel::mpsc; // 1
|
||||||
use futures::sink::SinkExt;
|
use futures_util::sink::SinkExt;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
# type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
# type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;
|
||||||
|
|
|
@ -127,6 +127,8 @@ pub trait Write {
|
||||||
/// #
|
/// #
|
||||||
/// # Ok(()) }) }
|
/// # Ok(()) }) }
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// [`write`]: #tymethod.write
|
||||||
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> ret!('a, WriteAllFuture, io::Result<()>)
|
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> ret!('a, WriteAllFuture, io::Result<()>)
|
||||||
where
|
where
|
||||||
Self: Unpin,
|
Self: Unpin,
|
||||||
|
|
48
src/stream/stream/filter_map.rs
Normal file
48
src/stream/stream/filter_map.rs
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
/// A stream that both filters and maps.
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct FilterMap<S, F, T, B> {
|
||||||
|
stream: S,
|
||||||
|
f: F,
|
||||||
|
__from: PhantomData<T>,
|
||||||
|
__to: PhantomData<B>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, F, T, B> FilterMap<S, F, T, B> {
|
||||||
|
pin_utils::unsafe_pinned!(stream: S);
|
||||||
|
pin_utils::unsafe_unpinned!(f: F);
|
||||||
|
|
||||||
|
pub(crate) fn new(stream: S, f: F) -> Self {
|
||||||
|
FilterMap {
|
||||||
|
stream,
|
||||||
|
f,
|
||||||
|
__from: PhantomData,
|
||||||
|
__to: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S, F, B> futures_core::stream::Stream for FilterMap<S, F, S::Item, B>
|
||||||
|
where
|
||||||
|
S: futures_core::stream::Stream,
|
||||||
|
F: FnMut(S::Item) -> Option<B>,
|
||||||
|
{
|
||||||
|
type Item = B;
|
||||||
|
|
||||||
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
||||||
|
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
|
||||||
|
match next {
|
||||||
|
Some(v) => match (self.as_mut().f())(v) {
|
||||||
|
Some(b) => Poll::Ready(Some(b)),
|
||||||
|
None => {
|
||||||
|
cx.waker().wake_by_ref();
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => Poll::Ready(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,20 +2,20 @@ use std::cmp::Ordering;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
|
||||||
use crate::future::Future;
|
use crate::future::Future;
|
||||||
use crate::stream::Stream;
|
|
||||||
use crate::task::{Context, Poll};
|
use crate::task::{Context, Poll};
|
||||||
|
|
||||||
/// A future that yields the minimum item in a stream by a given comparison function.
|
#[allow(missing_debug_implementations)]
|
||||||
#[derive(Clone, Debug)]
|
pub struct MinByFuture<S, F, T> {
|
||||||
pub struct MinByFuture<S: Stream, F> {
|
|
||||||
stream: S,
|
stream: S,
|
||||||
compare: F,
|
compare: F,
|
||||||
min: Option<S::Item>,
|
min: Option<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Stream + Unpin, F> Unpin for MinByFuture<S, F> {}
|
impl<S, F, T> MinByFuture<S, F, T> {
|
||||||
|
pin_utils::unsafe_pinned!(stream: S);
|
||||||
|
pin_utils::unsafe_unpinned!(compare: F);
|
||||||
|
pin_utils::unsafe_unpinned!(min: Option<T>);
|
||||||
|
|
||||||
impl<S: Stream + Unpin, F> MinByFuture<S, F> {
|
|
||||||
pub(super) fn new(stream: S, compare: F) -> Self {
|
pub(super) fn new(stream: S, compare: F) -> Self {
|
||||||
MinByFuture {
|
MinByFuture {
|
||||||
stream,
|
stream,
|
||||||
|
@ -25,25 +25,25 @@ impl<S: Stream + Unpin, F> MinByFuture<S, F> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, F> Future for MinByFuture<S, F>
|
impl<S, F> Future for MinByFuture<S, F, S::Item>
|
||||||
where
|
where
|
||||||
S: futures_core::stream::Stream + Unpin,
|
S: futures_core::stream::Stream + Unpin + Sized,
|
||||||
S::Item: Copy,
|
S::Item: Copy,
|
||||||
F: FnMut(&S::Item, &S::Item) -> Ordering,
|
F: FnMut(&S::Item, &S::Item) -> Ordering,
|
||||||
{
|
{
|
||||||
type Output = Option<S::Item>;
|
type Output = Option<S::Item>;
|
||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let next = futures_core::ready!(Pin::new(&mut self.stream).poll_next(cx));
|
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
|
||||||
|
|
||||||
match next {
|
match next {
|
||||||
Some(new) => {
|
Some(new) => {
|
||||||
cx.waker().wake_by_ref();
|
cx.waker().wake_by_ref();
|
||||||
match self.as_mut().min.take() {
|
match self.as_mut().min().take() {
|
||||||
None => self.as_mut().min = Some(new),
|
None => *self.as_mut().min() = Some(new),
|
||||||
Some(old) => match (&mut self.as_mut().compare)(&new, &old) {
|
Some(old) => match (&mut self.as_mut().compare())(&new, &old) {
|
||||||
Ordering::Less => self.as_mut().min = Some(new),
|
Ordering::Less => *self.as_mut().min() = Some(new),
|
||||||
_ => self.as_mut().min = Some(old),
|
_ => *self.as_mut().min() = Some(old),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
Poll::Pending
|
Poll::Pending
|
||||||
|
|
|
@ -23,18 +23,22 @@
|
||||||
|
|
||||||
mod all;
|
mod all;
|
||||||
mod any;
|
mod any;
|
||||||
|
mod filter_map;
|
||||||
mod find_map;
|
mod find_map;
|
||||||
mod min_by;
|
mod min_by;
|
||||||
mod next;
|
mod next;
|
||||||
|
mod nth;
|
||||||
mod take;
|
mod take;
|
||||||
|
|
||||||
pub use take::Take;
|
pub use take::Take;
|
||||||
|
|
||||||
use all::AllFuture;
|
use all::AllFuture;
|
||||||
use any::AnyFuture;
|
use any::AnyFuture;
|
||||||
|
use filter_map::FilterMap;
|
||||||
use find_map::FindMapFuture;
|
use find_map::FindMapFuture;
|
||||||
use min_by::MinByFuture;
|
use min_by::MinByFuture;
|
||||||
use next::NextFuture;
|
use next::NextFuture;
|
||||||
|
use nth::NthFuture;
|
||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
@ -132,6 +136,43 @@ pub trait Stream {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Both filters and maps a stream.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// Basic usage:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # fn main() { async_std::task::block_on(async {
|
||||||
|
/// #
|
||||||
|
/// use std::collections::VecDeque;
|
||||||
|
/// use async_std::stream::Stream;
|
||||||
|
///
|
||||||
|
/// let s: VecDeque<&str> = vec!["1", "lol", "3", "NaN", "5"].into_iter().collect();
|
||||||
|
///
|
||||||
|
/// let mut parsed = s.filter_map(|a| a.parse::<u32>().ok());
|
||||||
|
///
|
||||||
|
/// let one = parsed.next().await;
|
||||||
|
/// assert_eq!(one, Some(1));
|
||||||
|
///
|
||||||
|
/// let three = parsed.next().await;
|
||||||
|
/// assert_eq!(three, Some(3));
|
||||||
|
///
|
||||||
|
/// let five = parsed.next().await;
|
||||||
|
/// assert_eq!(five, Some(5));
|
||||||
|
///
|
||||||
|
/// let end = parsed.next().await;
|
||||||
|
/// assert_eq!(end, None);
|
||||||
|
/// #
|
||||||
|
/// # }) }
|
||||||
|
fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F, Self::Item, B>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
F: FnMut(Self::Item) -> Option<B>,
|
||||||
|
{
|
||||||
|
FilterMap::new(self, f)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the element that gives the minimum value with respect to the
|
/// Returns the element that gives the minimum value with respect to the
|
||||||
/// specified comparison function. If several elements are equally minimum,
|
/// specified comparison function. If several elements are equally minimum,
|
||||||
/// the first element is returned. If the stream is empty, `None` is returned.
|
/// the first element is returned. If the stream is empty, `None` is returned.
|
||||||
|
@ -157,14 +198,72 @@ pub trait Stream {
|
||||||
/// #
|
/// #
|
||||||
/// # }) }
|
/// # }) }
|
||||||
/// ```
|
/// ```
|
||||||
fn min_by<F>(self, compare: F) -> MinByFuture<Self, F>
|
fn min_by<F>(self, compare: F) -> MinByFuture<Self, F, Self::Item>
|
||||||
where
|
where
|
||||||
Self: Sized + Unpin,
|
Self: Sized,
|
||||||
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
|
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
|
||||||
{
|
{
|
||||||
MinByFuture::new(self, compare)
|
MinByFuture::new(self, compare)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the nth element of the stream.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// Basic usage:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # fn main() { async_std::task::block_on(async {
|
||||||
|
/// #
|
||||||
|
/// use std::collections::VecDeque;
|
||||||
|
/// use async_std::stream::Stream;
|
||||||
|
///
|
||||||
|
/// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
|
||||||
|
///
|
||||||
|
/// let second = s.nth(1).await;
|
||||||
|
/// assert_eq!(second, Some(2));
|
||||||
|
/// #
|
||||||
|
/// # }) }
|
||||||
|
/// ```
|
||||||
|
/// Calling `nth()` multiple times:
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # fn main() { async_std::task::block_on(async {
|
||||||
|
/// #
|
||||||
|
/// use std::collections::VecDeque;
|
||||||
|
/// use async_std::stream::Stream;
|
||||||
|
///
|
||||||
|
/// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
|
||||||
|
///
|
||||||
|
/// let second = s.nth(0).await;
|
||||||
|
/// assert_eq!(second, Some(1));
|
||||||
|
///
|
||||||
|
/// let second = s.nth(0).await;
|
||||||
|
/// assert_eq!(second, Some(2));
|
||||||
|
/// #
|
||||||
|
/// # }) }
|
||||||
|
/// ```
|
||||||
|
/// Returning `None` if the stream finished before returning `n` elements:
|
||||||
|
/// ```
|
||||||
|
/// # fn main() { async_std::task::block_on(async {
|
||||||
|
/// #
|
||||||
|
/// use std::collections::VecDeque;
|
||||||
|
/// use async_std::stream::Stream;
|
||||||
|
///
|
||||||
|
/// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
|
||||||
|
///
|
||||||
|
/// let fourth = s.nth(4).await;
|
||||||
|
/// assert_eq!(fourth, None);
|
||||||
|
/// #
|
||||||
|
/// # }) }
|
||||||
|
/// ```
|
||||||
|
fn nth(&mut self, n: usize) -> ret!('_, NthFuture, Option<Self::Item>)
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
NthFuture::new(self, n)
|
||||||
|
}
|
||||||
|
|
||||||
/// Tests if every element of the stream matches a predicate.
|
/// Tests if every element of the stream matches a predicate.
|
||||||
///
|
///
|
||||||
/// `all()` takes a closure that returns `true` or `false`. It applies
|
/// `all()` takes a closure that returns `true` or `false`. It applies
|
||||||
|
|
41
src/stream/stream/nth.rs
Normal file
41
src/stream/stream/nth.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
use std::pin::Pin;
|
||||||
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
|
#[allow(missing_debug_implementations)]
|
||||||
|
pub struct NthFuture<'a, S> {
|
||||||
|
stream: &'a mut S,
|
||||||
|
n: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, S> NthFuture<'a, S> {
|
||||||
|
pin_utils::unsafe_pinned!(stream: &'a mut S);
|
||||||
|
pin_utils::unsafe_unpinned!(n: usize);
|
||||||
|
|
||||||
|
pub(crate) fn new(stream: &'a mut S, n: usize) -> Self {
|
||||||
|
NthFuture { stream, n }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, S> futures_core::future::Future for NthFuture<'a, S>
|
||||||
|
where
|
||||||
|
S: futures_core::stream::Stream + Unpin + Sized,
|
||||||
|
{
|
||||||
|
type Output = Option<S::Item>;
|
||||||
|
|
||||||
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
|
use futures_core::stream::Stream;
|
||||||
|
|
||||||
|
let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
|
||||||
|
match next {
|
||||||
|
Some(v) => match self.n {
|
||||||
|
0 => Poll::Ready(Some(v)),
|
||||||
|
_ => {
|
||||||
|
*self.as_mut().n() -= 1;
|
||||||
|
cx.waker().wake_by_ref();
|
||||||
|
Poll::Pending
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => Poll::Ready(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue