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