microasync_util/io/read/
mod.rs

1extern crate std;
2
3use core::future::Future;
4use std::io;
5
6pub struct ReadFuture<'a, T>(&'a mut T, &'a mut [u8]);
7
8pub struct ReadExactFuture<'a, T>(&'a mut T, &'a mut [u8]);
9
10/// Trait that adds async variants of some std::io::Read functions.
11pub trait ReadAsync<'a, T, FRead, FReadExact>
12where
13    FRead: Future<Output = Result<usize, io::Error>> + 'a,
14{
15    /// Async equivalent to std::io::Read::read.
16    fn read(&'a mut self, bytes: &'a mut [u8]) -> FRead;
17
18    /// Async equivalent to std::io::Read::read_exact. This MAY read more bytes than the length of
19    /// the array if needed (namely, when using UdpSockets), but will avoid doing so to the best of its ability.
20    fn read_exact(&'a mut self, bytes: &'a mut [u8]) -> FReadExact;
21}
22
23impl<'a, T> ReadAsync<'a, usize, ReadFuture<'a, T>, ReadExactFuture<'a, T>> for T
24where
25    ReadFuture<'a, T>: Future<Output = Result<usize, io::Error>> + 'a,
26    ReadExactFuture<'a, T>: Future<Output = Result<(), io::Error>> + 'a,
27{
28    fn read(&'a mut self, bytes: &'a mut [u8]) -> ReadFuture<'a, T> {
29        ReadFuture(self, bytes)
30    }
31
32    fn read_exact(&'a mut self, bytes: &'a mut [u8]) -> ReadExactFuture<'a, T> {
33        ReadExactFuture(self, bytes)
34    }
35}
36
37pub mod file;
38pub mod tcpstream;
39pub mod udpsocket;