Skip to content

Commit c78dbdd

Browse files
Stjepan Glavinayoshuawuyts
authored andcommitted
Refactor extension_trait
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent e2db765 commit c78dbdd

File tree

13 files changed

+45
-58
lines changed

13 files changed

+45
-58
lines changed

src/fs/dir_builder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::future::Future;
22

3+
use cfg_if::cfg_if;
4+
35
use crate::io;
46
use crate::path::Path;
57
use crate::task::blocking;

src/future/future.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension_trait! {
2929
3030
[`Waker`]: ../task/struct.Waker.html
3131
"#]
32-
pub trait Future [FutureExt: std::future::Future] {
32+
pub trait Future {
3333
#[doc = r#"
3434
The type of value produced on completion.
3535
"#]
@@ -101,4 +101,7 @@ extension_trait! {
101101
"#]
102102
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output>;
103103
}
104+
105+
pub trait FutureExt: std::future::Future {
106+
}
104107
}

src/io/buf_read/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extension_trait! {
4444
https://docs.rs/futures-preview/0.3.0-alpha.17/futures/io/trait.AsyncBufRead.html
4545
[provided methods]: #provided-methods
4646
"#]
47-
pub trait BufRead [BufReadExt: futures_io::AsyncBufRead] {
47+
pub trait BufRead {
4848
#[doc = r#"
4949
Returns the contents of the internal buffer, filling it with more data from the
5050
inner reader if it is empty.
@@ -67,7 +67,9 @@ extension_trait! {
6767
should no longer be returned in calls to `read`.
6868
"#]
6969
fn consume(self: Pin<&mut Self>, amt: usize);
70+
}
7071

72+
pub trait BufReadExt: futures_io::AsyncBufRead {
7173
#[doc = r#"
7274
Reads all bytes into `buf` until the delimiter `byte` or EOF is reached.
7375

src/io/read/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extension_trait! {
5050
[`poll_read`]: #tymethod.poll_read
5151
[`poll_read_vectored`]: #method.poll_read_vectored
5252
"#]
53-
pub trait Read [ReadExt: futures_io::AsyncRead] {
53+
pub trait Read {
5454
#[doc = r#"
5555
Attempt to read from the `AsyncRead` into `buf`.
5656
"#]
@@ -70,7 +70,9 @@ extension_trait! {
7070
) -> Poll<io::Result<usize>> {
7171
unreachable!("this impl only appears in the rendered docs")
7272
}
73+
}
7374

75+
pub trait ReadExt: futures_io::AsyncRead {
7476
#[doc = r#"
7577
Reads some bytes from the byte stream.
7678

src/io/seek.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extension_trait! {
3333
https://docs.rs/futures-preview/0.3.0-alpha.17/futures/io/trait.AsyncSeek.html
3434
[provided methods]: #provided-methods
3535
"#]
36-
pub trait Seek [SeekExt: futures_io::AsyncSeek] {
36+
pub trait Seek {
3737
#[doc = r#"
3838
Attempt to seek to an offset, in bytes, in a stream.
3939
"#]
@@ -42,7 +42,9 @@ extension_trait! {
4242
cx: &mut Context<'_>,
4343
pos: SeekFrom,
4444
) -> Poll<io::Result<u64>>;
45+
}
4546

47+
pub trait SeekExt: futures_io::AsyncSeek {
4648
#[doc = r#"
4749
Seeks to a new position in a byte stream.
4850
@@ -70,7 +72,7 @@ extension_trait! {
7072
fn seek(
7173
&mut self,
7274
pos: SeekFrom,
73-
) -> impl Future<Output = io::Result<u64>> [SeekFuture<'_, Self>]
75+
) -> impl Future<Output = io::Result<u64>> + '_ [SeekFuture<'_, Self>]
7476
where
7577
Self: Unpin,
7678
{

src/io/write/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extension_trait! {
4949
[`poll_flush`]: #tymethod.poll_flush
5050
[`poll_close`]: #tymethod.poll_close
5151
"#]
52-
pub trait Write [WriteExt: futures_io::AsyncWrite] {
52+
pub trait Write {
5353
#[doc = r#"
5454
Attempt to write bytes from `buf` into the object.
5555
"#]
@@ -80,7 +80,9 @@ extension_trait! {
8080
Attempt to close the object.
8181
"#]
8282
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>>;
83+
}
8384

85+
pub trait WriteExt: futures_io::AsyncWrite {
8486
#[doc = r#"
8587
Writes some bytes into the byte stream.
8688

src/stream/stream/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ cfg_if! {
9595

9696
use std::pin::Pin;
9797

98-
use std::future::Future;
98+
use crate::future::Future;
9999
use crate::stream::FromStream;
100100

101101
pub use merge::Merge;
@@ -122,7 +122,7 @@ extension_trait! {
122122
https://docs.rs/futures-preview/0.3.0-alpha.17/futures/stream/trait.Stream.html
123123
[provided methods]: #provided-methods
124124
"#]
125-
pub trait Stream [StreamExt: futures_core::stream::Stream] {
125+
pub trait Stream {
126126
#[doc = r#"
127127
The type of items yielded by this stream.
128128
"#]
@@ -180,7 +180,9 @@ extension_trait! {
180180
```
181181
"#]
182182
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
183+
}
183184

185+
pub trait StreamExt: futures_core::stream::Stream {
184186
#[doc = r#"
185187
Advances the stream and returns the next value.
186188

src/task/blocking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! A thread pool for running blocking functions asynchronously.
22
3-
use std::future::Future;
43
use std::sync::atomic::{AtomicU64, Ordering};
54
use std::thread;
65
use std::time::Duration;
76

87
use crossbeam_channel::{bounded, Receiver, Sender};
98
use lazy_static::lazy_static;
109

10+
use crate::future::Future;
1111
use crate::task::task::{JoinHandle, Tag};
1212
use crate::utils::abort_on_panic;
1313

src/task/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ cfg_if::cfg_if! {
8686
#[inline]
8787
pub fn blocking<F, R>(future: F) -> task::JoinHandle<R>
8888
where
89-
F: std::future::Future<Output = R> + Send + 'static,
89+
F: crate::future::Future<Output = R> + Send + 'static,
9090
R: Send + 'static,
9191
{
9292
blocking::spawn(future)

src/task/pool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::future::Future;
21
use std::iter;
32
use std::thread;
43

@@ -11,6 +10,7 @@ use super::task;
1110
use super::task_local;
1211
use super::worker;
1312
use super::{Builder, JoinHandle};
13+
use crate::future::Future;
1414
use crate::utils::abort_on_panic;
1515

1616
/// Spawns a task.

src/task/task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::fmt;
2-
use std::future::Future;
32
use std::i64;
43
use std::mem;
54
use std::num::NonZeroU64;
@@ -8,6 +7,7 @@ use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
87
use std::sync::Arc;
98

109
use super::task_local;
10+
use crate::future::Future;
1111
use crate::task::{Context, Poll};
1212

1313
/// A handle to a task.

src/task/task_local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use std::sync::Mutex;
77
use lazy_static::lazy_static;
88

99
use super::worker;
10+
use crate::future::Future;
1011
use crate::utils::abort_on_panic;
11-
use std::future::Future;
1212

1313
/// Declares task-local values.
1414
///

src/utils.rs

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ macro_rules! extension_trait {
3636
// - `$name`: trait name that gets rendered in the docs
3737
// - `$ext`: name of the hidden extension trait
3838
// - `$base`: base trait from the `futures` crate
39-
$(#[$attr:meta])*
40-
pub trait $name:ident [$ext:ident: $base:path] {
41-
$($body:tt)*
39+
#[doc = $doc:tt]
40+
pub trait $name:ident {
41+
$($body_base:tt)*
42+
}
43+
44+
pub trait $ext:ident: $base:path {
45+
$($body_ext:tt)*
4246
}
4347

4448
// Shim trait impls that only appear in docs.
@@ -58,21 +62,21 @@ macro_rules! extension_trait {
5862
pub struct ImplFuture<'a, T>(std::marker::PhantomData<&'a T>);
5963
}
6064

61-
// Render a fake trait containing all methods from the base trait and the extension trait.
65+
// Render a fake trait combining the bodies of the base trait and the extension trait.
6266
#[cfg(feature = "docs")]
63-
$(#[$attr])*
67+
#[doc = $doc]
6468
pub trait $name {
65-
extension_trait!(@doc () $($body)*);
69+
extension_trait!(@doc () $($body_base)* $($body_ext)*);
6670
}
6771

6872
// When not rendering docs, re-export the base trait from the futures crate.
6973
#[cfg(not(feature = "docs"))]
7074
pub use $base as $name;
7175

7276
// The extension trait that adds methods to any type implementing the base trait.
73-
$(#[$attr])*
77+
/// Extension trait.
7478
pub trait $ext: $base {
75-
extension_trait!(@ext () $($body)*);
79+
extension_trait!(@ext () $($body_ext)*);
7680
}
7781

7882
// Blanket implementation of the extension trait for any type implementing the base trait.
@@ -82,47 +86,15 @@ macro_rules! extension_trait {
8286
$(#[cfg(feature = "docs")] $imp)*
8387
};
8488

85-
// Parse an associated type.
86-
(@doc ($($head:tt)*) type $name:ident; $($tail:tt)*) => {
87-
extension_trait!(@doc ($($head)* type $name;) $($tail)*);
88-
};
89-
(@ext ($($head:tt)*) type $ident:ty; $($tail:tt)*) => {
90-
extension_trait!(@ext ($($head)*) $($tail)*);
91-
};
92-
93-
// Parse a required method.
94-
(@doc ($($head:tt)*) fn $name:ident $args:tt $(-> $ret:ty)?; $($tail:tt)*) => {
95-
extension_trait!(@doc ($($head)* fn $name $args $(-> $ret)?;) $($tail)*);
96-
};
97-
(@ext ($($head:tt)*) fn $name:ident $args:tt $(-> $ret:ty)?; $($tail:tt)*) => {
98-
extension_trait!(@ext ($($head)*) $($tail)*);
99-
};
100-
101-
// Parse a provided method that exists in the base trait.
102-
(@doc ($($head:tt)*) fn $name:ident $args:tt $(-> $ret:ty)? { $($body:tt)* } $($tail:tt)*) => {
103-
extension_trait!(@doc ($($head)* fn $name $args $(-> $ret)? { $($body)* }) $($tail)*);
104-
};
105-
(@ext ($($head:tt)*) fn $name:ident $args:tt $(-> $ret:ty)? { $($body:tt)* } $($tail:tt)*) => {
106-
extension_trait!(@ext ($($head)*) $($tail)*);
107-
};
108-
109-
// Parse the return type in an extension method where the future doesn't borrow.
110-
(@doc ($($head:tt)*) -> impl Future<Output = $out:ty> [$f:ty] $($tail:tt)*) => {
111-
extension_trait!(@doc ($($head)* -> owned::ImplFuture<$out>) $($tail)*);
112-
};
113-
(@ext ($($head:tt)*) -> impl Future<Output = $out:ty> [$f:ty] $($tail:tt)*) => {
114-
extension_trait!(@ext ($($head)* -> $f) $($tail)*);
115-
};
116-
117-
// Parse the return type in an extension method where the future borrows its environment.
118-
(@doc ($($head:tt)*) -> impl Future<Output = $out:ty> + $lt:lifetime [$f:ty] $($tail:tt)*) => {
119-
extension_trait!(@doc ($($head)* -> borrowed::ImplFuture<$lt, $out>) $($tail)*);
89+
// Parse the return type in an extension method.
90+
(@doc ($($head:tt)*) -> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*) => {
91+
extension_trait!(@doc ($($head)* -> borrowed::ImplFuture<$($lt,)? $out>) $($tail)*);
12092
};
121-
(@ext ($($head:tt)*) -> impl Future<Output = $out:ty> + $lt:lifetime [$f:ty] $($tail:tt)*) => {
93+
(@ext ($($head:tt)*) -> impl Future<Output = $out:ty> $(+ $lt:lifetime)? [$f:ty] $($tail:tt)*) => {
12294
extension_trait!(@ext ($($head)* -> $f) $($tail)*);
12395
};
12496

125-
// Parse a token that doesn't fit into any of the previous patterns.
97+
// Parse a token.
12698
(@doc ($($head:tt)*) $token:tt $($tail:tt)*) => {
12799
extension_trait!(@doc ($($head)* $token) $($tail)*);
128100
};

0 commit comments

Comments
 (0)