12
12
#[ cfg( feature = "alloc" ) ]
13
13
extern crate alloc;
14
14
15
+ use core:: future:: Future ;
16
+
15
17
mod impls;
16
18
17
19
pub use embedded_io:: {
@@ -46,7 +48,7 @@ pub trait Read: ErrorType {
46
48
/// the hardware with e.g. DMA.
47
49
///
48
50
/// Implementations should document whether they're actually side-effect-free on cancel or not.
49
- async fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > ;
51
+ fn read ( & mut self , buf : & mut [ u8 ] ) -> impl Future < Output = Result < usize , Self :: Error > > ;
50
52
51
53
/// Read the exact number of bytes required to fill `buf`.
52
54
///
@@ -55,18 +57,20 @@ pub trait Read: ErrorType {
55
57
///
56
58
/// This function is not side-effect-free on cancel (AKA "cancel-safe"), i.e. if you cancel (drop) a returned
57
59
/// future that hasn't completed yet, some bytes might have already been read, which will get lost.
58
- async fn read_exact ( & mut self , mut buf : & mut [ u8 ] ) -> Result < ( ) , ReadExactError < Self :: Error > > {
59
- while !buf. is_empty ( ) {
60
- match self . read ( buf) . await {
61
- Ok ( 0 ) => break ,
62
- Ok ( n) => buf = & mut buf[ n..] ,
63
- Err ( e) => return Err ( ReadExactError :: Other ( e) ) ,
60
+ fn read_exact ( & mut self , mut buf : & mut [ u8 ] ) -> impl Future < Output = Result < ( ) , ReadExactError < Self :: Error > > > {
61
+ async move {
62
+ while !buf. is_empty ( ) {
63
+ match self . read ( buf) . await {
64
+ Ok ( 0 ) => break ,
65
+ Ok ( n) => buf = & mut buf[ n..] ,
66
+ Err ( e) => return Err ( ReadExactError :: Other ( e) ) ,
67
+ }
68
+ }
69
+ if buf. is_empty ( ) {
70
+ Ok ( ( ) )
71
+ } else {
72
+ Err ( ReadExactError :: UnexpectedEof )
64
73
}
65
- }
66
- if buf. is_empty ( ) {
67
- Ok ( ( ) )
68
- } else {
69
- Err ( ReadExactError :: UnexpectedEof )
70
74
}
71
75
}
72
76
}
@@ -82,7 +86,7 @@ pub trait BufRead: ErrorType {
82
86
/// If the reader is at end-of-file (EOF), an empty slice is returned. There is no guarantee that a reader at EOF
83
87
/// will always be so in the future, for example a reader can stop being at EOF if another process appends
84
88
/// more bytes to the underlying file.
85
- async fn fill_buf ( & mut self ) -> Result < & [ u8 ] , Self :: Error > ;
89
+ fn fill_buf ( & mut self ) -> impl Future < Output = Result < & [ u8 ] , Self :: Error > > ;
86
90
87
91
/// Tell this buffer that `amt` bytes have been consumed from the buffer, so they should no longer be returned in calls to `fill_buf`.
88
92
fn consume ( & mut self , amt : usize ) ;
@@ -116,11 +120,13 @@ pub trait Write: ErrorType {
116
120
/// the hardware with e.g. DMA.
117
121
///
118
122
/// Implementations should document whether they're actually side-effect-free on cancel or not.
119
- async fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize , Self :: Error > ;
123
+ fn write ( & mut self , buf : & [ u8 ] ) -> impl Future < Output = Result < usize , Self :: Error > > ;
120
124
121
125
/// Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
122
- async fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
123
- Ok ( ( ) )
126
+ fn flush ( & mut self ) -> impl Future < Output = Result < ( ) , Self :: Error > > {
127
+ async {
128
+ Ok ( ( ) )
129
+ }
124
130
}
125
131
126
132
/// Write an entire buffer into this writer.
@@ -130,16 +136,18 @@ pub trait Write: ErrorType {
130
136
///
131
137
/// This function is not side-effect-free on cancel (AKA "cancel-safe"), i.e. if you cancel (drop) a returned
132
138
/// future that hasn't completed yet, some bytes might have already been written.
133
- async fn write_all ( & mut self , buf : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
134
- let mut buf = buf;
135
- while !buf. is_empty ( ) {
136
- match self . write ( buf) . await {
137
- Ok ( 0 ) => panic ! ( "write() returned Ok(0)" ) ,
138
- Ok ( n) => buf = & buf[ n..] ,
139
- Err ( e) => return Err ( e) ,
139
+ fn write_all ( & mut self , buf : & [ u8 ] ) -> impl Future < Output = Result < ( ) , Self :: Error > > {
140
+ async move {
141
+ let mut buf = buf;
142
+ while !buf. is_empty ( ) {
143
+ match self . write ( buf) . await {
144
+ Ok ( 0 ) => panic ! ( "write() returned Ok(0)" ) ,
145
+ Ok ( n) => buf = & buf[ n..] ,
146
+ Err ( e) => return Err ( e) ,
147
+ }
140
148
}
149
+ Ok ( ( ) )
141
150
}
142
- Ok ( ( ) )
143
151
}
144
152
}
145
153
@@ -148,52 +156,66 @@ pub trait Write: ErrorType {
148
156
/// This trait is the `embedded-io-async` equivalent of [`std::io::Seek`].
149
157
pub trait Seek : ErrorType {
150
158
/// Seek to an offset, in bytes, in a stream.
151
- async fn seek ( & mut self , pos : SeekFrom ) -> Result < u64 , Self :: Error > ;
159
+ fn seek ( & mut self , pos : SeekFrom ) -> impl Future < Output = Result < u64 , Self :: Error > > ;
152
160
153
161
/// Rewind to the beginning of a stream.
154
- async fn rewind ( & mut self ) -> Result < ( ) , Self :: Error > {
155
- self . seek ( SeekFrom :: Start ( 0 ) ) . await ?;
156
- Ok ( ( ) )
162
+ fn rewind ( & mut self ) -> impl Future < Output = Result < ( ) , Self :: Error > > {
163
+ async move {
164
+ self . seek ( SeekFrom :: Start ( 0 ) ) . await ?;
165
+ Ok ( ( ) )
166
+ }
157
167
}
158
168
159
169
/// Returns the current seek position from the start of the stream.
160
- async fn stream_position ( & mut self ) -> Result < u64 , Self :: Error > {
161
- self . seek ( SeekFrom :: Current ( 0 ) ) . await
170
+ fn stream_position ( & mut self ) -> impl Future < Output = Result < u64 , Self :: Error > > {
171
+ async move {
172
+ self . seek ( SeekFrom :: Current ( 0 ) ) . await
173
+ }
162
174
}
163
175
}
164
176
165
- impl < T : ?Sized + Read > Read for & mut T {
177
+ impl < T : ?Sized + Read + Send > Read for & mut T {
166
178
#[ inline]
167
- async fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
168
- T :: read ( self , buf) . await
179
+ fn read ( & mut self , buf : & mut [ u8 ] ) -> impl Future < Output = Result < usize , Self :: Error > > {
180
+ async {
181
+ T :: read ( self , buf) . await
182
+ }
169
183
}
170
184
}
171
185
172
- impl < T : ?Sized + BufRead > BufRead for & mut T {
173
- async fn fill_buf ( & mut self ) -> Result < & [ u8 ] , Self :: Error > {
174
- T :: fill_buf ( self ) . await
186
+ impl < T : ?Sized + BufRead + Send > BufRead for & mut T {
187
+ fn fill_buf ( & mut self ) -> impl Future < Output = Result < & [ u8 ] , Self :: Error > > {
188
+ async {
189
+ T :: fill_buf ( self ) . await
190
+ }
175
191
}
176
192
177
193
fn consume ( & mut self , amt : usize ) {
178
194
T :: consume ( self , amt) ;
179
195
}
180
196
}
181
197
182
- impl < T : ?Sized + Write > Write for & mut T {
198
+ impl < T : ?Sized + Write + Send > Write for & mut T {
183
199
#[ inline]
184
- async fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize , Self :: Error > {
185
- T :: write ( self , buf) . await
200
+ fn write ( & mut self , buf : & [ u8 ] ) -> impl Future < Output = Result < usize , Self :: Error > > {
201
+ async {
202
+ T :: write ( self , buf) . await
203
+ }
186
204
}
187
205
188
206
#[ inline]
189
- async fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
190
- T :: flush ( self ) . await
207
+ fn flush ( & mut self ) -> impl Future < Output = Result < ( ) , Self :: Error > > {
208
+ async {
209
+ T :: flush ( self ) . await
210
+ }
191
211
}
192
212
}
193
213
194
214
impl < T : ?Sized + Seek > Seek for & mut T {
195
215
#[ inline]
196
- async fn seek ( & mut self , pos : SeekFrom ) -> Result < u64 , Self :: Error > {
197
- T :: seek ( self , pos) . await
216
+ fn seek ( & mut self , pos : SeekFrom ) -> impl Future < Output = Result < u64 , Self :: Error > > {
217
+ async move {
218
+ T :: seek ( self , pos) . await
219
+ }
198
220
}
199
221
}
0 commit comments