@@ -137,6 +137,8 @@ pub struct Drain<'a, T> {
137
137
idx : usize ,
138
138
len : usize ,
139
139
entries : * mut [ Bucket < T > ] ,
140
+ // If None, pull from `entries`
141
+ next : Option < usize > ,
140
142
extra_values : * mut Vec < ExtraValue < T > > ,
141
143
lt : PhantomData < & ' a mut HeaderMap < T > > ,
142
144
}
@@ -922,6 +924,10 @@ impl<T> HeaderMap<T> {
922
924
///
923
925
/// The internal memory is kept for reuse.
924
926
///
927
+ /// For each yielded item that has `None` provided for the `HeaderName`,
928
+ /// then the associated header name is the same as that of the previously
929
+ /// yielded item. The first yielded item will have `HeaderName` set.
930
+ ///
925
931
/// # Examples
926
932
///
927
933
/// ```
@@ -935,18 +941,13 @@ impl<T> HeaderMap<T> {
935
941
///
936
942
/// let mut drain = map.drain();
937
943
///
938
- /// let (key, mut vals) = drain.next().unwrap();
939
944
///
940
- /// assert_eq!("host", key);
941
- /// assert_eq!("hello", vals.next().unwrap());
942
- /// assert_eq!("goodbye", vals.next().unwrap());
943
- /// assert!(vals.next().is_none());
945
+ /// assert_eq!(drain.next(), Some((Some(HOST), "hello".parse().unwrap())));
946
+ /// assert_eq!(drain.next(), Some((None, "goodbye".parse().unwrap())));
944
947
///
945
- /// let (key, mut vals) = drain.next ().unwrap();
948
+ /// assert_eq!(drain.next(), Some((Some(CONTENT_LENGTH), "123".parse ().unwrap())) );
946
949
///
947
- /// assert_eq!("content-length", key);
948
- /// assert_eq!("123", vals.next().unwrap());
949
- /// assert!(vals.next().is_none());
950
+ /// assert_eq!(drain.next(), None);
950
951
/// ```
951
952
pub fn drain ( & mut self ) -> Drain < ' _ , T > {
952
953
for i in self . indices . iter_mut ( ) {
@@ -970,6 +971,7 @@ impl<T> HeaderMap<T> {
970
971
len,
971
972
entries,
972
973
extra_values,
974
+ next : None ,
973
975
lt : PhantomData ,
974
976
}
975
977
}
@@ -2165,9 +2167,25 @@ impl<'a, T> FusedIterator for ValuesMut<'a, T> {}
2165
2167
// ===== impl Drain =====
2166
2168
2167
2169
impl < ' a , T > Iterator for Drain < ' a , T > {
2168
- type Item = ( HeaderName , ValueDrain < ' a , T > ) ;
2170
+ type Item = ( Option < HeaderName > , T ) ;
2169
2171
2170
2172
fn next ( & mut self ) -> Option < Self :: Item > {
2173
+ if let Some ( next) = self . next {
2174
+ // Remove the extra value
2175
+
2176
+ let raw_links = RawLinks ( self . entries ) ;
2177
+ let extra = unsafe {
2178
+ remove_extra_value ( raw_links, & mut * self . extra_values , next)
2179
+ } ;
2180
+
2181
+ match extra. next {
2182
+ Link :: Extra ( idx) => self . next = Some ( idx) ,
2183
+ Link :: Entry ( _) => self . next = None ,
2184
+ }
2185
+
2186
+ return Some ( ( None , extra. value ) ) ;
2187
+ }
2188
+
2171
2189
let idx = self . idx ;
2172
2190
2173
2191
if idx == self . len {
@@ -2176,37 +2194,27 @@ impl<'a, T> Iterator for Drain<'a, T> {
2176
2194
2177
2195
self . idx += 1 ;
2178
2196
2179
- let key;
2180
- let value;
2181
- let next;
2182
-
2183
- let values = unsafe {
2197
+ unsafe {
2184
2198
let entry = & ( * self . entries ) [ idx] ;
2185
2199
2186
2200
// Read the header name
2187
- key = ptr:: read ( & entry. key as * const _ ) ;
2188
- value = ptr:: read ( & entry. value as * const _ ) ;
2189
- next = entry. links . map ( |l| l. next ) ;
2190
-
2201
+ let key = ptr:: read ( & entry. key as * const _ ) ;
2202
+ let value = ptr:: read ( & entry. value as * const _ ) ;
2203
+ self . next = entry. links . map ( |l| l. next ) ;
2191
2204
2192
- let raw_links = RawLinks ( self . entries ) ;
2193
- let extra_values = self . extra_values ;
2194
-
2195
- ValueDrain {
2196
- raw_links,
2197
- extra_values,
2198
- first : Some ( value) ,
2199
- next : next,
2200
- lt : PhantomData ,
2201
- }
2202
- } ;
2203
-
2204
- Some ( ( key, values) )
2205
+ Some ( ( Some ( key) , value) )
2206
+ }
2205
2207
}
2206
2208
2207
2209
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2210
+ // At least this many names... It's unknown if the user wants
2211
+ // to count the extra_values on top.
2212
+ //
2213
+ // For instance, extending a new `HeaderMap` wouldn't need to
2214
+ // reserve the upper-bound in `entries`, only the lower-bound.
2208
2215
let lower = self . len - self . idx ;
2209
- ( lower, Some ( lower) )
2216
+ let upper = unsafe { ( * self . extra_values ) . len ( ) } + lower;
2217
+ ( lower, Some ( upper) )
2210
2218
}
2211
2219
}
2212
2220
0 commit comments