@@ -3,14 +3,15 @@ use std::fmt;
3
3
use bytes:: Bytes ;
4
4
5
5
/// A raw header value.
6
- #[ derive( Clone , PartialEq , Eq ) ]
6
+ #[ derive( Clone , Debug ) ]
7
7
pub struct Raw ( Lines ) ;
8
8
9
9
impl Raw {
10
10
/// Returns the amount of lines.
11
11
#[ inline]
12
12
pub fn len ( & self ) -> usize {
13
13
match self . 0 {
14
+ Lines :: Empty => 0 ,
14
15
Lines :: One ( ..) => 1 ,
15
16
Lines :: Many ( ref lines) => lines. len ( )
16
17
}
@@ -39,6 +40,7 @@ impl Raw {
39
40
pub fn push < V : Into < Raw > > ( & mut self , val : V ) {
40
41
let raw = val. into ( ) ;
41
42
match raw. 0 {
43
+ Lines :: Empty => ( ) ,
42
44
Lines :: One ( one) => self . push_line ( one) ,
43
45
Lines :: Many ( lines) => {
44
46
for line in lines {
@@ -48,9 +50,12 @@ impl Raw {
48
50
}
49
51
}
50
52
51
- fn push_line ( & mut self , line : Line ) {
52
- let lines = :: std:: mem:: replace ( & mut self . 0 , Lines :: Many ( Vec :: new ( ) ) ) ;
53
+ fn push_line ( & mut self , line : Bytes ) {
54
+ let lines = :: std:: mem:: replace ( & mut self . 0 , Lines :: Empty ) ;
53
55
match lines {
56
+ Lines :: Empty => {
57
+ self . 0 = Lines :: One ( line) ;
58
+ }
54
59
Lines :: One ( one) => {
55
60
self . 0 = Lines :: Many ( vec ! [ one, line] ) ;
56
61
}
@@ -62,20 +67,14 @@ impl Raw {
62
67
}
63
68
}
64
69
65
- #[ derive( Debug , Clone , PartialEq , Eq ) ]
70
+ #[ derive( Clone ) ]
66
71
enum Lines {
67
- One ( Line ) ,
68
- Many ( Vec < Line > ) ,
69
- }
70
-
71
- #[ derive( Debug , Clone , PartialEq , Eq ) ]
72
- enum Line {
73
- Static ( & ' static [ u8 ] ) ,
74
- Owned ( Vec < u8 > ) ,
75
- Shared ( Bytes ) ,
72
+ Empty ,
73
+ One ( Bytes ) ,
74
+ Many ( Vec < Bytes > ) ,
76
75
}
77
76
78
- fn eq < A : AsRef < [ u8 ] > , B : AsRef < [ u8 ] > > ( a : & [ A ] , b : & [ B ] ) -> bool {
77
+ fn eq_many < A : AsRef < [ u8 ] > , B : AsRef < [ u8 ] > > ( a : & [ A ] , b : & [ B ] ) -> bool {
79
78
if a. len ( ) != b. len ( ) {
80
79
false
81
80
} else {
@@ -88,18 +87,54 @@ fn eq<A: AsRef<[u8]>, B: AsRef<[u8]>>(a: &[A], b: &[B]) -> bool {
88
87
}
89
88
}
90
89
90
+ fn eq < B : AsRef < [ u8 ] > > ( raw : & Raw , b : & [ B ] ) -> bool {
91
+ match raw. 0 {
92
+ Lines :: Empty => b. is_empty ( ) ,
93
+ Lines :: One ( ref line) => eq_many ( & [ line] , b) ,
94
+ Lines :: Many ( ref lines) => eq_many ( lines, b)
95
+ }
96
+ }
97
+
98
+ impl PartialEq for Raw {
99
+ fn eq ( & self , other : & Raw ) -> bool {
100
+ match other. 0 {
101
+ Lines :: Empty => eq ( self , & [ ] as & [ Bytes ] ) ,
102
+ Lines :: One ( ref line) => eq ( self , & [ line] ) ,
103
+ Lines :: Many ( ref lines) => eq ( self , lines) ,
104
+ }
105
+ }
106
+ }
107
+
108
+ impl Eq for Raw { }
109
+
91
110
impl PartialEq < [ Vec < u8 > ] > for Raw {
92
111
fn eq ( & self , bytes : & [ Vec < u8 > ] ) -> bool {
93
- match self . 0 {
94
- Lines :: One ( ref line) => eq ( & [ line] , bytes) ,
95
- Lines :: Many ( ref lines) => eq ( lines, bytes)
96
- }
112
+ eq ( self , bytes)
113
+ }
114
+ }
115
+
116
+ impl < ' a > PartialEq < [ & ' a [ u8 ] ] > for Raw {
117
+ fn eq ( & self , bytes : & [ & [ u8 ] ] ) -> bool {
118
+ eq ( self , bytes)
119
+ }
120
+ }
121
+
122
+ impl PartialEq < [ String ] > for Raw {
123
+ fn eq ( & self , bytes : & [ String ] ) -> bool {
124
+ eq ( self , bytes)
125
+ }
126
+ }
127
+
128
+ impl < ' a > PartialEq < [ & ' a str ] > for Raw {
129
+ fn eq ( & self , bytes : & [ & ' a str ] ) -> bool {
130
+ eq ( self , bytes)
97
131
}
98
132
}
99
133
100
134
impl PartialEq < [ u8 ] > for Raw {
101
135
fn eq ( & self , bytes : & [ u8 ] ) -> bool {
102
136
match self . 0 {
137
+ Lines :: Empty => bytes. is_empty ( ) ,
103
138
Lines :: One ( ref line) => line. as_ref ( ) == bytes,
104
139
Lines :: Many ( ..) => false
105
140
}
@@ -108,10 +143,7 @@ impl PartialEq<[u8]> for Raw {
108
143
109
144
impl PartialEq < str > for Raw {
110
145
fn eq ( & self , s : & str ) -> bool {
111
- match self . 0 {
112
- Lines :: One ( ref line) => line. as_ref ( ) == s. as_bytes ( ) ,
113
- Lines :: Many ( ..) => false
114
- }
146
+ self == s. as_bytes ( )
115
147
}
116
148
}
117
149
@@ -155,31 +187,7 @@ impl<'a> From<&'a [u8]> for Raw {
155
187
impl From < Bytes > for Raw {
156
188
#[ inline]
157
189
fn from ( val : Bytes ) -> Raw {
158
- Raw ( Lines :: One ( Line :: Shared ( val) ) )
159
- }
160
- }
161
-
162
- impl From < Vec < u8 > > for Line {
163
- #[ inline]
164
- fn from ( val : Vec < u8 > ) -> Line {
165
- Line :: Owned ( val)
166
- }
167
- }
168
-
169
- impl From < Bytes > for Line {
170
- #[ inline]
171
- fn from ( val : Bytes ) -> Line {
172
- Line :: Shared ( val)
173
- }
174
- }
175
-
176
- impl AsRef < [ u8 ] > for Line {
177
- fn as_ref ( & self ) -> & [ u8 ] {
178
- match * self {
179
- Line :: Static ( ref s) => s,
180
- Line :: Owned ( ref v) => v. as_ref ( ) ,
181
- Line :: Shared ( ref m) => m. as_ref ( ) ,
182
- }
190
+ Raw ( Lines :: One ( val) )
183
191
}
184
192
}
185
193
@@ -188,12 +196,17 @@ pub fn parsed(val: Bytes) -> Raw {
188
196
}
189
197
190
198
pub fn push ( raw : & mut Raw , val : Bytes ) {
191
- raw. push_line ( Line :: from ( val) ) ;
199
+ raw. push_line ( val) ;
192
200
}
193
201
194
- impl fmt:: Debug for Raw {
202
+ pub fn new ( ) -> Raw {
203
+ Raw ( Lines :: Empty )
204
+ }
205
+
206
+ impl fmt:: Debug for Lines {
195
207
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
196
- match self . 0 {
208
+ match * self {
209
+ Lines :: Empty => f. pad ( "[]" ) ,
197
210
Lines :: One ( ref line) => fmt:: Debug :: fmt ( & [ line] , f) ,
198
211
Lines :: Many ( ref lines) => fmt:: Debug :: fmt ( lines, f)
199
212
}
@@ -205,6 +218,7 @@ impl ::std::ops::Index<usize> for Raw {
205
218
206
219
fn index ( & self , idx : usize ) -> & [ u8 ] {
207
220
match self . 0 {
221
+ Lines :: Empty => panic ! ( "index of out of bounds: {}" , idx) ,
208
222
Lines :: One ( ref line) => if idx == 0 {
209
223
line. as_ref ( )
210
224
} else {
@@ -217,20 +231,20 @@ impl ::std::ops::Index<usize> for Raw {
217
231
218
232
macro_rules! literals {
219
233
( $( $len: expr => $( $value: expr) ,+; ) +) => (
220
- fn maybe_literal<' a>( s: Cow <' a, [ u8 ] >) -> Line {
234
+ fn maybe_literal<' a>( s: Cow <' a, [ u8 ] >) -> Bytes {
221
235
match s. len( ) {
222
236
$( $len => {
223
237
$(
224
238
if s. as_ref( ) == $value {
225
- return Line :: Static ( $value) ;
239
+ return Bytes :: from_static ( $value) ;
226
240
}
227
241
) +
228
242
} ) +
229
243
230
244
_ => ( )
231
245
}
232
246
233
- Line :: from( s. into_owned( ) )
247
+ Bytes :: from( s. into_owned( ) )
234
248
}
235
249
236
250
#[ test]
@@ -263,12 +277,19 @@ impl<'a> IntoIterator for &'a Raw {
263
277
}
264
278
}
265
279
266
- #[ derive( Debug ) ]
267
280
pub struct RawLines < ' a > {
268
281
inner : & ' a Lines ,
269
282
pos : usize ,
270
283
}
271
284
285
+ impl < ' a > fmt:: Debug for RawLines < ' a > {
286
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
287
+ f. debug_tuple ( "RawLines" )
288
+ . field ( & self . inner )
289
+ . finish ( )
290
+ }
291
+ }
292
+
272
293
impl < ' a > Iterator for RawLines < ' a > {
273
294
type Item = & ' a [ u8 ] ;
274
295
@@ -277,6 +298,7 @@ impl<'a> Iterator for RawLines<'a> {
277
298
let current_pos = self . pos ;
278
299
self . pos += 1 ;
279
300
match * self . inner {
301
+ Lines :: Empty => None ,
280
302
Lines :: One ( ref line) => {
281
303
if current_pos == 0 {
282
304
Some ( line. as_ref ( ) )
0 commit comments