Skip to content

Commit f6a2393

Browse files
bors[bot]Stjepan Glavina
and
Stjepan Glavina
authored
Merge #241
241: Simplify extension traits using a macro r=yoshuawuyts a=stjepang This PR would fix #235 Async methods in our extension traits are now written in the simpler `-> impl Future<Output = T> [ConcreteFuture<Self>]` style. At the same time, doc tests are used even when the `docs` feature is not enabled. Co-authored-by: Stjepan Glavina <[email protected]>
2 parents a2744e3 + 414fadd commit f6a2393

File tree

8 files changed

+1850
-1877
lines changed

8 files changed

+1850
-1877
lines changed

src/io/buf_read/mod.rs

+235-251
Large diffs are not rendered by default.

src/io/read/mod.rs

+252-276
Large diffs are not rendered by default.

src/io/seek.rs

+90-95
Original file line numberDiff line numberDiff line change
@@ -5,119 +5,114 @@ use cfg_if::cfg_if;
55
use crate::future::Future;
66
use crate::io::{self, SeekFrom};
77
use crate::task::{Context, Poll};
8+
use crate::utils::extension_trait;
89

910
cfg_if! {
1011
if #[cfg(feature = "docs")] {
1112
use std::ops::{Deref, DerefMut};
13+
}
14+
}
1215

