1use std::mem;
2
3use futures::{Future, Poll};
4
5enum State<R, T> {
6 Pending {
7 rd: R,
8 buf: T,
9 },
10 Empty,
11}
12
13pub fn read<R, T>(rd: R, buf: T) -> Read<R, T>
19 where R: ::std::io::Read,
20 T: AsMut<[u8]>
21{
22 Read { state: State::Pending { rd: rd, buf: buf } }
23}
24
25#[must_use = "futures do nothing unless polled"]
30pub struct Read<R, T> {
31 state: State<R, T>,
32}
33
34impl<R, T> Future for Read<R, T>
35 where R: ::std::io::Read,
36 T: AsMut<[u8]>
37{
38 type Item = (R, T, usize);
39 type Error = ::std::io::Error;
40
41 fn poll(&mut self) -> Poll<(R, T, usize), ::std::io::Error> {
42 let nread = match self.state {
43 State::Pending { ref mut rd, ref mut buf } => try_nb!(rd.read(&mut buf.as_mut()[..])),
44 State::Empty => panic!("poll a Read after it's done"),
45 };
46
47 match mem::replace(&mut self.state, State::Empty) {
48 State::Pending { rd, buf } => Ok((rd, buf, nread).into()),
49 State::Empty => panic!("invalid internal state"),
50 }
51 }
52}