1 use std::io::{self, BufRead};
2 use std::mem;
3
4 use futures::{Poll, Stream};
5
6 use AsyncRead;
7
8 /// Combinator created by the top-level `lines` method which is a stream over
9 /// the lines of text on an I/O object.
10 #[derive(Debug)]
11 pub struct Lines<A> {
12 io: A,
13 line: String,
14 }
15
16 /// Creates a new stream from the I/O object given representing the lines of
17 /// input that are found on `A`.
18 ///
19 /// This method takes an asynchronous I/O object, `a`, and returns a `Stream` of
20 /// lines that the object contains. The returned stream will reach its end once
21 /// `a` reaches EOF.
lines<A>(a: A) -> Lines<A> where A: AsyncRead + BufRead,22 pub fn lines<A>(a: A) -> Lines<A>
23 where A: AsyncRead + BufRead,
24 {
25 Lines {
26 io: a,
27 line: String::new(),
28 }
29 }
30
31 impl<A> Lines<A> {
32 /// Returns the underlying I/O object.
33 ///
34 /// Note that this may lose data already read into internal buffers. It's
35 /// recommended to only call this once the stream has reached its end.
into_inner(self) -> A36 pub fn into_inner(self) -> A {
37 self.io
38 }
39 }
40
41 impl<A> Stream for Lines<A>
42 where A: AsyncRead + BufRead,
43 {
44 type Item = String;
45 type Error = io::Error;
46
poll(&mut self) -> Poll<Option<String>, io::Error>47 fn poll(&mut self) -> Poll<Option<String>, io::Error> {
48 let n = try_nb!(self.io.read_line(&mut self.line));
49 if n == 0 && self.line.len() == 0 {
50 return Ok(None.into())
51 }
52 if self.line.ends_with("\n") {
53 self.line.pop();
54 if self.line.ends_with("\r") {
55 self.line.pop();
56 }
57 }
58 Ok(Some(mem::replace(&mut self.line, String::new())).into())
59 }
60 }
61