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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use primitives::IntoInner;
use types::{Input, ParseResult};
use buffer::{InputBuf, StreamError, Stream};
#[derive(Debug, Eq, PartialEq, Hash)]
pub struct SliceStream<'i, I: 'i> {
pos: usize,
slice: &'i [I],
}
impl<'i, I: 'i> SliceStream<'i, I> {
#[inline]
pub fn new(slice: &'i [I]) -> Self {
SliceStream {
pos: 0,
slice: slice,
}
}
#[inline]
pub fn len(&self) -> usize {
self.slice.len() - self.pos
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<'a, 'i, I: 'i + Copy + PartialEq> Stream<'a, 'i> for SliceStream<'i, I> {
type Input = InputBuf<'i, I>;
#[inline]
fn parse<F, T, E>(&'a mut self, f: F) -> Result<T, StreamError<<Self::Input as Input>::Buffer, E>>
where F: FnOnce(Self::Input) -> ParseResult<Self::Input, T, E>,
T: 'i,
E: 'i {
use primitives::Primitives;
if self.is_empty() {
return Err(StreamError::EndOfInput);
}
match f(InputBuf::new(&self.slice[self.pos..])).into_inner() {
(remainder, Ok(data)) => {
self.pos += self.len() - remainder.len();
Ok(data)
},
(mut remainder, Err(err)) => {
if remainder.is_incomplete() {
Err(StreamError::Incomplete)
} else {
let r = remainder.len();
self.pos += self.len() - r;
Err(StreamError::ParseError(remainder.consume_remaining(), err))
}
},
}
}
}