2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-04-06 16:36:40 +00:00

Merge pull request #1006 from nnethercote/rm-extension_trait

Remove `extension_trait`
This commit is contained in:
Yosh 2022-03-16 12:32:50 +01:00 committed by GitHub
commit 7e455db4f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 3073 additions and 3834 deletions

View file

@ -20,123 +20,14 @@ cfg_unstable_default! {
use crate::future::timeout::TimeoutFuture; use crate::future::timeout::TimeoutFuture;
} }
extension_trait! { pub use core::future::Future as Future;
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>;
}
#[doc = r#" #[doc = r#"
Extension methods for [`Future`]. Extension methods for [`Future`].
[`Future`]: ../future/trait.Future.html [`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. /// Returns a Future that delays execution for a specified time.
/// ///
/// # Examples /// # Examples
@ -153,7 +44,7 @@ extension_trait! {
/// ``` /// ```
#[cfg(feature = "unstable")] #[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(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 where
Self: Sized, Self: Sized,
{ {
@ -178,8 +69,7 @@ extension_trait! {
#[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn flatten( fn flatten(
self, self,
) -> impl Future<Output = <Self::Output as IntoFuture>::Output> ) -> FlattenFuture<Self, <Self::Output as IntoFuture>::Future>
[FlattenFuture<Self, <Self::Output as IntoFuture>::Future>]
where where
Self: Sized, Self: Sized,
<Self as Future>::Output: IntoFuture, <Self as Future>::Output: IntoFuture,
@ -221,7 +111,7 @@ extension_trait! {
fn race<F>( fn race<F>(
self, self,
other: F, other: F,
) -> impl Future<Output = <Self as std::future::Future>::Output> [Race<Self, F>] ) -> Race<Self, F>
where where
Self: std::future::Future + Sized, Self: std::future::Future + Sized,
F: std::future::Future<Output = <Self as std::future::Future>::Output>, F: std::future::Future<Output = <Self as std::future::Future>::Output>,
@ -267,7 +157,7 @@ extension_trait! {
fn try_race<F, T, E>( fn try_race<F, T, E>(
self, self,
other: F other: F
) -> impl Future<Output = <Self as std::future::Future>::Output> [TryRace<Self, F>] ) -> TryRace<Self, F>
where where
Self: std::future::Future<Output = Result<T, E>> + Sized, Self: std::future::Future<Output = Result<T, E>> + Sized,
F: std::future::Future<Output = <Self as std::future::Future>::Output>, F: std::future::Future<Output = <Self as std::future::Future>::Output>,
@ -304,7 +194,7 @@ extension_trait! {
fn join<F>( fn join<F>(
self, self,
other: F other: F
) -> impl Future<Output = (<Self as std::future::Future>::Output, <F as std::future::Future>::Output)> [Join<Self, F>] ) -> Join<Self, F>
where where
Self: std::future::Future + Sized, Self: std::future::Future + Sized,
F: std::future::Future, F: std::future::Future,
@ -351,7 +241,7 @@ extension_trait! {
fn try_join<F, A, B, E>( fn try_join<F, A, B, E>(
self, self,
other: F other: F
) -> impl Future<Output = Result<(A, B), E>> [TryJoin<Self, F>] ) -> TryJoin<Self, F>
where where
Self: std::future::Future<Output = Result<A, E>> + Sized, Self: std::future::Future<Output = Result<A, E>> + Sized,
F: std::future::Future<Output = Result<B, E>>, F: std::future::Future<Output = Result<B, E>>,
@ -387,46 +277,12 @@ extension_trait! {
"#] "#]
#[cfg(any(all(feature = "default", feature = "unstable"), feature = "docs"))] #[cfg(any(all(feature = "default", feature = "unstable"), feature = "docs"))]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[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 where Self: Sized
{ {
TimeoutFuture::new(self, dur) TimeoutFuture::new(self, dur)
} }
} }
impl<F: Future + Unpin + ?Sized> Future for Box<F> { impl<T: Future + ?Sized> FutureExt for T {}
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<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")
}
}
}

View file

@ -15,61 +15,14 @@ use std::pin::Pin;
use crate::io; use crate::io;
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
extension_trait! { pub use futures_io::AsyncBufRead as BufRead;
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);
}
#[doc = r#" #[doc = r#"
Extension methods for [`BufRead`]. Extension methods for [`BufRead`].
[`BufRead`]: ../trait.BufRead.html [`BufRead`]: ../trait.BufRead.html
"#] "#]
pub trait BufReadExt: futures_io::AsyncBufRead { pub trait BufReadExt: BufRead {
#[doc = r#" #[doc = r#"
Reads all bytes into `buf` until the delimiter `byte` or EOF is reached. Reads all bytes into `buf` until the delimiter `byte` or EOF is reached.
@ -124,7 +77,7 @@ extension_trait! {
&'a mut self, &'a mut self,
byte: u8, byte: u8,
buf: &'a mut Vec<u8>, buf: &'a mut Vec<u8>,
) -> impl Future<Output = usize> + 'a [ReadUntilFuture<'a, Self>] ) -> ReadUntilFuture<'a, Self>
where where
Self: Unpin, Self: Unpin,
{ {
@ -177,7 +130,7 @@ extension_trait! {
fn read_line<'a>( fn read_line<'a>(
&'a mut self, &'a mut self,
buf: &'a mut String, buf: &'a mut String,
) -> impl Future<Output = io::Result<usize>> + 'a [ReadLineFuture<'a, Self>] ) -> ReadLineFuture<'a, Self>
where where
Self: Unpin, Self: Unpin,
{ {
@ -284,62 +237,7 @@ extension_trait! {
} }
} }
impl<T: BufRead + Unpin + ?Sized> BufRead for Box<T> { impl<T: BufRead + ?Sized> BufReadExt for 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")
}
}
}
pub fn read_until_internal<R: BufReadExt + ?Sized>( pub fn read_until_internal<R: BufReadExt + ?Sized>(
mut reader: Pin<&mut R>, mut reader: Pin<&mut R>,

View file

@ -21,63 +21,14 @@ pub use bytes::Bytes;
pub use chain::Chain; pub use chain::Chain;
pub use take::Take; pub use take::Take;
extension_trait! { pub use futures_io::AsyncRead as Read;
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")
}
}
#[doc = r#" #[doc = r#"
Extension methods for [`Read`]. Extension methods for [`Read`].
[`Read`]: ../trait.Read.html [`Read`]: ../trait.Read.html
"#] "#]
pub trait ReadExt: futures_io::AsyncRead { pub trait ReadExt: Read {
#[doc = r#" #[doc = r#"
Reads some bytes from the byte stream. Reads some bytes from the byte stream.
@ -112,7 +63,7 @@ extension_trait! {
fn read<'a>( fn read<'a>(
&'a mut self, &'a mut self,
buf: &'a mut [u8], buf: &'a mut [u8],
) -> impl Future<Output = io::Result<usize>> + 'a [ReadFuture<'a, Self>] ) -> ReadFuture<'a, Self>
where where
Self: Unpin Self: Unpin
{ {
@ -134,7 +85,7 @@ extension_trait! {
fn read_vectored<'a>( fn read_vectored<'a>(
&'a mut self, &'a mut self,
bufs: &'a mut [IoSliceMut<'a>], bufs: &'a mut [IoSliceMut<'a>],
) -> impl Future<Output = io::Result<usize>> + 'a [ReadVectoredFuture<'a, Self>] ) -> ReadVectoredFuture<'a, Self>
where where
Self: Unpin, Self: Unpin,
{ {
@ -171,7 +122,7 @@ extension_trait! {
fn read_to_end<'a>( fn read_to_end<'a>(
&'a mut self, &'a mut self,
buf: &'a mut Vec<u8>, buf: &'a mut Vec<u8>,
) -> impl Future<Output = io::Result<usize>> + 'a [ReadToEndFuture<'a, Self>] ) -> ReadToEndFuture<'a, Self>
where where
Self: Unpin, Self: Unpin,
{ {
@ -210,7 +161,7 @@ extension_trait! {
fn read_to_string<'a>( fn read_to_string<'a>(
&'a mut self, &'a mut self,
buf: &'a mut String, buf: &'a mut String,
) -> impl Future<Output = io::Result<usize>> + 'a [ReadToStringFuture<'a, Self>] ) -> ReadToStringFuture<'a, Self>
where where
Self: Unpin, Self: Unpin,
{ {
@ -265,7 +216,7 @@ extension_trait! {
fn read_exact<'a>( fn read_exact<'a>(
&'a mut self, &'a mut self,
buf: &'a mut [u8], buf: &'a mut [u8],
) -> impl Future<Output = io::Result<()>> + 'a [ReadExactFuture<'a, Self>] ) -> ReadExactFuture<'a, Self>
where where
Self: Unpin, Self: Unpin,
{ {
@ -420,53 +371,9 @@ extension_trait! {
fn chain<R: Read>(self, next: R) -> Chain<Self, R> where Self: Sized { fn chain<R: Read>(self, next: R) -> Chain<Self, R> where Self: Sized {
Chain { first: self, second: next, done_first: false } Chain { first: self, second: next, done_first: false }
} }
} }
impl<T: Read + Unpin + ?Sized> Read for Box<T> { impl<T: Read + ?Sized> ReadExt for 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")
}
}
}
/// Initializes a buffer if necessary. /// Initializes a buffer if necessary.
/// ///

View file

@ -4,51 +4,14 @@ use seek::SeekFuture;
use crate::io::SeekFrom; use crate::io::SeekFrom;
extension_trait! { pub use futures_io::AsyncSeek as Seek;
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>>;
}
#[doc = r#" #[doc = r#"
Extension methods for [`Seek`]. Extension methods for [`Seek`].
[`Seek`]: ../trait.Seek.html [`Seek`]: ../trait.Seek.html
"#] "#]
pub trait SeekExt: futures_io::AsyncSeek { pub trait SeekExt: Seek {
#[doc = r#" #[doc = r#"
Seeks to a new position in a byte stream. Seeks to a new position in a byte stream.
@ -76,7 +39,7 @@ extension_trait! {
fn seek( fn seek(
&mut self, &mut self,
pos: SeekFrom, pos: SeekFrom,
) -> impl Future<Output = io::Result<u64>> + '_ [SeekFuture<'_, Self>] ) -> SeekFuture<'_, Self>
where where
Self: Unpin, Self: Unpin,
{ {
@ -84,37 +47,4 @@ extension_trait! {
} }
} }
impl<T: Seek + Unpin + ?Sized> Seek for Box<T> { impl<T: Seek + ?Sized> SeekExt for 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")
}
}
}

View file

@ -12,76 +12,14 @@ use write_vectored::WriteVectoredFuture;
use crate::io::{self, IoSlice}; use crate::io::{self, IoSlice};
extension_trait! { pub use futures_io::AsyncWrite as Write;
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<()>>;
}
#[doc = r#" #[doc = r#"
Extension methods for [`Write`]. Extension methods for [`Write`].
[`Write`]: ../trait.Write.html [`Write`]: ../trait.Write.html
"#] "#]
pub trait WriteExt: futures_io::AsyncWrite { pub trait WriteExt: Write {
#[doc = r#" #[doc = r#"
Writes some bytes into the byte stream. Writes some bytes into the byte stream.
@ -110,7 +48,7 @@ extension_trait! {
fn write<'a>( fn write<'a>(
&'a mut self, &'a mut self,
buf: &'a [u8], buf: &'a [u8],
) -> impl Future<Output = io::Result<usize>> + 'a [WriteFuture<'a, Self>] ) -> WriteFuture<'a, Self>
where where
Self: Unpin, Self: Unpin,
{ {
@ -136,7 +74,7 @@ extension_trait! {
# Ok(()) }) } # Ok(()) }) }
``` ```
"#] "#]
fn flush(&mut self) -> impl Future<Output = io::Result<()>> + '_ [FlushFuture<'_, Self>] fn flush(&mut self) -> FlushFuture<'_, Self>
where where
Self: Unpin, Self: Unpin,
{ {
@ -158,7 +96,7 @@ extension_trait! {
fn write_vectored<'a>( fn write_vectored<'a>(
&'a mut self, &'a mut self,
bufs: &'a [IoSlice<'a>], bufs: &'a [IoSlice<'a>],
) -> impl Future<Output = io::Result<usize>> + 'a [WriteVectoredFuture<'a, Self>] ) -> WriteVectoredFuture<'a, Self>
where where
Self: Unpin, Self: Unpin,
{ {
@ -194,7 +132,7 @@ extension_trait! {
fn write_all<'a>( fn write_all<'a>(
&'a mut self, &'a mut self,
buf: &'a [u8], buf: &'a [u8],
) -> impl Future<Output = io::Result<()>> + 'a [WriteAllFuture<'a, Self>] ) -> WriteAllFuture<'a, Self>
where where
Self: Unpin, Self: Unpin,
{ {
@ -231,7 +169,7 @@ extension_trait! {
fn write_fmt<'a>( fn write_fmt<'a>(
&'a mut self, &'a mut self,
fmt: std::fmt::Arguments<'_>, fmt: std::fmt::Arguments<'_>,
) -> impl Future<Output = io::Result<()>> + 'a [WriteFmtFuture<'a, Self>] ) -> WriteFmtFuture<'a, Self>
where where
Self: Unpin, Self: Unpin,
{ {
@ -246,79 +184,4 @@ extension_trait! {
} }
} }
impl<T: Write + Unpin + ?Sized> Write for Box<T> { impl<T: Write + ?Sized> WriteExt for 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")
}
}
}

View file

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

View file

@ -143,98 +143,14 @@ cfg_unstable! {
mod unzip; mod unzip;
} }
extension_trait! { pub use futures_core::stream::Stream as Stream;
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>>;
}
#[doc = r#" #[doc = r#"
Extension methods for [`Stream`]. Extension methods for [`Stream`].
[`Stream`]: ../stream/trait.Stream.html [`Stream`]: ../stream/trait.Stream.html
"#] "#]
pub trait StreamExt: futures_core::stream::Stream { pub trait StreamExt: Stream {
#[doc = r#" #[doc = r#"
Advances the stream and returns the next value. 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 where
Self: Unpin, Self: Unpin,
{ {
@ -712,7 +628,7 @@ extension_trait! {
"#] "#]
fn last( fn last(
self, self,
) -> impl Future<Output = Option<Self::Item>> [LastFuture<Self, Self::Item>] ) -> LastFuture<Self, Self::Item>
where where
Self: Sized, Self: Sized,
{ {
@ -922,7 +838,7 @@ extension_trait! {
fn min_by_key<B, F>( fn min_by_key<B, F>(
self, self,
key_by: F, key_by: F,
) -> impl Future<Output = Option<Self::Item>> [MinByKeyFuture<Self, Self::Item, F>] ) -> MinByKeyFuture<Self, Self::Item, F>
where where
Self: Sized, Self: Sized,
B: Ord, B: Ord,
@ -958,7 +874,7 @@ extension_trait! {
fn max_by_key<B, F>( fn max_by_key<B, F>(
self, self,
key_by: F, key_by: F,
) -> impl Future<Output = Option<Self::Item>> [MaxByKeyFuture<Self, Self::Item, F>] ) -> MaxByKeyFuture<Self, Self::Item, F>
where where
Self: Sized, Self: Sized,
B: Ord, B: Ord,
@ -997,7 +913,7 @@ extension_trait! {
fn min_by<F>( fn min_by<F>(
self, self,
compare: F, compare: F,
) -> impl Future<Output = Option<Self::Item>> [MinByFuture<Self, F, Self::Item>] ) -> MinByFuture<Self, F, Self::Item>
where where
Self: Sized, Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> Ordering, F: FnMut(&Self::Item, &Self::Item) -> Ordering,
@ -1030,7 +946,7 @@ extension_trait! {
"#] "#]
fn max( fn max(
self, self,
) -> impl Future<Output = Option<Self::Item>> [MaxFuture<Self, Self::Item>] ) -> MaxFuture<Self, Self::Item>
where where
Self: Sized, Self: Sized,
Self::Item: Ord, Self::Item: Ord,
@ -1063,7 +979,7 @@ extension_trait! {
"#] "#]
fn min( fn min(
self, self,
) -> impl Future<Output = Option<Self::Item>> [MinFuture<Self, Self::Item>] ) -> MinFuture<Self, Self::Item>
where where
Self: Sized, Self: Sized,
Self::Item: Ord, Self::Item: Ord,
@ -1101,7 +1017,7 @@ extension_trait! {
fn max_by<F>( fn max_by<F>(
self, self,
compare: F, compare: F,
) -> impl Future<Output = Option<Self::Item>> [MaxByFuture<Self, F, Self::Item>] ) -> MaxByFuture<Self, F, Self::Item>
where where
Self: Sized, Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> Ordering, F: FnMut(&Self::Item, &Self::Item) -> Ordering,
@ -1165,7 +1081,7 @@ extension_trait! {
fn nth( fn nth(
&mut self, &mut self,
n: usize, n: usize,
) -> impl Future<Output = Option<Self::Item>> + '_ [NthFuture<'_, Self>] ) -> NthFuture<'_, Self>
where where
Self: Unpin + Sized, Self: Unpin + Sized,
{ {
@ -1221,7 +1137,7 @@ extension_trait! {
fn all<F>( fn all<F>(
&mut self, &mut self,
f: F, f: F,
) -> impl Future<Output = bool> + '_ [AllFuture<'_, Self, F, Self::Item>] ) -> AllFuture<'_, Self, F, Self::Item>
where where
Self: Unpin + Sized, Self: Unpin + Sized,
F: FnMut(Self::Item) -> bool, F: FnMut(Self::Item) -> bool,
@ -1270,7 +1186,7 @@ extension_trait! {
fn find<P>( fn find<P>(
&mut self, &mut self,
p: P, p: P,
) -> impl Future<Output = Option<Self::Item>> + '_ [FindFuture<'_, Self, P>] ) -> FindFuture<'_, Self, P>
where where
Self: Unpin + Sized, Self: Unpin + Sized,
P: FnMut(&Self::Item) -> bool, P: FnMut(&Self::Item) -> bool,
@ -1298,7 +1214,7 @@ extension_trait! {
fn find_map<F, B>( fn find_map<F, B>(
&mut self, &mut self,
f: F, f: F,
) -> impl Future<Output = Option<B>> + '_ [FindMapFuture<'_, Self, F>] ) -> FindMapFuture<'_, Self, F>
where where
Self: Unpin + Sized, Self: Unpin + Sized,
F: FnMut(Self::Item) -> Option<B>, F: FnMut(Self::Item) -> Option<B>,
@ -1332,7 +1248,7 @@ extension_trait! {
self, self,
init: B, init: B,
f: F, f: F,
) -> impl Future<Output = B> [FoldFuture<Self, F, B>] ) -> FoldFuture<Self, F, B>
where where
Self: Sized, Self: Sized,
F: FnMut(B, Self::Item) -> B, F: FnMut(B, Self::Item) -> B,
@ -1369,7 +1285,7 @@ extension_trait! {
fn partition<B, F>( fn partition<B, F>(
self, self,
f: F, f: F,
) -> impl Future<Output = (B, B)> [PartitionFuture<Self, F, B>] ) -> PartitionFuture<Self, F, B>
where where
Self: Sized, Self: Sized,
F: FnMut(&Self::Item) -> bool, F: FnMut(&Self::Item) -> bool,
@ -1405,7 +1321,7 @@ extension_trait! {
fn for_each<F>( fn for_each<F>(
self, self,
f: F, f: F,
) -> impl Future<Output = ()> [ForEachFuture<Self, F>] ) -> ForEachFuture<Self, F>
where where
Self: Sized, Self: Sized,
F: FnMut(Self::Item), F: FnMut(Self::Item),
@ -1461,7 +1377,7 @@ extension_trait! {
fn any<F>( fn any<F>(
&mut self, &mut self,
f: F, f: F,
) -> impl Future<Output = bool> + '_ [AnyFuture<'_, Self, F, Self::Item>] ) -> AnyFuture<'_, Self, F, Self::Item>
where where
Self: Unpin + Sized, Self: Unpin + Sized,
F: FnMut(Self::Item) -> bool, F: FnMut(Self::Item) -> bool,
@ -1697,7 +1613,7 @@ extension_trait! {
&mut self, &mut self,
init: T, init: T,
f: F, f: F,
) -> impl Future<Output = Result<T, E>> + '_ [TryFoldFuture<'_, Self, F, T>] ) -> TryFoldFuture<'_, Self, F, T>
where where
Self: Unpin + Sized, Self: Unpin + Sized,
F: FnMut(B, Self::Item) -> Result<T, E>, F: FnMut(B, Self::Item) -> Result<T, E>,
@ -1742,7 +1658,7 @@ extension_trait! {
fn try_for_each<F, E>( fn try_for_each<F, E>(
&mut self, &mut self,
f: F, f: F,
) -> impl Future<Output = E> + 'a [TryForEachFuture<'_, Self, F>] ) -> TryForEachFuture<'_, Self, F>
where where
Self: Unpin + Sized, Self: Unpin + Sized,
F: FnMut(Self::Item) -> Result<(), E>, F: FnMut(Self::Item) -> Result<(), E>,
@ -1824,7 +1740,7 @@ extension_trait! {
"#] "#]
#[cfg(feature = "unstable")] #[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(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 where
FromA: Default + Extend<A>, FromA: Default + Extend<A>,
FromB: Default + Extend<B>, FromB: Default + Extend<B>,
@ -1888,7 +1804,7 @@ extension_trait! {
#[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn collect<'a, B>( fn collect<'a, B>(
self, self,
) -> impl Future<Output = B> + 'a [Pin<Box<dyn Future<Output = B> + 'a + Send>>] ) -> Pin<Box<dyn Future<Output = B> + 'a + Send>>
where where
Self: Sized + 'a + Send, Self: Sized + 'a + Send,
B: FromStream<Self::Item>, B: FromStream<Self::Item>,
@ -1962,7 +1878,7 @@ extension_trait! {
fn partial_cmp<S>( fn partial_cmp<S>(
self, self,
other: S other: S
) -> impl Future<Output = Option<Ordering>> [PartialCmpFuture<Self, S>] ) -> PartialCmpFuture<Self, S>
where where
Self: Sized + Stream, Self: Sized + Stream,
S: Stream, S: Stream,
@ -2002,7 +1918,7 @@ extension_trait! {
fn position<P>( fn position<P>(
&mut self, &mut self,
predicate: P, predicate: P,
) -> impl Future<Output = Option<usize>> + '_ [PositionFuture<'_, Self, P>] ) -> PositionFuture<'_, Self, P>
where where
Self: Unpin + Sized, Self: Unpin + Sized,
P: FnMut(Self::Item) -> bool, P: FnMut(Self::Item) -> bool,
@ -2040,7 +1956,7 @@ extension_trait! {
fn cmp<S>( fn cmp<S>(
self, self,
other: S other: S
) -> impl Future<Output = Ordering> [CmpFuture<Self, S>] ) -> CmpFuture<Self, S>
where where
Self: Sized + Stream, Self: Sized + Stream,
S: Stream, S: Stream,
@ -2071,7 +1987,7 @@ extension_trait! {
"#] "#]
#[cfg(feature = "unstable")] #[cfg(feature = "unstable")]
#[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn count(self) -> impl Future<Output = usize> [CountFuture<Self>] fn count(self) -> CountFuture<Self>
where where
Self: Sized, Self: Sized,
{ {
@ -2106,7 +2022,7 @@ extension_trait! {
fn ne<S>( fn ne<S>(
self, self,
other: S other: S
) -> impl Future<Output = bool> [NeFuture<Self, S>] ) -> NeFuture<Self, S>
where where
Self: Sized, Self: Sized,
S: Sized + Stream, S: Sized + Stream,
@ -2143,7 +2059,7 @@ extension_trait! {
fn ge<S>( fn ge<S>(
self, self,
other: S other: S
) -> impl Future<Output = bool> [GeFuture<Self, S>] ) -> GeFuture<Self, S>
where where
Self: Sized + Stream, Self: Sized + Stream,
S: Stream, S: Stream,
@ -2180,7 +2096,7 @@ extension_trait! {
fn eq<S>( fn eq<S>(
self, self,
other: S other: S
) -> impl Future<Output = bool> [EqFuture<Self, S>] ) -> EqFuture<Self, S>
where where
Self: Sized + Stream, Self: Sized + Stream,
S: Sized + Stream, S: Sized + Stream,
@ -2217,7 +2133,7 @@ extension_trait! {
fn gt<S>( fn gt<S>(
self, self,
other: S other: S
) -> impl Future<Output = bool> [GtFuture<Self, S>] ) -> GtFuture<Self, S>
where where
Self: Sized + Stream, Self: Sized + Stream,
S: Stream, S: Stream,
@ -2254,7 +2170,7 @@ extension_trait! {
fn le<S>( fn le<S>(
self, self,
other: S other: S
) -> impl Future<Output = bool> [LeFuture<Self, S>] ) -> LeFuture<Self, S>
where where
Self: Sized + Stream, Self: Sized + Stream,
S: Stream, S: Stream,
@ -2291,7 +2207,7 @@ extension_trait! {
fn lt<S>( fn lt<S>(
self, self,
other: S other: S
) -> impl Future<Output = bool> [LtFuture<Self, S>] ) -> LtFuture<Self, S>
where where
Self: Sized + Stream, Self: Sized + Stream,
S: Stream, S: Stream,
@ -2335,7 +2251,7 @@ extension_trait! {
#[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn sum<'a, S>( fn sum<'a, S>(
self, self,
) -> impl Future<Output = S> + 'a [Pin<Box<dyn Future<Output = S> + 'a>>] ) -> Pin<Box<dyn Future<Output = S> + 'a>>
where where
Self: Sized + Stream<Item = S> + 'a, Self: Sized + Stream<Item = S> + 'a,
S: Sum<Self::Item>, S: Sum<Self::Item>,
@ -2381,7 +2297,7 @@ extension_trait! {
#[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[cfg_attr(feature = "docs", doc(cfg(unstable)))]
fn product<'a, P>( fn product<'a, P>(
self, self,
) -> impl Future<Output = P> + 'a [Pin<Box<dyn Future<Output = P> + 'a>>] ) -> Pin<Box<dyn Future<Output = P> + 'a>>
where where
Self: Sized + Stream<Item = P> + 'a, Self: Sized + Stream<Item = P> + 'a,
P: Product, P: Product,
@ -2390,39 +2306,5 @@ extension_trait! {
} }
} }
impl<S: Stream + Unpin + ?Sized> Stream for Box<S> { impl<T: Stream + ?Sized> StreamExt for T {}
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;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
unreachable!("this impl only appears in the rendered docs")
}
}
}

View file

@ -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)*);
};
}