1use std::io;
2use std::mem;
3
4use futures::{Future, Poll};
5
6use AsyncRead;
7
8#[derive(Debug)]
9enum State<R, T> {
10 Pending { rd: R, buf: T },
11 Empty,
12}
13
14pub fn read<R, T>(rd: R, buf: T) -> Read<R, T>
20where
21 R: AsyncRead,
22 T: AsMut<[u8]>,
23{
24 Read {
25 state: State::Pending { rd: rd, buf: buf },
26 }
27}
28
29#[derive(Debug)]
34pub struct Read<R, T> {
35 state: State<R, T>,
36}
37
38impl<R, T> Future for Read<R, T>
39where
40 R: AsyncRead,
41 T: AsMut<[u8]>,
42{
43 type Item = (R, T, usize);
44 type Error = io::Error;
45
46 fn poll(&mut self) -> Poll<(R, T, usize), io::Error> {
47 let nread = match self.state {
48 State::Pending {
49 ref mut rd,
50 ref mut buf,
51 } => try_ready!(rd.poll_read(&mut buf.as_mut()[..])),
52 State::Empty => panic!("poll a Read after it's done"),
53 };
54
55 match mem::replace(&mut self.state, State::Empty) {
56 State::Pending { rd, buf } => Ok((rd, buf, nread).into()),
57 State::Empty => panic!("invalid internal state"),
58 }
59 }
60}