13-
#[doc(hidden)]
14-
pub struct ImplFuture<T>(std::marker::PhantomData<T>);
15-
16-
/// Allows seeking through a byte stream.
17-
///
18-
/// This trait is a re-export of [`futures::io::AsyncSeek`] and is an async version of
19-
/// [`std::io::Seek`].
20-
///
21-
/// The [provided methods] do not really exist in the trait itself, but they become
22-
/// available when the prelude is imported:
23-
///
24-
/// ```
25-
/// # #[allow(unused_imports)]
26-
/// use async_std::prelude::*;
27-
/// ```
28-
///
29-
/// [`std::io::Seek`]: https://doc.rust-lang.org/std/io/trait.Seek.html
30-
/// [`futures::io::AsyncSeek`]:
31-
/// https://docs.rs/futures-preview/0.3.0-alpha.17/futures/io/trait.AsyncSeek.html
32-
/// [provided methods]: #provided-methods
33-
pub trait Seek {
34-
/// Attempt to seek to an offset, in bytes, in a stream.
35-
fn poll_seek(
36-
self: Pin<&mut Self>,
37-
cx: &mut Context<'_>,
38-
pos: SeekFrom,
39-
) -> Poll<io::Result<u64>>;
40-
41-
/// Seeks to a new position in a byte stream.
42-
///
43-
/// Returns the new position in the byte stream.
44-
///
45-
/// A seek beyond the end of stream is allowed, but behavior is defined by the
46-
/// implementation.
47-
///
48-
/// # Examples
49-
///
50-
/// ```no_run
51-
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
52-
/// #
53-
/// use async_std::fs::File;
54-
/// use async_std::io::SeekFrom;
55-
/// use async_std::prelude::*;
56-
///
57-
/// let mut file = File::open("a.txt").await?;
58-
///
59-
/// let file_len = file.seek(SeekFrom::End(0)).await?;
60-
/// #
61-
/// # Ok(()) }) }
62-
/// ```
63-
fn seek(&mut self, pos: SeekFrom) -> ImplFuture<io::Result<u64>>
64-
where
65-
Self: Unpin
66-
{
67-
unreachable!()
68-
}
69-
}
16+
extension_trait! {
17+
#[doc = r#"
18+
Allows seeking through a byte stream.
7019
71-
impl<T: Seek + Unpin + ?Sized> Seek for Box<T> {
72-
fn poll_seek(
73-
self: Pin<&mut Self>,
74-
cx: &mut Context<'_>,
75-
pos: SeekFrom,
76-
) -> Poll<io::Result<u64>> {
77-
unreachable!()
78-
}
79-
}
20+
This trait is a re-export of [`futures::io::AsyncSeek`] and is an async version of
21+
[`std::io::Seek`].
8022
81-
impl<T: Seek + Unpin + ?Sized> Seek for &mut T {
82-
fn poll_seek(
83-
self: Pin<&mut Self>,
84-
cx: &mut Context<'_>,
85-
pos: SeekFrom,
86-
) -> Poll<io::Result<u64>> {
87-
unreachable!()
88-
}
89-
}
23+
The [provided methods] do not really exist in the trait itself, but they become
24+
available when the prelude is imported:
25+
26+
```
27+
# #[allow(unused_imports)]
28+
use async_std::prelude::*;
29+
```
30+
31+
[`std::io::Seek`]: https://doc.rust-lang.org/std/io/trait.Seek.html
32+
[`futures::io::AsyncSeek`]:
33+
https://docs.rs/futures-preview/0.3.0-alpha.17/futures/io/trait.AsyncSeek.html
34+
[provided methods]: #provided-methods
35+
"#]
36+
pub trait Seek [SeekExt: futures_io::AsyncSeek] {
37+
#[doc = r#"
38+
Attempt to seek to an offset, in bytes, in a stream.
39+
"#]
40+
fn poll_seek(
41+
self: Pin<&mut Self>,
42+
cx: &mut Context<'_>,
43+
pos: SeekFrom,
44+
) -> Poll<io::Result<u64>>;
45+
46+
#[doc = r#"
47+
Seeks to a new position in a byte stream.
48+
49+
Returns the new position in the byte stream.
50+
51+
A seek beyond the end of stream is allowed, but behavior is defined by the
52+
implementation.
53+
54+
# Examples
55+
56+
```no_run
57+
# fn main() -> std::io::Result<()> { async_std::task::block_on(async {
58+
#
59+
use async_std::fs::File;
60+
use async_std::io::SeekFrom;
61+
use async_std::prelude::*;
9062
91-
impl<P> Seek for Pin<P>
63+
let mut file = File::open("a.txt").await?;
64+
65+
let file_len = file.seek(SeekFrom::End(0)).await?;
66+
#
67+
# Ok(()) }) }
68+
```
69+
"#]
70+
fn seek(
71+
&mut self,
72+
pos: SeekFrom,
73+
) -> impl Future<Output = io::Result<u64>> [SeekFuture<'_, Self>]
9274
where
93-
P: DerefMut + Unpin,
94-
<P as Deref>::Target: Seek,
75+
Self: Unpin,
9576
{
96-
fn poll_seek(
97-
self: Pin<&mut Self>,
98-
cx: &mut Context<'_>,
99-
pos: SeekFrom,
100-
) -> Poll<io::Result<u64>> {
101-
unreachable!()
102-
}
77+
SeekFuture { seeker: self, pos }
10378
}
104-
} else {
105-
pub use futures_io::AsyncSeek as Seek;
10679
}
107-
}
10880

109-
#[doc(hidden)]
110-
pub trait SeekExt: futures_io::AsyncSeek {
111-
fn seek(&mut self, pos: SeekFrom) -> SeekFuture<'_, Self>
81+
impl<T: Seek + Unpin + ?Sized> Seek for Box<T> {
82+
fn poll_seek(
83+
self: Pin<&mut Self>,
84+
cx: &mut Context<'_>,
85+
pos: SeekFrom,
86+
) -> Poll<io::Result<u64>> {
87+
unreachable!("this impl only appears in the rendered docs")
88+
}
89+
}
90+
91+
impl<T: Seek + Unpin + ?Sized> Seek for &mut T {
92+
fn poll_seek(
93+
self: Pin<&mut Self>,
94+
cx: &mut Context<'_>,
95+
pos: SeekFrom,
96+
) -> Poll<io::Result<u64>> {
97+
unreachable!("this impl only appears in the rendered docs")
98+
}
99+
}
100+
101+
impl<P> Seek for Pin<P>
112102
where
113-
Self: Unpin,
103+
P: DerefMut + Unpin,
104+
<P as Deref>::Target: Seek,
114105
{
115-
SeekFuture { seeker: self, pos }
106+
fn poll_seek(
107+
self: Pin<&mut Self>,
108+
cx: &mut Context<'_>,
109+
pos: SeekFrom,
110+
) -> Poll<io::Result<u64>> {
111+
unreachable!("this impl only appears in the rendered docs")
112+
}
116113
}
117114
}
118115

119-
impl<T: futures_io::AsyncSeek + ?Sized> SeekExt for T {}
120-
121116
#[doc(hidden)]
122117
#[allow(missing_debug_implementations)]
123118
pub struct SeekFuture<'a, T: Unpin + ?Sized> {

0 commit comments

Comments
 (0)