Merge branch 'master' into fs-stream-find-map

pull/174/head
Fedor Sakharov 5 years ago
commit efb8415429
No known key found for this signature in database
GPG Key ID: 93D436E666BF0FEE

@ -1,6 +1,16 @@
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:
fast_finish: true
@ -35,16 +45,15 @@ matrix:
script:
- cargo doc --features docs
# TODO(yoshuawuyts): re-enable mdbook
# - name: book
# rust: nightly
# os: linux
# before_script:
# - test -x $HOME/.cargo/bin/mdbook || ./ci/install-mdbook.sh
# - cargo build # to find 'extern crate async_std' by `mdbook test`
# script:
# - mdbook build docs
# - mdbook test -L ./target/debug/deps docs
- name: book
rust: nightly
os: linux
before_script:
- test -x $HOME/.cargo/bin/mdbook || ./ci/install-mdbook.sh
- cargo build # to find 'extern crate async_std' by `mdbook test`
script:
- mdbook build docs
- mdbook test -L ./target/debug/deps docs
script:
- cargo check --features unstable --all --benches --bins --examples --tests

@ -44,6 +44,10 @@ femme = "1.2.0"
surf = "1.0.2"
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]
version = "0.3.0-alpha.18"
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
# extern crate async_std;
# extern crate futures;
# extern crate futures_channel;
# extern crate futures_util;
use async_std::{
io::{self, BufReader},
net::{TcpListener, TcpStream, ToSocketAddrs},
prelude::*,
task,
};
use futures::{
channel::mpsc,
SinkExt,
};
use futures_channel::mpsc;
use futures_util::SinkExt;
use std::{
collections::hash_map::{HashMap, Entry},
sync::Arc,

@ -22,17 +22,16 @@ Let's add waiting to the server:
```rust,edition2018
# extern crate async_std;
# extern crate futures;
# extern crate futures_channel;
# extern crate futures_util;
# use async_std::{
# io::{self, BufReader},
# net::{TcpListener, TcpStream, ToSocketAddrs},
# prelude::*,
# task,
# };
# use futures::{
# channel::mpsc,
# SinkExt,
# };
# use futures_channel::mpsc;
# use futures_util::SinkExt;
# use std::{
# collections::hash_map::{HashMap, Entry},
# sync::Arc,
@ -156,17 +155,16 @@ And to the broker:
```rust,edition2018
# extern crate async_std;
# extern crate futures;
# extern crate futures_channel;
# extern crate futures_util;
# use async_std::{
# io::{self, BufReader},
# net::{TcpListener, TcpStream, ToSocketAddrs},
# prelude::*,
# task,
# };
# use futures::{
# channel::mpsc,
# SinkExt,
# };
# use futures_channel::mpsc;
# use futures_util::SinkExt;
# use std::{
# collections::hash_map::{HashMap, Entry},
# sync::Arc,

@ -12,15 +12,16 @@ The order of events "Bob sends message to Alice" and "Alice joins" is determined
```rust,edition2018
# extern crate async_std;
# extern crate futures;
# extern crate futures_channel;
# extern crate futures_util;
# use async_std::{
# io::{Write},
# net::TcpStream,
# prelude::{Future, Stream},
# task,
# };
# use futures::channel::mpsc;
# use futures::sink::SinkExt;
# use futures_channel::mpsc;
# use futures_util::sink::SinkExt;
# use std::sync::Arc;
#
# 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
# extern crate async_std;
# extern crate futures;
# extern crate futures_channel;
# extern crate futures_util;
# use async_std::net::TcpStream;
# use futures::{channel::mpsc, SinkExt};
# use futures_channel::mpsc;
# use futures_util::SinkExt;
# use std::sync::Arc;
#
# 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
# extern crate async_std;
# extern crate futures;
# extern crate futures_channel;
# extern crate futures_util;
# 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;
# type Receiver<T> = mpsc::UnboundedReceiver<T>;
@ -118,15 +122,18 @@ The final code looks like this:
```rust,edition2018
# extern crate async_std;
# extern crate futures;
# extern crate futures_channel;
# extern crate futures_util;
use async_std::{
io::{BufReader, BufRead, Write},
net::{TcpListener, TcpStream, ToSocketAddrs},
task,
};
use futures::{channel::mpsc, future::Future, select, FutureExt, SinkExt, StreamExt};
use futures_channel::mpsc;
use futures_util::{select, FutureExt, SinkExt, StreamExt};
use std::{
collections::hash_map::{Entry, HashMap},
future::Future,
sync::Arc,
};

@ -16,13 +16,13 @@ With async, we can just use the `select!` macro.
```rust,edition2018
# extern crate async_std;
# extern crate futures;
# extern crate futures_util;
use async_std::{
io::{stdin, BufRead, BufReader, Write},
net::{TcpStream, ToSocketAddrs},
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>>;

@ -13,14 +13,15 @@ if Alice and Charley send two messages to Bob at the same time, Bob will see the
```rust,edition2018
# extern crate async_std;
# extern crate futures;
# extern crate futures_channel;
# extern crate futures_util;
# use async_std::{
# io::Write,
# net::TcpStream,
# prelude::Stream,
# };
use futures::channel::mpsc; // 1
use futures::sink::SinkExt;
use futures_channel::mpsc; // 1
use futures_util::sink::SinkExt;
use std::sync::Arc;
# type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

@ -127,6 +127,8 @@ pub trait Write {
/// #
/// # Ok(()) }) }
/// ```
///
/// [`write`]: #tymethod.write
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> ret!('a, WriteAllFuture, io::Result<()>)
where
Self: Unpin,

@ -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 crate::future::Future;
use crate::stream::Stream;
use crate::task::{Context, Poll};
/// A future that yields the minimum item in a stream by a given comparison function.
#[derive(Clone, Debug)]
pub struct MinByFuture<S: Stream, F> {
#[allow(missing_debug_implementations)]
pub struct MinByFuture<S, F, T> {
stream: S,
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 {
MinByFuture {
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
S: futures_core::stream::Stream + Unpin,
S: futures_core::stream::Stream + Unpin + Sized,
S::Item: Copy,
F: FnMut(&S::Item, &S::Item) -> Ordering,
{
type Output = Option<S::Item>;
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 {
Some(new) => {
cx.waker().wake_by_ref();
match self.as_mut().min.take() {
None => self.as_mut().min = Some(new),
Some(old) => match (&mut self.as_mut().compare)(&new, &old) {
Ordering::Less => self.as_mut().min = Some(new),
_ => self.as_mut().min = Some(old),
match self.as_mut().min().take() {
None => *self.as_mut().min() = Some(new),
Some(old) => match (&mut self.as_mut().compare())(&new, &old) {
Ordering::Less => *self.as_mut().min() = Some(new),
_ => *self.as_mut().min() = Some(old),
},
}
Poll::Pending

@ -23,18 +23,22 @@
mod all;
mod any;
mod filter_map;
mod find_map;
mod min_by;
mod next;
mod nth;
mod take;
pub use take::Take;
use all::AllFuture;
use any::AnyFuture;
use filter_map::FilterMap;
use find_map::FindMapFuture;
use min_by::MinByFuture;
use next::NextFuture;
use nth::NthFuture;
use std::cmp::Ordering;
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
/// specified comparison function. If several elements are equally minimum,
/// 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
Self: Sized + Unpin,
Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
{
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.
///
/// `all()` takes a closure that returns `true` or `false`. It applies

@ -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…
Cancel
Save