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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::io;
use std::marker::PhantomData;
use super::{Pattern, Endian};
#[derive(Debug, Clone)]
pub struct U8;
impl Pattern for U8 {
type Value = u8;
}
#[derive(Debug, Clone)]
pub struct U16;
impl Pattern for U16 {
type Value = u16;
}
impl Endian for U16 {}
#[derive(Debug, Clone)]
pub struct U24;
impl Pattern for U24 {
type Value = u32;
}
impl Endian for U24 {}
#[derive(Debug, Clone)]
pub struct U32;
impl Pattern for U32 {
type Value = u32;
}
impl Endian for U32 {}
#[derive(Debug, Clone)]
pub struct U40;
impl Pattern for U40 {
type Value = u64;
}
impl Endian for U40 {}
#[derive(Debug, Clone)]
pub struct U48;
impl Pattern for U48 {
type Value = u64;
}
impl Endian for U48 {}
#[derive(Debug, Clone)]
pub struct U56;
impl Pattern for U56 {
type Value = u64;
}
impl Endian for U56 {}
#[derive(Debug, Clone)]
pub struct U64;
impl Pattern for U64 {
type Value = u64;
}
impl Endian for U64 {}
#[derive(Debug, Clone)]
pub struct I8;
impl Pattern for I8 {
type Value = i8;
}
#[derive(Debug, Clone)]
pub struct I16;
impl Pattern for I16 {
type Value = i16;
}
impl Endian for I16 {}
#[derive(Debug, Clone)]
pub struct I24;
impl Pattern for I24 {
type Value = i32;
}
impl Endian for I24 {}
#[derive(Debug, Clone)]
pub struct I32;
impl Pattern for I32 {
type Value = i32;
}
impl Endian for I32 {}
#[derive(Debug, Clone)]
pub struct I40;
impl Pattern for I40 {
type Value = i64;
}
impl Endian for I40 {}
#[derive(Debug, Clone)]
pub struct I48;
impl Pattern for I48 {
type Value = i64;
}
impl Endian for I48 {}
#[derive(Debug, Clone)]
pub struct I56;
impl Pattern for I56 {
type Value = i64;
}
impl Endian for I56 {}
#[derive(Debug, Clone)]
pub struct I64;
impl Pattern for I64 {
type Value = i64;
}
impl Endian for I64 {}
#[derive(Debug, Clone)]
pub struct F32;
impl Pattern for F32 {
type Value = f32;
}
impl Endian for F32 {}
#[derive(Debug, Clone)]
pub struct F64;
impl Pattern for F64 {
type Value = f64;
}
impl Endian for F64 {}
#[derive(Debug, Clone)]
pub struct Eos;
impl Pattern for Eos {
type Value = Result<(), u8>;
}
#[derive(Debug)]
pub struct Until<F, T> {
pred: F,
min_buffer_size: usize,
max_buffer_size: usize,
_phantom: PhantomData<T>,
}
impl<F, T> Until<F, T> {
#[allow(missing_docs)]
pub fn unwrap(self) -> (F, usize, usize) {
(self.pred, self.min_buffer_size, self.max_buffer_size)
}
pub fn min_buffer_size(mut self, size: usize) -> Self {
assert!(size > 0);
self.min_buffer_size = size;
self
}
pub fn max_buffer_size(mut self, size: usize) -> Self {
assert!(size >= self.min_buffer_size);
self.max_buffer_size = size;
self
}
}
impl<F, T> Pattern for Until<F, T> {
type Value = (Vec<u8>, T);
}
pub fn until<F, T>(f: F) -> Until<F, T>
where F: Fn(&[u8], bool) -> io::Result<Option<T>>
{
Until {
pred: f,
min_buffer_size: 1024,
max_buffer_size: 10 * 1024 * 1024,
_phantom: PhantomData,
}
}