Skip to content

Expose extension traits in preludes #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/future/future/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ extension_trait! {
"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
Expand All @@ -36,6 +44,9 @@ extension_trait! {
`.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#"
Expand Down Expand Up @@ -110,6 +121,11 @@ extension_trait! {
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output>;
}

#[doc = r#"
Extension methods for [`Future`].

[`Future`]: ../future/trait.Future.html
"#]
pub trait FutureExt: std::future::Future {
/// Returns a Future that delays execution for a specified time.
///
Expand Down
9 changes: 8 additions & 1 deletion src/io/buf_read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension_trait! {
[`std::io::BufRead`].

The [provided methods] do not really exist in the trait itself, but they become
available when the prelude is imported:
available when [`BufReadExt`] from the [prelude] is imported:

```
# #[allow(unused_imports)]
Expand All @@ -36,6 +36,8 @@ extension_trait! {
[`futures::io::AsyncBufRead`]:
https://docs.rs/futures-preview/0.3.0-alpha.17/futures/io/trait.AsyncBufRead.html
[provided methods]: #provided-methods
[`BufReadExt`]: ../io/prelude/trait.BufReadExt.html
[prelude]: ../prelude/index.html
"#]
pub trait BufRead {
#[doc = r#"
Expand All @@ -62,6 +64,11 @@ extension_trait! {
fn consume(self: Pin<&mut Self>, amt: usize);
}

#[doc = r#"
Extension methods for [`BufRead`].

[`BufRead`]: ../trait.BufRead.html
"#]
pub trait BufReadExt: futures_io::AsyncBufRead {
#[doc = r#"
Reads all bytes into `buf` until the delimiter `byte` or EOF is reached.
Expand Down
18 changes: 9 additions & 9 deletions src/io/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! The async I/O Prelude
//! The async I/O prelude.
//!
//! The purpose of this module is to alleviate imports of many common I/O traits
//! by adding a glob import to the top of I/O heavy modules:
Expand All @@ -17,11 +17,11 @@ pub use crate::io::Seek;
#[doc(no_inline)]
pub use crate::io::Write;

#[doc(hidden)]
pub use crate::io::buf_read::BufReadExt as _;
#[doc(hidden)]
pub use crate::io::read::ReadExt as _;
#[doc(hidden)]
pub use crate::io::seek::SeekExt as _;
#[doc(hidden)]
pub use crate::io::write::WriteExt as _;
#[doc(inline)]
pub use crate::io::buf_read::BufReadExt;
#[doc(inline)]
pub use crate::io::read::ReadExt;
#[doc(inline)]
pub use crate::io::seek::SeekExt;
#[doc(inline)]
pub use crate::io::write::WriteExt;
9 changes: 8 additions & 1 deletion src/io/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension_trait! {
[`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 the prelude is imported:
trait itself, but they become available when [`ReadExt`] from the [prelude] is imported:

```
# #[allow(unused_imports)]
Expand All @@ -43,6 +43,8 @@ extension_trait! {
https://docs.rs/futures-preview/0.3.0-alpha.17/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#"
Expand All @@ -66,6 +68,11 @@ extension_trait! {
}
}

#[doc = r#"
Extension methods for [`Read`].

[`Read`]: ../trait.Read.html
"#]
pub trait ReadExt: futures_io::AsyncRead {
#[doc = r#"
Reads some bytes from the byte stream.
Expand Down
9 changes: 8 additions & 1 deletion src/io/seek/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension_trait! {
[`std::io::Seek`].

The [provided methods] do not really exist in the trait itself, but they become
available when the prelude is imported:
available when [`SeekExt`] the [prelude] is imported:

```
# #[allow(unused_imports)]
Expand All @@ -29,6 +29,8 @@ extension_trait! {
[`futures::io::AsyncSeek`]:
https://docs.rs/futures-preview/0.3.0-alpha.17/futures/io/trait.AsyncSeek.html
[provided methods]: #provided-methods
[`SeekExt`]: ../io/prelude/trait.SeekExt.html
[prelude]: ../prelude/index.html
"#]
pub trait Seek {
#[doc = r#"
Expand All @@ -41,6 +43,11 @@ extension_trait! {
) -> Poll<io::Result<u64>>;
}

#[doc = r#"
Extension methods for [`Seek`].

[`Seek`]: ../trait.Seek.html
"#]
pub trait SeekExt: futures_io::AsyncSeek {
#[doc = r#"
Seeks to a new position in a byte stream.
Expand Down
2 changes: 1 addition & 1 deletion src/io/stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl Stdin {
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::io;
/// use crate::async_std::prelude::*;
/// use async_std::prelude::*;
///
/// let mut buffer = String::new();
///
Expand Down
9 changes: 8 additions & 1 deletion src/io/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension_trait! {

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
the prelude is imported:
[`WriteExt`] from the [prelude] is imported:

```
# #[allow(unused_imports)]
Expand All @@ -40,6 +40,8 @@ extension_trait! {
[`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#"
Expand Down Expand Up @@ -74,6 +76,11 @@ extension_trait! {
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>>;
}

#[doc = r#"
Extension methods for [`Write`].

[`Write`]: ../trait.Write.html
"#]
pub trait WriteExt: futures_io::AsyncWrite {
#[doc = r#"
Writes some bytes into the byte stream.
Expand Down
32 changes: 17 additions & 15 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@

#[doc(no_inline)]
pub use crate::future::Future;
#[doc(no_inline)]
pub use crate::stream::Stream;
#[doc(no_inline)]
pub use crate::task_local;

#[doc(inline)]
pub use crate::future::future::FutureExt;
#[doc(inline)]
pub use crate::stream::stream::StreamExt;

#[doc(no_inline)]
pub use crate::io::BufRead as _;
#[doc(no_inline)]
Expand All @@ -21,23 +31,15 @@ pub use crate::io::Read as _;
pub use crate::io::Seek as _;
#[doc(no_inline)]
pub use crate::io::Write as _;

#[doc(no_inline)]
pub use crate::stream::Stream;
pub use crate::io::prelude::BufReadExt as _;
#[doc(no_inline)]
pub use crate::task_local;

#[doc(hidden)]
pub use crate::future::future::FutureExt as _;
#[doc(hidden)]
pub use crate::io::buf_read::BufReadExt as _;
#[doc(hidden)]
pub use crate::io::read::ReadExt as _;
#[doc(hidden)]
pub use crate::io::seek::SeekExt as _;
#[doc(hidden)]
pub use crate::io::write::WriteExt as _;
#[doc(hidden)]
pub use crate::stream::stream::StreamExt as _;
pub use crate::io::prelude::ReadExt as _;
#[doc(no_inline)]
pub use crate::io::prelude::SeekExt as _;
#[doc(no_inline)]
pub use crate::io::prelude::WriteExt as _;

cfg_unstable! {
#[doc(no_inline)]
Expand Down
6 changes: 2 additions & 4 deletions src/stream/from_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ use std::pin::Pin;
///
/// ```
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// use crate::async_std::stream::FromStream;
/// use async_std::prelude::*;
/// use async_std::stream;
/// use async_std::stream::{self, FromStream};
///
/// let five_fives = stream::repeat(5).take(5);
///
Expand Down Expand Up @@ -117,9 +116,8 @@ pub trait FromStream<T> {
///
/// ```
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// use crate::async_std::stream::FromStream;
/// use async_std::prelude::*;
/// use async_std::stream;
/// use async_std::stream::{self, FromStream};
///
/// let five_fives = stream::repeat(5).take(5);
///
Expand Down
9 changes: 8 additions & 1 deletion src/stream/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ extension_trait! {
[`std::iter::Iterator`].

The [provided methods] do not really exist in the trait itself, but they become
available when the prelude is imported:
available when [`StreamExt`] from the [prelude] is imported:

```
# #[allow(unused_imports)]
Expand All @@ -149,6 +149,8 @@ extension_trait! {
[`futures::stream::Stream`]:
https://docs.rs/futures-preview/0.3.0-alpha.17/futures/stream/trait.Stream.html
[provided methods]: #provided-methods
[`StreamExt`]: ../prelude/trait.StreamExt.html
[prelude]: ../prelude/index.html
"#]
pub trait Stream {
#[doc = r#"
Expand Down Expand Up @@ -210,6 +212,11 @@ extension_trait! {
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
}

#[doc = r#"
Extension methods for [`Stream`].

[`Stream`]: ../stream/trait.Stream.html
"#]
pub trait StreamExt: futures_core::stream::Stream {
#[doc = r#"
Advances the stream and returns the next value.
Expand Down
12 changes: 5 additions & 7 deletions src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,19 @@
//! inter-task synchronisation mechanism, at the cost of some
//! extra memory.
//!
//! - [`Mutex`]: Mutual Exclusion mechanism, which ensures that at
//! - [`Mutex`]: Mutual exclusion mechanism, which ensures that at
//! most one task at a time is able to access some data.
//!
//! - [`RwLock`]: Provides a mutual exclusion mechanism which allows
//! multiple readers at the same time, while allowing only one
//! writer at a time. In some cases, this can be more efficient than
//! a mutex.
//!
//! [`Arc`]: crate::sync::Arc
//! [`Barrier`]: crate::sync::Barrier
//! [`Condvar`]: crate::sync::Condvar
//! [`Arc`]: struct.Arc.html
//! [`Barrier`]: struct.Barrier.html
//! [`channel`]: fn.channel.html
//! [`Mutex`]: crate::sync::Mutex
//! [`Once`]: crate::sync::Once
//! [`RwLock`]: crate::sync::RwLock
//! [`Mutex`]: struct.Mutex.html
//! [`RwLock`]: struct.RwLock.html
//!
//! # Examples
//!
Expand Down
7 changes: 4 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ macro_rules! extension_trait {
$($body_base:tt)*
}

#[doc = $doc_ext:tt]
pub trait $ext:ident: $base:path {
$($body_ext:tt)*
}
Expand Down Expand Up @@ -177,13 +178,13 @@ macro_rules! extension_trait {
pub use $base as $name;

// The extension trait that adds methods to any type implementing the base trait.
/// Extension trait.
pub trait $ext: $base {
#[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: $base + ?Sized> $ext for T {}
impl<T: $name + ?Sized> $ext for T {}

// Shim trait impls that only appear in docs.
$(#[cfg(feature = "docs")] $imp)*
Expand Down