1use std::io::{self, BufRead};
2use std::mem;
3
4use futures::{Poll, Stream};
5
6use AsyncRead;
7
8#[derive(Debug)]
11pub struct Lines<A> {
12 io: A,
13 line: String,
14}
15
16pub fn lines<A>(a: A) -> Lines<A>
23where
24 A: AsyncRead + BufRead,
25{
26 Lines {
27 io: a,
28 line: String::new(),
29 }
30}
31
32impl<A> Lines<A> {
33 pub fn into_inner(self) -> A {
38 self.io
39 }
40}
41
42impl<A> Stream for Lines<A>
43where
44 A: AsyncRead + BufRead,
45{
46 type Item = String;
47 type Error = io::Error;
48
49 fn poll(&mut self) -> Poll<Option<String>, io::Error> {
50 let n = try_nb!(self.io.read_line(&mut self.line));
51 if n == 0 && self.line.len() == 0 {
52 return Ok(None.into());
53 }
54 if self.line.ends_with("\n") {
55 self.line.pop();
56 if self.line.ends_with("\r") {
57 self.line.pop();
58 }
59 }
60 Ok(Some(mem::replace(&mut self.line, String::new())).into())
61 }
62}