Merge pull request #1006 from nnethercote/rm-extension_trait

Remove `extension_trait`
pull/1009/head
Yosh 3 years ago committed by GitHub
commit 7e455db4f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -20,123 +20,14 @@ cfg_unstable_default! {
use crate::future::timeout::TimeoutFuture;
}
extension_trait! {
use core::pin::Pin;
use core::ops::{Deref, DerefMut};
use crate::task::{Context, Poll};
#[doc = r#"
A future represents an asynchronous computation.
A future is a value that may not have finished computing yet. This kind of
"asynchronous value" makes it possible for a thread to continue doing useful
work while it waits for the value to become available.
The [provided methods] do not really exist in the trait itself, but they become
available when [`FutureExt`] from the [prelude] is imported:
```
# #[allow(unused_imports)]
use async_std::prelude::*;
```
# The `poll` method
The core method of future, `poll`, *attempts* to resolve the future into a
final value. This method does not block if the value is not ready. Instead,
the current task is scheduled to be woken up when it's possible to make
further progress by `poll`ing again. The `context` passed to the `poll`
method can provide a [`Waker`], which is a handle for waking up the current
task.
When using a future, you generally won't call `poll` directly, but instead
`.await` the value.
[`Waker`]: ../task/struct.Waker.html
[provided methods]: #provided-methods
[`FutureExt`]: ../prelude/trait.FutureExt.html
[prelude]: ../prelude/index.html
"#]
pub trait Future {
#[doc = r#"
The type of value produced on completion.
"#]
type Output;
#[doc = r#"
Attempt to resolve the future to a final value, registering
the current task for wakeup if the value is not yet available.
# Return value
This function returns:
- [`Poll::Pending`] if the future is not ready yet
- [`Poll::Ready(val)`] with the result `val` of this future if it
finished successfully.
Once a future has finished, clients should not `poll` it again.
When a future is not ready yet, `poll` returns `Poll::Pending` and
stores a clone of the [`Waker`] copied from the current [`Context`].
This [`Waker`] is then woken once the future can make progress.
For example, a future waiting for a socket to become
readable would call `.clone()` on the [`Waker`] and store it.
When a signal arrives elsewhere indicating that the socket is readable,
[`Waker::wake`] is called and the socket future's task is awoken.
Once a task has been woken up, it should attempt to `poll` the future
again, which may or may not produce a final value.
Note that on multiple calls to `poll`, only the [`Waker`] from the
[`Context`] passed to the most recent call should be scheduled to
receive a wakeup.
# Runtime characteristics
Futures alone are *inert*; they must be *actively* `poll`ed to make
progress, meaning that each time the current task is woken up, it should
actively re-`poll` pending futures that it still has an interest in.
The `poll` function is not called repeatedly in a tight loop -- instead,
it should only be called when the future indicates that it is ready to
make progress (by calling `wake()`). If you're familiar with the
`poll(2)` or `select(2)` syscalls on Unix it's worth noting that futures
typically do *not* suffer the same problems of "all wakeups must poll
all events"; they are more like `epoll(4)`.
An implementation of `poll` should strive to return quickly, and should
not block. Returning quickly prevents unnecessarily clogging up
threads or event loops. If it is known ahead of time that a call to
`poll` may end up taking awhile, the work should be offloaded to a
thread pool (or something similar) to ensure that `poll` can return
quickly.
# Panics
Once a future has completed (returned `Ready` from `poll`), calling its
`poll` method again may panic, block forever, or cause other kinds of
problems; the `Future` trait places no requirements on the effects of
such a call. However, as the `poll` method is not marked `unsafe`,
Rust's usual rules apply: calls must never cause undefined behavior
(memory corruption, incorrect use of `unsafe` functions, or the like),
regardless of the future's state.
[`Poll::Pending`]: ../task/enum.Poll.html#variant.Pending
[`Poll::Ready(val)`]: ../task/enum.Poll.html#variant.Ready
[`Context`]: ../task/struct.Context.html
[`Waker`]: ../task/struct.Waker.html
[`Waker::wake`]: ../task/struct.Waker.html#method.wake
"#]
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output>;
}
pub use core::future::Future as Future;
#[doc = r#"
Extension methods for [`Future`].
[`Future`]: ../future/trait.Future.html
"#]
pub trait FutureExt: core::future::Future {
pub trait FutureExt: Future {
/// Returns a Future that delays execution for a specified time.
///
/// # Examples
@ -153,7 +44,7 @@ extension_trait! {
/// ```
#[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn delay(self, dur: Duration) -> impl Future<Output = Self::Output> [DelayFuture<Self>]
fn delay(self, dur: Duration) -> DelayFuture<Self>
where
Self: Sized,
{
@ -178,8 +69,7 @@ extension_trait! {
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn flatten(
self,
) -> impl Future<Output = <Self::Output as IntoFuture>::Output>
[FlattenFuture<Self, <Self::Output as IntoFuture>::Future>]
) -> FlattenFuture<Self, <Self::Output as IntoFuture>::Future>
where
Self: Sized,
<Self as Future>::Output: IntoFuture,
@ -221,7 +111,7 @@ extension_trait! {
fn race<F>(
self,
other: F,
) -> impl Future<Output = <Self as std::future::Future>::Output> [Race<Self, F>]
) -> Race<Self, F>
where
Self: std::future::Future + Sized,
F: std::future::Future<Output = <Self as std::future::Future>::Output>,
@ -267,7 +157,7 @@ extension_trait! {
fn try_race<F, T, E>(
self,
other: F
) -> impl Future<Output = <Self as std::future::Future>::Output> [TryRace<Self, F>]
) -> TryRace<Self, F>
where
Self: std::future::Future<Output = Result<T, E>> + Sized,
F: std::future::Future<Output = <Self as std::future::Future>::Output>,
@ -304,7 +194,7 @@ extension_trait! {
fn join<F>(
self,
other: F
) -> impl Future<Output = (<Self as std::future::Future>::Output, <F as std::future::Future>::Output)> [Join<Self, F>]
) -> Join<Self, F>
where
Self: std::future::Future + Sized,
F: std::future::Future,
@ -351,7 +241,7 @@ extension_trait! {
fn try_join<F, A, B, E>(
self,
other: F
) -> impl Future<Output = Result<(A, B), E>> [TryJoin<Self, F>]
) -> TryJoin<Self, F>
where
Self: std::future::Future<Output = Result<A, E>> + Sized,
F: std::future::Future<Output = Result<B, E>>,
@ -387,46 +277,12 @@ extension_trait! {
"#]
#[cfg(any(all(feature = "default", feature = "unstable"), feature = "docs"))]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn timeout(self, dur: Duration) -> impl Future<Output = Self::Output> [TimeoutFuture<Self>]
fn timeout(self, dur: Duration) -> TimeoutFuture<Self>
where Self: Sized
{
TimeoutFuture::new(self, dur)
}
}
impl<F: Future + Unpin + ?Sized> Future for Box<F> {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<F: Future + Unpin + ?Sized> Future for &mut F {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<P> Future for Pin<P>
where
P: DerefMut + Unpin,
<P as Deref>::Target: Future,
{
type Output = <<P as Deref>::Target as Future>::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<T: Future + ?Sized> FutureExt for T {}
impl<F: Future> Future for std::panic::AssertUnwindSafe<F> {
type Output = F::Output;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
unreachable!("this impl only appears in the rendered docs")
}
}
}

@ -15,61 +15,14 @@ use std::pin::Pin;
use crate::io;
use crate::task::{Context, Poll};
extension_trait! {
use std::ops::{Deref, DerefMut};
#[doc = r#"
Allows reading from a buffered byte stream.
This trait is a re-export of [`futures::io::AsyncBufRead`] and is an async version of
[`std::io::BufRead`].
The [provided methods] do not really exist in the trait itself, but they become
available when [`BufReadExt`] from the [prelude] is imported:
```
# #[allow(unused_imports)]
use async_std::io::prelude::*;
```
[`std::io::BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html
[`futures::io::AsyncBufRead`]:
https://docs.rs/futures/0.3/futures/io/trait.AsyncBufRead.html
[provided methods]: #provided-methods
[`BufReadExt`]: ../io/prelude/trait.BufReadExt.html
[prelude]: ../prelude/index.html
"#]
pub trait BufRead {
#[doc = r#"
Returns the contents of the internal buffer, filling it with more data from the
inner reader if it is empty.
This function is a lower-level call. It needs to be paired with the [`consume`]
method to function properly. When calling this method, none of the contents will be
"read" in the sense that later calling `read` may return the same contents. As
such, [`consume`] must be called with the number of bytes that are consumed from
this buffer to ensure that the bytes are never returned twice.
[`consume`]: #tymethod.consume
An empty buffer returned indicates that the stream has reached EOF.
"#]
// TODO: write a proper doctest with `consume`
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>>;
#[doc = r#"
Tells this buffer that `amt` bytes have been consumed from the buffer, so they
should no longer be returned in calls to `read`.
"#]
fn consume(self: Pin<&mut Self>, amt: usize);
}
pub use futures_io::AsyncBufRead as BufRead;
#[doc = r#"
Extension methods for [`BufRead`].
[`BufRead`]: ../trait.BufRead.html
"#]
pub trait BufReadExt: futures_io::AsyncBufRead {
pub trait BufReadExt: BufRead {
#[doc = r#"
Reads all bytes into `buf` until the delimiter `byte` or EOF is reached.
@ -124,7 +77,7 @@ extension_trait! {
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>,
) -> impl Future<Output = usize> + 'a [ReadUntilFuture<'a, Self>]
) -> ReadUntilFuture<'a, Self>
where
Self: Unpin,
{
@ -177,7 +130,7 @@ extension_trait! {
fn read_line<'a>(
&'a mut self,
buf: &'a mut String,
) -> impl Future<Output = io::Result<usize>> + 'a [ReadLineFuture<'a, Self>]
) -> ReadLineFuture<'a, Self>
where
Self: Unpin,
{
@ -284,62 +237,7 @@ extension_trait! {
}
}
impl<T: BufRead + Unpin + ?Sized> BufRead for Box<T> {
fn poll_fill_buf(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<io::Result<&[u8]>> {
unreachable!("this impl only appears in the rendered docs")
}
fn consume(self: Pin<&mut Self>, amt: usize) {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<T: BufRead + Unpin + ?Sized> BufRead for &mut T {
fn poll_fill_buf(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<io::Result<&[u8]>> {
unreachable!("this impl only appears in the rendered docs")
}
fn consume(self: Pin<&mut Self>, amt: usize) {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<P> BufRead for Pin<P>
where
P: DerefMut + Unpin,
<P as Deref>::Target: BufRead,
{
fn poll_fill_buf(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<io::Result<&[u8]>> {
unreachable!("this impl only appears in the rendered docs")
}
fn consume(self: Pin<&mut Self>, amt: usize) {
unreachable!("this impl only appears in the rendered docs")
}
}
impl BufRead for &[u8] {
fn poll_fill_buf(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<io::Result<&[u8]>> {
unreachable!()
}
fn consume(self: Pin<&mut Self>, amt: usize) {
unreachable!("this impl only appears in the rendered docs")
}
}
}
impl<T: BufRead + ?Sized> BufReadExt for T {}
pub fn read_until_internal<R: BufReadExt + ?Sized>(
mut reader: Pin<&mut R>,

@ -21,63 +21,14 @@ pub use bytes::Bytes;
pub use chain::Chain;
pub use take::Take;
extension_trait! {
use std::pin::Pin;
use std::ops::{Deref, DerefMut};
use crate::io;
use crate::task::{Context, Poll};
#[doc = r#"
Allows reading from a byte stream.
This trait is a re-export of [`futures::io::AsyncRead`] and is an async version of
[`std::io::Read`].
Methods other than [`poll_read`] and [`poll_read_vectored`] do not really exist in the
trait itself, but they become available when [`ReadExt`] from the [prelude] is imported:
```
# #[allow(unused_imports)]
use async_std::prelude::*;
```
[`std::io::Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
[`futures::io::AsyncRead`]:
https://docs.rs/futures/0.3/futures/io/trait.AsyncRead.html
[`poll_read`]: #tymethod.poll_read
[`poll_read_vectored`]: #method.poll_read_vectored
[`ReadExt`]: ../io/prelude/trait.ReadExt.html
[prelude]: ../prelude/index.html
"#]
pub trait Read {
#[doc = r#"
Attempt to read from the `AsyncRead` into `buf`.
"#]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>>;
#[doc = r#"
Attempt to read from the `AsyncRead` into `bufs` using vectored IO operations.
"#]
fn poll_read_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &mut [IoSliceMut<'_>],
) -> Poll<io::Result<usize>> {
unreachable!("this impl only appears in the rendered docs")
}
}
pub use futures_io::AsyncRead as Read;
#[doc = r#"
Extension methods for [`Read`].
[`Read`]: ../trait.Read.html
"#]
pub trait ReadExt: futures_io::AsyncRead {
pub trait ReadExt: Read {
#[doc = r#"
Reads some bytes from the byte stream.
@ -112,7 +63,7 @@ extension_trait! {
fn read<'a>(
&'a mut self,
buf: &'a mut [u8],
) -> impl Future<Output = io::Result<usize>> + 'a [ReadFuture<'a, Self>]
) -> ReadFuture<'a, Self>
where
Self: Unpin
{
@ -134,7 +85,7 @@ extension_trait! {
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>],
) -> impl Future<Output = io::Result<usize>> + 'a [ReadVectoredFuture<'a, Self>]
) -> ReadVectoredFuture<'a, Self>
where
Self: Unpin,
{
@ -171,7 +122,7 @@ extension_trait! {
fn read_to_end<'a>(
&'a mut self,
buf: &'a mut Vec<u8>,
) -> impl Future<Output = io::Result<usize>> + 'a [ReadToEndFuture<'a, Self>]
) -> ReadToEndFuture<'a, Self>
where
Self: Unpin,
{
@ -210,7 +161,7 @@ extension_trait! {
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String,
) -> impl Future<Output = io::Result<usize>> + 'a [ReadToStringFuture<'a, Self>]
) -> ReadToStringFuture<'a, Self>
where
Self: Unpin,
{
@ -265,7 +216,7 @@ extension_trait! {
fn read_exact<'a>(
&'a mut self,
buf: &'a mut [u8],
) -> impl Future<Output = io::Result<()>> + 'a [ReadExactFuture<'a, Self>]
) -> ReadExactFuture<'a, Self>
where
Self: Unpin,
{
@ -420,53 +371,9 @@ extension_trait! {
fn chain<R: Read>(self, next: R) -> Chain<Self, R> where Self: Sized {
Chain { first: self, second: next, done_first: false }
}
}
impl<T: Read + Unpin + ?Sized> Read for Box<T> {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<T: Read + Unpin + ?Sized> Read for &mut T {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<P> Read for Pin<P>
where
P: DerefMut + Unpin,
<P as Deref>::Target: Read,
{
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl Read for &[u8] {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
unreachable!("this impl only appears in the rendered docs")
}
}
}
impl<T: Read + ?Sized> ReadExt for T {}
/// Initializes a buffer if necessary.
///

@ -4,51 +4,14 @@ use seek::SeekFuture;
use crate::io::SeekFrom;
extension_trait! {
use std::ops::{Deref, DerefMut};
use std::pin::Pin;
use crate::io;
use crate::task::{Context, Poll};
#[doc = r#"
Allows seeking through a byte stream.
This trait is a re-export of [`futures::io::AsyncSeek`] and is an async version of
[`std::io::Seek`].
The [provided methods] do not really exist in the trait itself, but they become
available when [`SeekExt`] the [prelude] is imported:
```
# #[allow(unused_imports)]
use async_std::prelude::*;
```
[`std::io::Seek`]: https://doc.rust-lang.org/std/io/trait.Seek.html
[`futures::io::AsyncSeek`]:
https://docs.rs/futures/0.3/futures/io/trait.AsyncSeek.html
[provided methods]: #provided-methods
[`SeekExt`]: ../io/prelude/trait.SeekExt.html
[prelude]: ../prelude/index.html
"#]
pub trait Seek {
#[doc = r#"
Attempt to seek to an offset, in bytes, in a stream.
"#]
fn poll_seek(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
pos: SeekFrom,
) -> Poll<io::Result<u64>>;
}
pub use futures_io::AsyncSeek as Seek;
#[doc = r#"
Extension methods for [`Seek`].
[`Seek`]: ../trait.Seek.html
"#]
pub trait SeekExt: futures_io::AsyncSeek {
pub trait SeekExt: Seek {
#[doc = r#"
Seeks to a new position in a byte stream.
@ -76,7 +39,7 @@ extension_trait! {
fn seek(
&mut self,
pos: SeekFrom,
) -> impl Future<Output = io::Result<u64>> + '_ [SeekFuture<'_, Self>]
) -> SeekFuture<'_, Self>
where
Self: Unpin,
{
@ -84,37 +47,4 @@ extension_trait! {
}
}
impl<T: Seek + Unpin + ?Sized> Seek for Box<T> {
fn poll_seek(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
pos: SeekFrom,
) -> Poll<io::Result<u64>> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<T: Seek + Unpin + ?Sized> Seek for &mut T {
fn poll_seek(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
pos: SeekFrom,
) -> Poll<io::Result<u64>> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<P> Seek for Pin<P>
where
P: DerefMut + Unpin,
<P as Deref>::Target: Seek,
{
fn poll_seek(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
pos: SeekFrom,
) -> Poll<io::Result<u64>> {
unreachable!("this impl only appears in the rendered docs")
}
}
}
impl<T: Seek + ?Sized> SeekExt for T {}

@ -12,76 +12,14 @@ use write_vectored::WriteVectoredFuture;
use crate::io::{self, IoSlice};
extension_trait! {
use std::pin::Pin;
use std::ops::{Deref, DerefMut};
use crate::task::{Context, Poll};
#[doc = r#"
Allows writing to a byte stream.
This trait is a re-export of [`futures::io::AsyncWrite`] and is an async version of
[`std::io::Write`].
Methods other than [`poll_write`], [`poll_write_vectored`], [`poll_flush`], and
[`poll_close`] do not really exist in the trait itself, but they become available when
[`WriteExt`] from the [prelude] is imported:
```
# #[allow(unused_imports)]
use async_std::prelude::*;
```
[`std::io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
[`futures::io::AsyncWrite`]:
https://docs.rs/futures/0.3/futures/io/trait.AsyncWrite.html
[`poll_write`]: #tymethod.poll_write
[`poll_write_vectored`]: #method.poll_write_vectored
[`poll_flush`]: #tymethod.poll_flush
[`poll_close`]: #tymethod.poll_close
[`WriteExt`]: ../io/prelude/trait.WriteExt.html
[prelude]: ../prelude/index.html
"#]
pub trait Write {
#[doc = r#"
Attempt to write bytes from `buf` into the object.
"#]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>>;
#[doc = r#"
Attempt to write bytes from `bufs` into the object using vectored IO operations.
"#]
fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<io::Result<usize>> {
unreachable!("this impl only appears in the rendered docs")
}
#[doc = r#"
Attempt to flush the object, ensuring that any buffered data reach
their destination.
"#]
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>>;
#[doc = r#"
Attempt to close the object.
"#]
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>>;
}
pub use futures_io::AsyncWrite as Write;
#[doc = r#"
Extension methods for [`Write`].
[`Write`]: ../trait.Write.html
"#]
pub trait WriteExt: futures_io::AsyncWrite {
pub trait WriteExt: Write {
#[doc = r#"
Writes some bytes into the byte stream.
@ -110,7 +48,7 @@ extension_trait! {
fn write<'a>(
&'a mut self,
buf: &'a [u8],
) -> impl Future<Output = io::Result<usize>> + 'a [WriteFuture<'a, Self>]
) -> WriteFuture<'a, Self>
where
Self: Unpin,
{
@ -136,7 +74,7 @@ extension_trait! {
# Ok(()) }) }
```
"#]
fn flush(&mut self) -> impl Future<Output = io::Result<()>> + '_ [FlushFuture<'_, Self>]
fn flush(&mut self) -> FlushFuture<'_, Self>
where
Self: Unpin,
{
@ -158,7 +96,7 @@ extension_trait! {
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>],
) -> impl Future<Output = io::Result<usize>> + 'a [WriteVectoredFuture<'a, Self>]
) -> WriteVectoredFuture<'a, Self>
where
Self: Unpin,
{
@ -194,7 +132,7 @@ extension_trait! {
fn write_all<'a>(
&'a mut self,
buf: &'a [u8],
) -> impl Future<Output = io::Result<()>> + 'a [WriteAllFuture<'a, Self>]
) -> WriteAllFuture<'a, Self>
where
Self: Unpin,
{
@ -231,7 +169,7 @@ extension_trait! {
fn write_fmt<'a>(
&'a mut self,
fmt: std::fmt::Arguments<'_>,
) -> impl Future<Output = io::Result<()>> + 'a [WriteFmtFuture<'a, Self>]
) -> WriteFmtFuture<'a, Self>
where
Self: Unpin,
{
@ -246,79 +184,4 @@ extension_trait! {
}
}
impl<T: Write + Unpin + ?Sized> Write for Box<T> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
unreachable!("this impl only appears in the rendered docs")
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unreachable!("this impl only appears in the rendered docs")
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<T: Write + Unpin + ?Sized> Write for &mut T {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
unreachable!("this impl only appears in the rendered docs")
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unreachable!("this impl only appears in the rendered docs")
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<P> Write for Pin<P>
where
P: DerefMut + Unpin,
<P as Deref>::Target: Write,
{
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
unreachable!("this impl only appears in the rendered docs")
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unreachable!("this impl only appears in the rendered docs")
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl Write for Vec<u8> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
unreachable!("this impl only appears in the rendered docs")
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unreachable!("this impl only appears in the rendered docs")
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unreachable!("this impl only appears in the rendered docs")
}
}
}
impl<T: Write + ?Sized> WriteExt for T {}

@ -282,7 +282,6 @@
#![doc(test(attr(deny(rust_2018_idioms, warnings))))]
#![doc(test(attr(allow(unused_extern_crates, unused_variables))))]
#![doc(html_logo_url = "https://async.rs/images/logo--hero.svg")]
#![recursion_limit = "2048"]
extern crate alloc;

@ -143,98 +143,14 @@ cfg_unstable! {
mod unzip;
}
extension_trait! {
use std::ops::{Deref, DerefMut};
use crate::task::{Context, Poll};
#[doc = r#"
An asynchronous stream of values.
This trait is a re-export of [`futures::stream::Stream`] and is an async version of
[`std::iter::Iterator`].
The [provided methods] do not really exist in the trait itself, but they become
available when [`StreamExt`] from the [prelude] is imported:
```
# #[allow(unused_imports)]
use async_std::prelude::*;
```
[`std::iter::Iterator`]: https://doc.rust-lang.org/std/iter/trait.Iterator.html
[`futures::stream::Stream`]:
https://docs.rs/futures/0.3/futures/stream/trait.Stream.html
[provided methods]: #provided-methods
[`StreamExt`]: ../prelude/trait.StreamExt.html
[prelude]: ../prelude/index.html
"#]
pub trait Stream {
#[doc = r#"
The type of items yielded by this stream.
"#]
type Item;
#[doc = r#"
Attempts to receive the next item from the stream.
There are several possible return values:
* `Poll::Pending` means this stream's next value is not ready yet.
* `Poll::Ready(None)` means this stream has been exhausted.
* `Poll::Ready(Some(item))` means `item` was received out of the stream.
# Examples
```
# fn main() { async_std::task::block_on(async {
#
use std::pin::Pin;
use async_std::prelude::*;
use async_std::stream;
use async_std::task::{Context, Poll};
fn increment(
s: impl Stream<Item = i32> + Unpin,
) -> impl Stream<Item = i32> + Unpin {
struct Increment<S>(S);
impl<S: Stream<Item = i32> + Unpin> Stream for Increment<S> {
type Item = S::Item;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
match Pin::new(&mut self.0).poll_next(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(None) => Poll::Ready(None),
Poll::Ready(Some(item)) => Poll::Ready(Some(item + 1)),
}
}
}
Increment(s)
}
let mut s = increment(stream::once(7));
assert_eq!(s.next().await, Some(8));
assert_eq!(s.next().await, None);
#
# }) }
```
"#]
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
}
pub use futures_core::stream::Stream as Stream;
#[doc = r#"
Extension methods for [`Stream`].
[`Stream`]: ../stream/trait.Stream.html
"#]
pub trait StreamExt: futures_core::stream::Stream {
pub trait StreamExt: Stream {
#[doc = r#"
Advances the stream and returns the next value.
@ -260,7 +176,7 @@ extension_trait! {
# }) }
```
"#]
fn next(&mut self) -> impl Future<Output = Option<Self::Item>> + '_ [NextFuture<'_, Self>]
fn next(&mut self) -> NextFuture<'_, Self>
where
Self: Unpin,
{
@ -712,7 +628,7 @@ extension_trait! {
"#]
fn last(
self,
) -> impl Future<Output = Option<Self::Item>> [LastFuture<Self, Self::Item>]
) -> LastFuture<Self, Self::Item>
where
Self: Sized,
{
@ -922,7 +838,7 @@ extension_trait! {
fn min_by_key<B, F>(
self,
key_by: F,
) -> impl Future<Output = Option<Self::Item>> [MinByKeyFuture<Self, Self::Item, F>]
) -> MinByKeyFuture<Self, Self::Item, F>
where
Self: Sized,
B: Ord,
@ -958,7 +874,7 @@ extension_trait! {
fn max_by_key<B, F>(
self,
key_by: F,
) -> impl Future<Output = Option<Self::Item>> [MaxByKeyFuture<Self, Self::Item, F>]
) -> MaxByKeyFuture<Self, Self::Item, F>
where
Self: Sized,
B: Ord,
@ -997,7 +913,7 @@ extension_trait! {
fn min_by<F>(
self,
compare: F,
) -> impl Future<Output = Option<Self::Item>> [MinByFuture<Self, F, Self::Item>]
) -> MinByFuture<Self, F, Self::Item>
where
Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
@ -1030,7 +946,7 @@ extension_trait! {
"#]
fn max(
self,
) -> impl Future<Output = Option<Self::Item>> [MaxFuture<Self, Self::Item>]
) -> MaxFuture<Self, Self::Item>
where
Self: Sized,
Self::Item: Ord,
@ -1063,7 +979,7 @@ extension_trait! {
"#]
fn min(
self,
) -> impl Future<Output = Option<Self::Item>> [MinFuture<Self, Self::Item>]
) -> MinFuture<Self, Self::Item>
where
Self: Sized,
Self::Item: Ord,
@ -1101,7 +1017,7 @@ extension_trait! {
fn max_by<F>(
self,
compare: F,
) -> impl Future<Output = Option<Self::Item>> [MaxByFuture<Self, F, Self::Item>]
) -> MaxByFuture<Self, F, Self::Item>
where
Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> Ordering,
@ -1165,7 +1081,7 @@ extension_trait! {
fn nth(
&mut self,
n: usize,
) -> impl Future<Output = Option<Self::Item>> + '_ [NthFuture<'_, Self>]
) -> NthFuture<'_, Self>
where
Self: Unpin + Sized,
{
@ -1221,7 +1137,7 @@ extension_trait! {
fn all<F>(
&mut self,
f: F,
) -> impl Future<Output = bool> + '_ [AllFuture<'_, Self, F, Self::Item>]
) -> AllFuture<'_, Self, F, Self::Item>
where
Self: Unpin + Sized,
F: FnMut(Self::Item) -> bool,
@ -1270,7 +1186,7 @@ extension_trait! {
fn find<P>(
&mut self,
p: P,
) -> impl Future<Output = Option<Self::Item>> + '_ [FindFuture<'_, Self, P>]
) -> FindFuture<'_, Self, P>
where
Self: Unpin + Sized,
P: FnMut(&Self::Item) -> bool,
@ -1298,7 +1214,7 @@ extension_trait! {
fn find_map<F, B>(
&mut self,
f: F,
) -> impl Future<Output = Option<B>> + '_ [FindMapFuture<'_, Self, F>]
) -> FindMapFuture<'_, Self, F>
where
Self: Unpin + Sized,
F: FnMut(Self::Item) -> Option<B>,
@ -1332,7 +1248,7 @@ extension_trait! {
self,
init: B,
f: F,
) -> impl Future<Output = B> [FoldFuture<Self, F, B>]
) -> FoldFuture<Self, F, B>
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
@ -1369,7 +1285,7 @@ extension_trait! {
fn partition<B, F>(
self,
f: F,
) -> impl Future<Output = (B, B)> [PartitionFuture<Self, F, B>]
) -> PartitionFuture<Self, F, B>
where
Self: Sized,
F: FnMut(&Self::Item) -> bool,
@ -1405,7 +1321,7 @@ extension_trait! {
fn for_each<F>(
self,
f: F,
) -> impl Future<Output = ()> [ForEachFuture<Self, F>]
) -> ForEachFuture<Self, F>
where
Self: Sized,
F: FnMut(Self::Item),
@ -1461,7 +1377,7 @@ extension_trait! {
fn any<F>(
&mut self,
f: F,
) -> impl Future<Output = bool> + '_ [AnyFuture<'_, Self, F, Self::Item>]
) -> AnyFuture<'_, Self, F, Self::Item>
where
Self: Unpin + Sized,
F: FnMut(Self::Item) -> bool,
@ -1697,7 +1613,7 @@ extension_trait! {
&mut self,
init: T,
f: F,
) -> impl Future<Output = Result<T, E>> + '_ [TryFoldFuture<'_, Self, F, T>]
) -> TryFoldFuture<'_, Self, F, T>
where
Self: Unpin + Sized,
F: FnMut(B, Self::Item) -> Result<T, E>,
@ -1742,7 +1658,7 @@ extension_trait! {
fn try_for_each<F, E>(
&mut self,
f: F,
) -> impl Future<Output = E> + 'a [TryForEachFuture<'_, Self, F>]
) -> TryForEachFuture<'_, Self, F>
where
Self: Unpin + Sized,
F: FnMut(Self::Item) -> Result<(), E>,
@ -1824,7 +1740,7 @@ extension_trait! {
"#]
#[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn unzip<A, B, FromA, FromB>(self) -> impl Future<Output = (FromA, FromB)> [UnzipFuture<Self, FromA, FromB>]
fn unzip<A, B, FromA, FromB>(self) -> UnzipFuture<Self, FromA, FromB>
where
FromA: Default + Extend<A>,
FromB: Default + Extend<B>,
@ -1888,7 +1804,7 @@ extension_trait! {
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn collect<'a, B>(
self,
) -> impl Future<Output = B> + 'a [Pin<Box<dyn Future<Output = B> + 'a + Send>>]
) -> Pin<Box<dyn Future<Output = B> + 'a + Send>>
where
Self: Sized + 'a + Send,
B: FromStream<Self::Item>,
@ -1962,7 +1878,7 @@ extension_trait! {
fn partial_cmp<S>(
self,
other: S
) -> impl Future<Output = Option<Ordering>> [PartialCmpFuture<Self, S>]
) -> PartialCmpFuture<Self, S>
where
Self: Sized + Stream,
S: Stream,
@ -2002,7 +1918,7 @@ extension_trait! {
fn position<P>(
&mut self,
predicate: P,
) -> impl Future<Output = Option<usize>> + '_ [PositionFuture<'_, Self, P>]
) -> PositionFuture<'_, Self, P>
where
Self: Unpin + Sized,
P: FnMut(Self::Item) -> bool,
@ -2040,7 +1956,7 @@ extension_trait! {
fn cmp<S>(
self,
other: S
) -> impl Future<Output = Ordering> [CmpFuture<Self, S>]
) -> CmpFuture<Self, S>
where
Self: Sized + Stream,
S: Stream,
@ -2071,7 +1987,7 @@ extension_trait! {
"#]
#[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn count(self) -> impl Future<Output = usize> [CountFuture<Self>]
fn count(self) -> CountFuture<Self>
where
Self: Sized,
{
@ -2106,7 +2022,7 @@ extension_trait! {
fn ne<S>(
self,
other: S
) -> impl Future<Output = bool> [NeFuture<Self, S>]
) -> NeFuture<Self, S>
where
Self: Sized,
S: Sized + Stream,
@ -2143,7 +2059,7 @@ extension_trait! {
fn ge<S>(
self,
other: S
) -> impl Future<Output = bool> [GeFuture<Self, S>]
) -> GeFuture<Self, S>
where
Self: Sized + Stream,
S: Stream,
@ -2180,7 +2096,7 @@ extension_trait! {
fn eq<S>(
self,
other: S
) -> impl Future<Output = bool> [EqFuture<Self, S>]
) -> EqFuture<Self, S>
where
Self: Sized + Stream,
S: Sized + Stream,
@ -2217,7 +2133,7 @@ extension_trait! {
fn gt<S>(
self,
other: S
) -> impl Future<Output = bool> [GtFuture<Self, S>]
) -> GtFuture<Self, S>
where
Self: Sized + Stream,
S: Stream,
@ -2254,7 +2170,7 @@ extension_trait! {
fn le<S>(
self,
other: S
) -> impl Future<Output = bool> [LeFuture<Self, S>]
) -> LeFuture<Self, S>
where
Self: Sized + Stream,
S: Stream,
@ -2291,7 +2207,7 @@ extension_trait! {
fn lt<S>(
self,
other: S
) -> impl Future<Output = bool> [LtFuture<Self, S>]
) -> LtFuture<Self, S>
where
Self: Sized + Stream,
S: Stream,
@ -2335,7 +2251,7 @@ extension_trait! {
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn sum<'a, S>(
self,
) -> impl Future<Output = S> + 'a [Pin<Box<dyn Future<Output = S> + 'a>>]
) -> Pin<Box<dyn Future<Output = S> + 'a>>
where
Self: Sized + Stream<Item = S> + 'a,
S: Sum<Self::Item>,
@ -2381,7 +2297,7 @@ extension_trait! {
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn product<'a, P>(
self,
) -> impl Future<Output = P> + 'a [Pin<Box<dyn Future<Output = P> + 'a>>]
) -> Pin<Box<dyn Future<Output = P> + 'a>>
where
Self: Sized + Stream<Item = P> + 'a,
P: Product,
@ -2390,39 +2306,5 @@ extension_trait! {
}
}
impl<S: Stream + Unpin + ?Sized> Stream for Box<S> {
type Item = S::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<S: Stream + Unpin + ?Sized> Stream for &mut S {
type Item = S::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<P> Stream for Pin<P>
where
P: DerefMut + Unpin,
<P as Deref>::Target: Stream,
{
type Item = <<P as Deref>::Target as Stream>::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
unreachable!("this impl only appears in the rendered docs")
}
}
impl<S: Stream> Stream for std::panic::AssertUnwindSafe<S> {
type Item = S::Item;
impl<T: Stream + ?Sized> StreamExt for T {}
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
unreachable!("this impl only appears in the rendered docs")
}
}
}

@ -233,99 +233,3 @@ macro_rules! cfg_default {
)*
}
}
/// Defines an extension trait for a base trait.
///
/// In generated docs, the base trait will contain methods from the extension trait. In actual
/// code, the base trait will be re-exported and the extension trait will be hidden. We then
/// re-export the extension trait from the prelude.
///
/// Inside invocations of this macro, we write a definitions that looks similar to the final
/// rendered docs, and the macro then generates all the boilerplate for us.
#[allow(unused_macros)]
#[doc(hidden)]
macro_rules! extension_trait {
(
// Interesting patterns:
// - `$name`: trait name that gets rendered in the docs
// - `$ext`: name of the hidden extension trait
// - `$base`: base trait
#[doc = $doc:tt]
pub trait $name:ident {
$($body_base:tt)*
}
#[doc = $doc_ext:tt]
pub trait $ext:ident: $base:path {
$($body_ext:tt)*
}
// Shim trait impls that only appear in docs.
$($imp:item)*
) => {
// A fake `impl Future` type that doesn't borrow.
#[allow(dead_code)]
mod owned {
#[doc(hidden)]
pub struct ImplFuture<T>(core::marker::PhantomData<T>);
}
// A fake `impl Future` type that borrows its environment.
#[allow(dead_code)]
mod borrowed {
#[doc(hidden)]
pub struct ImplFuture<'a, T>(core::marker::PhantomData<&'a T>);
}
// Render a fake trait combining the bodies of the base trait and the extension trait.
#[cfg(feature = "docs")]
#[doc = $doc]
pub trait $name {
extension_trait!(@doc [$($body_base)* $($body_ext)*] -> []);
}
// When not rendering docs, re-export the base trait from the futures crate.
#[cfg(not(feature = "docs"))]
pub use $base as $name;
// The extension trait that adds methods to any type implementing the base trait.
#[doc = $doc_ext]
pub trait $ext: $name {
extension_trait!(@ext [$($body_ext)*] -> []);
}
// Blanket implementation of the extension trait for any type implementing the base trait.
impl<T: $name + ?Sized> $ext for T {}
// Shim trait impls that only appear in docs.
$(#[cfg(feature = "docs")] $imp)*
};
// Parse the return type in an extension method.
(@doc [-> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*] -> [$($accum:tt)*]) => {
extension_trait!(@doc [$($tail)*] -> [$($accum)* -> owned::ImplFuture<$out>]);
};
(@ext [-> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*] -> [$($accum:tt)*]) => {
extension_trait!(@ext [$($tail)*] -> [$($accum)* -> $f]);
};
// Parse a token.
(@doc [$token:tt $($tail:tt)*] -> [$($accum:tt)*]) => {
extension_trait!(@doc [$($tail)*] -> [$($accum)* $token]);
};
(@ext [$token:tt $($tail:tt)*] -> [$($accum:tt)*]) => {
extension_trait!(@ext [$($tail)*] -> [$($accum)* $token]);
};
// Handle the end of the token list.
(@doc [] -> [$($accum:tt)*]) => { $($accum)* };
(@ext [] -> [$($accum:tt)*]) => { $($accum)* };
// Parse imports at the beginning of the macro.
($import:item $($tail:tt)*) => {
#[cfg(feature = "docs")]
$import
extension_trait!($($tail)*);
};
}

Loading…
Cancel
Save