Skip to content

Commit 0ce95b5

Browse files
author
Stjepan Glavina
committed
Refactor extension_trait
1 parent 5a184f2 commit 0ce95b5

File tree

14 files changed

+45
-60
lines changed

14 files changed

+45
-60
lines changed

src/fs/dir_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::future::Future;
21
use std::path::Path;
32

43
use cfg_if::cfg_if;
54

5+
use crate::future::Future;
66
use crate::io;
77
use crate::task::blocking;
88

src/fs/open_options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::future::Future;
21
use std::path::Path;
32

43
use cfg_if::cfg_if;
54

65
use crate::fs::File;
6+
use crate::future::Future;
77
use crate::io;
88
use crate::task::blocking;
99

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
@@ -41,7 +41,7 @@ extension_trait! {
4141
https://docs.rs/futures-preview/0.3.0-alpha.17/futures/io/trait.AsyncBufRead.html
4242
[provided methods]: #provided-methods
4343
"#]
44-
pub trait BufRead [BufReadExt: futures_io::AsyncBufRead] {
44+
pub trait BufRead {
4545
#[doc = r#"
4646
Returns the contents of the internal buffer, filling it with more data from the
4747
inner reader if it is empty.
@@ -64,7 +64,9 @@ extension_trait! {
6464
should no longer be returned in calls to `read`.
6565
"#]
6666
fn consume(self: Pin<&mut Self>, amt: usize);
67+
}
6768

69+
pub trait BufReadExt: futures_io::AsyncBufRead {
6870
#[doc = r#"
6971
Reads all bytes into `buf` until the delimiter `byte` or EOF is reached.
7072

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
@@ -47,7 +47,7 @@ extension_trait! {
4747
[`poll_flush`]: #tymethod.poll_flush
4848
[`poll_close`]: #tymethod.poll_close
4949
"#]
50-
pub trait Write [WriteExt: futures_io::AsyncWrite] {
50+
pub trait Write {
5151
#[doc = r#"
5252
Attempt to write bytes from `buf` into the object.
5353
"#]
@@ -78,7 +78,9 @@ extension_trait! {
7878
Attempt to close the object.
7979
"#]
8080
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>>;
81+
}
8182

83+
pub trait WriteExt: futures_io::AsyncWrite {
8284
#[doc = r#"
8385
Writes some bytes into the byte stream.
8486

src/stream/stream/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ cfg_if! {
8989
if #[cfg(any(feature = "unstable", feature = "docs"))] {
9090
use std::pin::Pin;
9191

92-
use std::future::Future;
92+
use crate::future::Future;
9393
use crate::stream::FromStream;
9494
}
9595
}
@@ -114,7 +114,7 @@ extension_trait! {
114114
https://docs.rs/futures-preview/0.3.0-alpha.17/futures/stream/trait.Stream.html
115115
[provided methods]: #provided-methods
116116
"#]
117-
pub trait Stream [StreamExt: futures_core::stream::Stream] {
117+
pub trait Stream {
118118
#[doc = r#"
119119
The type of items yielded by this stream.
120120
"#]
@@ -172,7 +172,9 @@ extension_trait! {
172172
```
173173
"#]
174174
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>;
175+
}
175176

177+
pub trait StreamExt: futures_core::stream::Stream {
176178
#[doc = r#"
177179
Advances the stream and returns the next value.
178180

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
@@ -81,7 +81,7 @@ pub(crate) mod blocking;
8181
#[inline]
8282
pub fn blocking<F, R>(future: F) -> task::JoinHandle<R>
8383
where
84-
F: std::future::Future<Output = R> + Send + 'static,
84+
F: crate::future::Future<Output = R> + Send + 'static,
8585
R: Send + 'static,
8686
{
8787
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)