1mod bytes;
2mod chain;
3mod read;
4mod read_exact;
5mod read_to_end;
6mod read_to_string;
7mod read_vectored;
8mod take;
910use read::ReadFuture;
11use read_exact::ReadExactFuture;
12use read_to_end::{read_to_end_internal, ReadToEndFuture};
13use read_to_string::ReadToStringFuture;
14use read_vectored::ReadVectoredFuture;
1516use std::mem;
1718use crate::io::IoSliceMut;
1920pub use bytes::Bytes;
21pub use chain::Chain;
22pub use take::Take;
2324pub use futures_io::AsyncRead as Read;
2526#[doc = r#"
27 Extension methods for [`Read`].
2829 [`Read`]: ../trait.Read.html
30"#]
31pub trait ReadExt: Read {
32#[doc = r#"
33 Reads some bytes from the byte stream.
3435 Returns the number of bytes read from the start of the buffer.
3637 If the return value is `Ok(n)`, then it must be guaranteed that
38 `0 <= n <= buf.len()`. A nonzero `n` value indicates that the buffer has been
39 filled in with `n` bytes of data. If `n` is `0`, then it can indicate one of two
40 scenarios:
4142 1. This reader has reached its "end of file" and will likely no longer be able to
43 produce bytes. Note that this does not mean that the reader will always no
44 longer be able to produce bytes.
45 2. The buffer specified was 0 bytes in length.
4647 # Examples
4849 ```no_run
50 # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
51 #
52 use async_std::fs::File;
53 use async_std::prelude::*;
5455 let mut file = File::open("a.txt").await?;
5657 let mut buf = vec![0; 1024];
58 let n = file.read(&mut buf).await?;
59 #
60 # Ok(()) }) }
61 ```
62 "#]
63fn read<'a>(
64&'a mut self,
65 buf: &'a mut [u8],
66 ) -> ReadFuture<'a, Self>
67where
68Self: Unpin
69 {
70 ReadFuture { reader: self, buf }
71 }
7273#[doc = r#"
74 Like [`read`], except that it reads into a slice of buffers.
7576 Data is copied to fill each buffer in order, with the final buffer written to
77 possibly being only partially filled. This method must behave as a single call to
78 [`read`] with the buffers concatenated would.
7980 The default implementation calls [`read`] with either the first nonempty buffer
81 provided, or an empty one if none exists.
8283 [`read`]: #tymethod.read
84 "#]
85fn read_vectored<'a>(
86&'a mut self,
87 bufs: &'a mut [IoSliceMut<'a>],
88 ) -> ReadVectoredFuture<'a, Self>
89where
90Self: Unpin,
91 {
92 ReadVectoredFuture { reader: self, bufs }
93 }
9495#[doc = r#"
96 Reads all bytes from the byte stream.
9798 All bytes read from this stream will be appended to the specified buffer `buf`.
99 This function will continuously call [`read`] to append more data to `buf` until
100 [`read`] returns either `Ok(0)` or an error.
101102 If successful, this function will return the total number of bytes read.
103104 [`read`]: #tymethod.read
105106 # Examples
107108 ```no_run
109 # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
110 #
111 use async_std::fs::File;
112 use async_std::prelude::*;
113114 let mut file = File::open("a.txt").await?;
115116 let mut buf = Vec::new();
117 file.read_to_end(&mut buf).await?;
118 #
119 # Ok(()) }) }
120 ```
121 "#]
122fn read_to_end<'a>(
123&'a mut self,
124 buf: &'a mut Vec<u8>,
125 ) -> ReadToEndFuture<'a, Self>
126where
127Self: Unpin,
128 {
129let start_len = buf.len();
130 ReadToEndFuture {
131 reader: self,
132 buf,
133 start_len,
134 }
135 }
136137#[doc = r#"
138 Reads all bytes from the byte stream and appends them into a string.
139140 If successful, this function will return the number of bytes read.
141142 If the data in this stream is not valid UTF-8 then an error will be returned and
143 `buf` will be left unmodified.
144145 # Examples
146147 ```no_run
148 # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
149 #
150 use async_std::fs::File;
151 use async_std::prelude::*;
152153 let mut file = File::open("a.txt").await?;
154155 let mut buf = String::new();
156 file.read_to_string(&mut buf).await?;
157 #
158 # Ok(()) }) }
159 ```
160 "#]
161fn read_to_string<'a>(
162&'a mut self,
163 buf: &'a mut String,
164 ) -> ReadToStringFuture<'a, Self>
165where
166Self: Unpin,
167 {
168let start_len = buf.len();
169 ReadToStringFuture {
170 reader: self,
171 bytes: unsafe { mem::replace(buf.as_mut_vec(), Vec::new()) },
172 buf,
173 start_len,
174 }
175 }
176177#[doc = r#"
178 Reads the exact number of bytes required to fill `buf`.
179180 This function reads as many bytes as necessary to completely fill the specified
181 buffer `buf`.
182183 No guarantees are provided about the contents of `buf` when this function is
184 called, implementations cannot rely on any property of the contents of `buf` being
185 true. It is recommended that implementations only write data to `buf` instead of
186 reading its contents.
187188 If this function encounters an "end of file" before completely filling the buffer,
189 it returns an error of the kind [`ErrorKind::UnexpectedEof`]. The contents of
190 `buf` are unspecified in this case.
191192 If any other read error is encountered then this function immediately returns. The
193 contents of `buf` are unspecified in this case.
194195 If this function returns an error, it is unspecified how many bytes it has read,
196 but it will never read more than would be necessary to completely fill the buffer.
197198 [`ErrorKind::UnexpectedEof`]: enum.ErrorKind.html#variant.UnexpectedEof
199200 # Examples
201202 ```no_run
203 # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
204 #
205 use async_std::fs::File;
206 use async_std::prelude::*;
207208 let mut file = File::open("a.txt").await?;
209210 let mut buf = vec![0; 10];
211 file.read_exact(&mut buf).await?;
212 #
213 # Ok(()) }) }
214 ```
215 "#]
216fn read_exact<'a>(
217&'a mut self,
218 buf: &'a mut [u8],
219 ) -> ReadExactFuture<'a, Self>
220where
221Self: Unpin,
222 {
223 ReadExactFuture { reader: self, buf }
224 }
225226#[doc = r#"
227 Creates an adaptor which will read at most `limit` bytes from it.
228229 This function returns a new instance of `Read` which will read at most
230 `limit` bytes, after which it will always return EOF ([`Ok(0)`]). Any
231 read errors will not count towards the number of bytes read and future
232 calls to [`read`] may succeed.
233234 # Examples
235236 [`File`]s implement `Read`:
237238 [`File`]: ../fs/struct.File.html
239 [`Ok(0)`]: ../../std/result/enum.Result.html#variant.Ok
240 [`read`]: tymethod.read
241242 ```no_run
243 # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
244 #
245 use async_std::io::prelude::*;
246 use async_std::fs::File;
247248 let f = File::open("foo.txt").await?;
249 let mut buffer = [0; 5];
250251 // read at most five bytes
252 let mut handle = f.take(5);
253254 handle.read(&mut buffer).await?;
255 #
256 # Ok(()) }) }
257 ```
258 "#]
259fn take(self, limit: u64) -> Take<Self>
260where
261Self: Sized,
262 {
263 Take { inner: self, limit }
264 }
265266#[doc = r#"
267 Creates a "by reference" adaptor for this instance of `Read`.
268269 The returned adaptor also implements `Read` and will simply borrow this
270 current reader.
271272 # Examples
273274 [`File`][file]s implement `Read`:
275276 [file]: ../fs/struct.File.html
277278 ```no_run
279 # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
280 #
281 use async_std::prelude::*;
282 use async_std::fs::File;
283284 let mut f = File::open("foo.txt").await?;
285 let mut buffer = Vec::new();
286 let mut other_buffer = Vec::new();
287288 {
289 let reference = f.by_ref();
290291 // read at most 5 bytes
292 reference.take(5).read_to_end(&mut buffer).await?;
293294 } // drop our &mut reference so we can use f again
295296 // original file still usable, read the rest
297 f.read_to_end(&mut other_buffer).await?;
298 #
299 # Ok(()) }) }
300 ```
301 "#]
302fn by_ref(&mut self) -> &mut Self where Self: Sized { self }
303304305#[doc = r#"
306 Transforms this `Read` instance to a `Stream` over its bytes.
307308 The returned type implements `Stream` where the `Item` is
309 `Result<u8, io::Error>`.
310 The yielded item is `Ok` if a byte was successfully read and `Err`
311 otherwise. EOF is mapped to returning `None` from this iterator.
312313 # Examples
314315 [`File`][file]s implement `Read`:
316317 [file]: ../fs/struct.File.html
318319 ```no_run
320 # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
321 #
322 use async_std::prelude::*;
323 use async_std::fs::File;
324325 let f = File::open("foo.txt").await?;
326 let mut s = f.bytes();
327328 while let Some(byte) = s.next().await {
329 println!("{}", byte.unwrap());
330 }
331 #
332 # Ok(()) }) }
333 ```
334 "#]
335fn bytes(self) -> Bytes<Self> where Self: Sized {
336 Bytes { inner: self }
337 }
338339#[doc = r#"
340 Creates an adaptor which will chain this stream with another.
341342 The returned `Read` instance will first read all bytes from this object
343 until EOF is encountered. Afterwards the output is equivalent to the
344 output of `next`.
345346 # Examples
347348 [`File`][file]s implement `Read`:
349350 [file]: ../fs/struct.File.html
351352 ```no_run
353 # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
354 #
355 use async_std::prelude::*;
356 use async_std::fs::File;
357358 let f1 = File::open("foo.txt").await?;
359 let f2 = File::open("bar.txt").await?;
360361 let mut handle = f1.chain(f2);
362 let mut buffer = String::new();
363364 // read the value into a String. We could use any Read method here,
365 // this is just one example.
366 handle.read_to_string(&mut buffer).await?;
367 #
368 # Ok(()) }) }
369 ```
370 "#]
371fn chain<R: Read>(self, next: R) -> Chain<Self, R> where Self: Sized {
372 Chain { first: self, second: next, done_first: false }
373 }
374}
375376impl<T: Read + ?Sized> ReadExt for T {}
377378/// Initializes a buffer if necessary.
379///
380/// Currently, a buffer is always initialized because `read_initializer`
381/// feature is not stable.
382#[inline]
383unsafe fn initialize<R: futures_io::AsyncRead>(_reader: &R, buf: &mut [u8]) {
384 std::ptr::write_bytes(buf.as_mut_ptr(), 0, buf.len())
385}
386387#[cfg(all(test, not(target_os = "unknown")))]
388mod tests {
389use crate::io;
390use crate::prelude::*;
391392#[test]
393fn test_read_by_ref() {
394crate::task::block_on(async {
395let mut f = io::Cursor::new(vec![0u8, 1, 2, 3, 4, 5, 6, 7, 8]);
396let mut buffer = Vec::new();
397let mut other_buffer = Vec::new();
398399 {
400let reference = f.by_ref();
401402// read at most 5 bytes
403assert_eq!(reference.take(5).read_to_end(&mut buffer).await.unwrap(), 5);
404assert_eq!(&buffer, &[0, 1, 2, 3, 4])
405 } // drop our &mut reference so we can use f again
406407 // original file still usable, read the rest
408assert_eq!(f.read_to_end(&mut other_buffer).await.unwrap(), 4);
409assert_eq!(&other_buffer, &[5, 6, 7, 8]);
410 });
411 }
412}