Skip to content

Commit

Permalink
Revert "Make ValueDrain eagerly collect its extra values"
Browse files Browse the repository at this point in the history
This reverts commit 8ffe094.

This patch was for 0.1.x, but is not necessary in 0.2.x due to an
upcoming API change for `Drain`.
  • Loading branch information
seanmonstar committed Nov 27, 2019
1 parent 43dffa1 commit 6c2b789
Showing 1 changed file with 34 additions and 49 deletions.
83 changes: 34 additions & 49 deletions src/header/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,10 @@ pub struct ValueIterMut<'a, T> {
/// An drain iterator of all values associated with a single header name.
#[derive(Debug)]
pub struct ValueDrain<'a, T> {
raw_links: RawLinks<T>,
extra_values: *mut Vec<ExtraValue<T>>,
first: Option<T>,
next: Option<::std::vec::IntoIter<T>>,
next: Option<usize>,
lt: PhantomData<&'a mut HeaderMap<T>>,
}

Expand Down Expand Up @@ -1190,16 +1192,13 @@ impl<T> HeaderMap<T> {
}

let raw_links = self.raw_links();
let extra_values = &mut self.extra_values;

let next = links.map(|l| {
drain_all_extra_values(raw_links, extra_values, l.next)
.into_iter()
});
let extra_values = &mut self.extra_values as *mut _;

ValueDrain {
raw_links,
extra_values,
first: Some(old),
next: next,
next: links.map(|l| l.next),
lt: PhantomData,
}
}
Expand Down Expand Up @@ -1702,22 +1701,6 @@ fn remove_extra_value<T>(mut raw_links: RawLinks<T>, extra_values: &mut Vec<Extr
extra
}


fn drain_all_extra_values<T>(raw_links: RawLinks<T>, extra_values: &mut Vec<ExtraValue<T>>, mut head: usize) -> Vec<T> {
let mut vec = Vec::new();
loop {
let extra = remove_extra_value(raw_links, extra_values, head);
vec.push(extra.value);

if let Link::Extra(idx) = extra.next {
head = idx;
} else {
break;
}
}
vec
}

impl<'a, T> IntoIterator for &'a HeaderMap<T> {
type Item = (&'a HeaderName, &'a T);
type IntoIter = Iter<'a, T>;
Expand Down Expand Up @@ -2203,17 +2186,17 @@ impl<'a, T> Iterator for Drain<'a, T> {
// Read the header name
key = ptr::read(&entry.key as *const _);
value = ptr::read(&entry.value as *const _);
next = entry.links.map(|l| l.next);


let raw_links = RawLinks(self.entries);
let extra_values = &mut *self.extra_values;
next = entry.links.map(|l| {
drain_all_extra_values(raw_links, extra_values, l.next)
.into_iter()
});
let extra_values = self.extra_values;

ValueDrain {
raw_links,
extra_values,
first: Some(value),
next,
next: next,
lt: PhantomData,
}
};
Expand Down Expand Up @@ -2947,15 +2930,12 @@ impl<'a, T> OccupiedEntry<'a, T> {
pub fn remove_entry_mult(self) -> (HeaderName, ValueDrain<'a, T>) {
let entry = self.map.remove_found(self.probe, self.index);
let raw_links = self.map.raw_links();
let extra_values = &mut self.map.extra_values;

let next = entry.links.map(|l| {
drain_all_extra_values(raw_links, extra_values, l.next)
.into_iter()
});
let extra_values = &mut self.map.extra_values as *mut _;
let drain = ValueDrain {
raw_links,
extra_values,
first: Some(entry.value),
next,
next: entry.links.map(|l| l.next),
lt: PhantomData,
};
(entry.key, drain)
Expand Down Expand Up @@ -3048,26 +3028,31 @@ impl<'a, T> Iterator for ValueDrain<'a, T> {
fn next(&mut self) -> Option<T> {
if self.first.is_some() {
self.first.take()
} else if let Some(ref mut extras) = self.next {
extras.next()
} else if let Some(next) = self.next {
// Remove the extra value
let extra = unsafe {
remove_extra_value(self.raw_links, &mut *self.extra_values, next)
};

match extra.next {
Link::Extra(idx) => self.next = Some(idx),
Link::Entry(_) => self.next = None,
}

Some(extra.value)
} else {
None
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
match (&self.first, &self.next) {
match (&self.first, self.next) {
// Exactly 1
(&Some(_), &None) => (1, Some(1)),
// 1 + extras
(&Some(_), &Some(ref extras)) => {
let (l, u) = extras.size_hint();
(l + 1, u.map(|u| u + 1))
},
// Extras only
(&None, &Some(ref extras)) => extras.size_hint(),
(&Some(_), None) => (1, Some(1)),
// At least 1
(&_, Some(_)) => (1, None),
// No more
(&None, &None) => (0, Some(0)),
(&None, None) => (0, Some(0)),
}
}
}
Expand Down

0 comments on commit 6c2b789

Please sign in to comment.