Skip to content

Commit

Permalink
Merge pull request rust-lang#84 from jeandudey/return-a
Browse files Browse the repository at this point in the history
Return A type parameter on ReadToEnd
  • Loading branch information
alexcrichton authored Aug 21, 2016
2 parents 761fd93 + e17cf71 commit bdeff1b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
40 changes: 29 additions & 11 deletions futures-io/src/read_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ use futures::{Poll, Future};
///
/// Created by the `read_to_end` function.
pub struct ReadToEnd<A> {
a: A,
buf: Vec<u8>,
state: State<A>,
}

enum State<A> {
Reading {
a: A,
buf: Vec<u8>,
},
Empty,
}

/// Creates a future which will read all the bytes associated with the I/O
Expand All @@ -23,22 +30,33 @@ pub fn read_to_end<A>(a: A, buf: Vec<u8>) -> ReadToEnd<A>
where A: Read,
{
ReadToEnd {
a: a,
buf: buf,
state: State::Reading {
a: a,
buf: buf,
}
}
}

impl<A> Future for ReadToEnd<A>
where A: Read,
{
type Item = Vec<u8>;
type Item = (A, Vec<u8>);
type Error = io::Error;

fn poll(&mut self) -> Poll<Vec<u8>, io::Error> {
// If we get `Ok`, then we know the stream hit EOF and we're done. If we
// hit "would block" then all the read data so far is in our buffer, and
// otherwise we propagate errors.
try_nb!(self.a.read_to_end(&mut self.buf));
Poll::Ok(mem::replace(&mut self.buf, Vec::new()))
fn poll(&mut self) -> Poll<(A, Vec<u8>), io::Error> {
match self.state {
State::Reading { ref mut a, ref mut buf } => {
// If we get `Ok`, then we know the stream hit EOF and we're done. If we
// hit "would block" then all the read data so far is in our buffer, and
// otherwise we propagate errors
try_nb!(a.read_to_end(buf));
},
State::Empty => panic!("poll ReadToEnd after it's done"),
}

match mem::replace(&mut self.state, State::Empty) {
State::Reading { a, buf } => Poll::Ok((a, buf)),
State::Empty => unreachable!(),
}
}
}
2 changes: 1 addition & 1 deletion futures-mio/tests/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn chain_clients() {
read_to_end(a.chain(b).chain(c), Vec::new())
});

let data = t!(l.run(copied));
let (_, data) = t!(l.run(copied));
t.join().unwrap();

assert_eq!(data, b"foo bar baz");
Expand Down
2 changes: 1 addition & 1 deletion futures-mio/tests/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn limit() {
read_to_end(a.take(4), Vec::new())
});

let data = t!(l.run(copied));
let (_, data) = t!(l.run(copied));
t.join().unwrap();

assert_eq!(data, b"foo ");
Expand Down
2 changes: 1 addition & 1 deletion futures-tls/tests/google.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn fetch_google() {
read_to_end(socket, Vec::new())
});

let data = t!(l.run(data));
let (_, data) = t!(l.run(data));
assert!(data.starts_with(b"HTTP/1.0 200 OK"));
assert!(data.ends_with(b"</html>"));
}
Expand Down
6 changes: 3 additions & 3 deletions futures-tls/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ fn client_to_server() {
});

// Finally, run everything!
let (amt, data) = t!(l.run(sent.join(received)));
let (amt, (_, data)) = t!(l.run(sent.join(received)));
assert_eq!(amt, AMT);
assert!(data == vec![9; amt as usize]);
}
Expand Down Expand Up @@ -488,7 +488,7 @@ fn server_to_client() {
});

// Finally, run everything!
let (amt, data) = t!(l.run(sent.join(received)));
let (amt, (_, data)) = t!(l.run(sent.join(received)));
assert_eq!(amt, AMT);
assert!(data == vec![9; amt as usize]);
}
Expand Down Expand Up @@ -538,7 +538,7 @@ fn one_byte_at_a_time() {
read_to_end(socket, Vec::new())
});

let (amt, data) = t!(l.run(sent.join(received)));
let (amt, (_, data)) = t!(l.run(sent.join(received)));
assert_eq!(amt, AMT);
assert!(data == vec![9; amt as usize]);
}

0 comments on commit bdeff1b

Please sign in to comment.