@@ -5,119 +5,114 @@ use cfg_if::cfg_if;
5
5
use crate :: future:: Future ;
6
6
use crate :: io:: { self , SeekFrom } ;
7
7
use crate :: task:: { Context , Poll } ;
8
+ use crate :: utils:: extension_trait;
8
9
9
10
cfg_if ! {
10
11
if #[ cfg( feature = "docs" ) ] {
11
12
use std:: ops:: { Deref , DerefMut } ;
13
+ }
14
+ }
12
15
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.
70
19
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`].
80
22
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::*;
90
62
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 >]
92
74
where
93
- P : DerefMut + Unpin ,
94
- <P as Deref >:: Target : Seek ,
75
+ Self : Unpin ,
95
76
{
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 }
103
78
}
104
- } else {
105
- pub use futures_io:: AsyncSeek as Seek ;
106
79
}
107
- }
108
80
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 >
112
102
where
113
- Self : Unpin ,
103
+ P : DerefMut + Unpin ,
104
+ <P as Deref >:: Target : Seek ,
114
105
{
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
+ }
116
113
}
117
114
}
118
115
119
- impl < T : futures_io:: AsyncSeek + ?Sized > SeekExt for T { }
120
-
121
116
#[ doc( hidden) ]
122
117
#[ allow( missing_debug_implementations) ]
123
118
pub struct SeekFuture < ' a , T : Unpin + ?Sized > {
0 commit comments