@@ -3,14 +3,15 @@ use std::fmt;
33use bytes:: Bytes ;
44
55/// A raw header value.
6- #[ derive( Clone , PartialEq , Eq ) ]
6+ #[ derive( Clone , Debug ) ]
77pub struct Raw ( Lines ) ;
88
99impl Raw {
1010 /// Returns the amount of lines.
1111 #[ inline]
1212 pub fn len ( & self ) -> usize {
1313 match self . 0 {
14+ Lines :: Empty => 0 ,
1415 Lines :: One ( ..) => 1 ,
1516 Lines :: Many ( ref lines) => lines. len ( )
1617 }
@@ -39,6 +40,7 @@ impl Raw {
3940 pub fn push < V : Into < Raw > > ( & mut self , val : V ) {
4041 let raw = val. into ( ) ;
4142 match raw. 0 {
43+ Lines :: Empty => ( ) ,
4244 Lines :: One ( one) => self . push_line ( one) ,
4345 Lines :: Many ( lines) => {
4446 for line in lines {
@@ -48,9 +50,12 @@ impl Raw {
4850 }
4951 }
5052
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 ) ;
5355 match lines {
56+ Lines :: Empty => {
57+ self . 0 = Lines :: One ( line) ;
58+ }
5459 Lines :: One ( one) => {
5560 self . 0 = Lines :: Many ( vec ! [ one, line] ) ;
5661 }
@@ -62,20 +67,14 @@ impl Raw {
6267 }
6368}
6469
65- #[ derive( Debug , Clone , PartialEq , Eq ) ]
70+ #[ derive( Clone ) ]
6671enum 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 > ) ,
7675}
7776
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 {
7978 if a. len ( ) != b. len ( ) {
8079 false
8180 } else {
@@ -88,18 +87,54 @@ fn eq<A: AsRef<[u8]>, B: AsRef<[u8]>>(a: &[A], b: &[B]) -> bool {
8887 }
8988}
9089
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+
91110impl PartialEq < [ Vec < u8 > ] > for Raw {
92111 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)
97131 }
98132}
99133
100134impl PartialEq < [ u8 ] > for Raw {
101135 fn eq ( & self , bytes : & [ u8 ] ) -> bool {
102136 match self . 0 {
137+ Lines :: Empty => bytes. is_empty ( ) ,
103138 Lines :: One ( ref line) => line. as_ref ( ) == bytes,
104139 Lines :: Many ( ..) => false
105140 }
@@ -108,10 +143,7 @@ impl PartialEq<[u8]> for Raw {
108143
109144impl PartialEq < str > for Raw {
110145 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 ( )
115147 }
116148}
117149
@@ -155,31 +187,7 @@ impl<'a> From<&'a [u8]> for Raw {
155187impl From < Bytes > for Raw {
156188 #[ inline]
157189 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) )
183191 }
184192}
185193
@@ -188,12 +196,17 @@ pub fn parsed(val: Bytes) -> Raw {
188196}
189197
190198pub fn push ( raw : & mut Raw , val : Bytes ) {
191- raw. push_line ( Line :: from ( val) ) ;
199+ raw. push_line ( val) ;
192200}
193201
194- impl fmt:: Debug for Raw {
202+ pub fn new ( ) -> Raw {
203+ Raw ( Lines :: Empty )
204+ }
205+
206+ impl fmt:: Debug for Lines {
195207 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
196- match self . 0 {
208+ match * self {
209+ Lines :: Empty => f. pad ( "[]" ) ,
197210 Lines :: One ( ref line) => fmt:: Debug :: fmt ( & [ line] , f) ,
198211 Lines :: Many ( ref lines) => fmt:: Debug :: fmt ( lines, f)
199212 }
@@ -205,6 +218,7 @@ impl ::std::ops::Index<usize> for Raw {
205218
206219 fn index ( & self , idx : usize ) -> & [ u8 ] {
207220 match self . 0 {
221+ Lines :: Empty => panic ! ( "index of out of bounds: {}" , idx) ,
208222 Lines :: One ( ref line) => if idx == 0 {
209223 line. as_ref ( )
210224 } else {
@@ -217,20 +231,20 @@ impl ::std::ops::Index<usize> for Raw {
217231
218232macro_rules! literals {
219233 ( $( $len: expr => $( $value: expr) ,+; ) +) => (
220- fn maybe_literal<' a>( s: Cow <' a, [ u8 ] >) -> Line {
234+ fn maybe_literal<' a>( s: Cow <' a, [ u8 ] >) -> Bytes {
221235 match s. len( ) {
222236 $( $len => {
223237 $(
224238 if s. as_ref( ) == $value {
225- return Line :: Static ( $value) ;
239+ return Bytes :: from_static ( $value) ;
226240 }
227241 ) +
228242 } ) +
229243
230244 _ => ( )
231245 }
232246
233- Line :: from( s. into_owned( ) )
247+ Bytes :: from( s. into_owned( ) )
234248 }
235249
236250 #[ test]
@@ -263,12 +277,19 @@ impl<'a> IntoIterator for &'a Raw {
263277 }
264278}
265279
266- #[ derive( Debug ) ]
267280pub struct RawLines < ' a > {
268281 inner : & ' a Lines ,
269282 pos : usize ,
270283}
271284
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+
272293impl < ' a > Iterator for RawLines < ' a > {
273294 type Item = & ' a [ u8 ] ;
274295
@@ -277,6 +298,7 @@ impl<'a> Iterator for RawLines<'a> {
277298 let current_pos = self . pos ;
278299 self . pos += 1 ;
279300 match * self . inner {
301+ Lines :: Empty => None ,
280302 Lines :: One ( ref line) => {
281303 if current_pos == 0 {
282304 Some ( line. as_ref ( ) )
0 commit comments