Skip to content

Commit

Permalink
http: Replace header::map::GetAll with std::slice::Iter (#2527)
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesfragoso authored Dec 18, 2021
1 parent 0bd5ccc commit 84ea9e7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 50 deletions.
4 changes: 4 additions & 0 deletions actix-http/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changes

## Unreleased - 2021-xx-xx
### Removed
* `header::map::GetAll` iterator, its `Iterator::size_hint` method was wrongly implemented. Replaced with `std::slice::Iter`. [#2527]

[#2527]: https://github.com/actix/actix-web/pull/2527


## 3.0.0-beta.16 - 2021-12-17
Expand Down
55 changes: 6 additions & 49 deletions actix-http/src/header/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,11 @@ impl HeaderMap {
/// assert_eq!(set_cookies_iter.next().unwrap(), "two=2");
/// assert!(set_cookies_iter.next().is_none());
/// ```
pub fn get_all(&self, key: impl AsHeaderName) -> GetAll<'_> {
GetAll::new(self.get_value(key))
pub fn get_all(&self, key: impl AsHeaderName) -> std::slice::Iter<'_, HeaderValue> {
match self.get_value(key) {
Some(value) => value.iter(),
None => (&[]).iter(),
}
}

// TODO: get_all_mut ?
Expand Down Expand Up @@ -602,52 +605,6 @@ impl<'a> IntoIterator for &'a HeaderMap {
}
}

/// Iterator over borrowed values with the same associated name.
///
/// See [`HeaderMap::get_all`].
#[derive(Debug)]
pub struct GetAll<'a> {
idx: usize,
value: Option<&'a Value>,
}

impl<'a> GetAll<'a> {
fn new(value: Option<&'a Value>) -> Self {
Self { idx: 0, value }
}
}

impl<'a> Iterator for GetAll<'a> {
type Item = &'a HeaderValue;

fn next(&mut self) -> Option<Self::Item> {
let val = self.value?;

match val.get(self.idx) {
Some(val) => {
self.idx += 1;
Some(val)
}
None => {
// current index is none; remove value to fast-path future next calls
self.value = None;
None
}
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
match self.value {
Some(val) => (val.len(), Some(val.len())),
None => (0, Some(0)),
}
}
}

impl ExactSizeIterator for GetAll<'_> {}

impl iter::FusedIterator for GetAll<'_> {}

/// Iterator over removed, owned values with the same associated name.
///
/// Returned from methods that remove or replace items. See [`HeaderMap::insert`]
Expand Down Expand Up @@ -895,7 +852,7 @@ mod tests {

assert_impl_all!(HeaderMap: IntoIterator);
assert_impl_all!(Keys<'_>: Iterator, ExactSizeIterator, FusedIterator);
assert_impl_all!(GetAll<'_>: Iterator, ExactSizeIterator, FusedIterator);
assert_impl_all!(std::slice::Iter<'_, HeaderValue>: Iterator, ExactSizeIterator, FusedIterator);
assert_impl_all!(Removed: Iterator, ExactSizeIterator, FusedIterator);
assert_impl_all!(Iter<'_>: Iterator, ExactSizeIterator, FusedIterator);
assert_impl_all!(IntoIter: Iterator, ExactSizeIterator, FusedIterator);
Expand Down
2 changes: 1 addition & 1 deletion src/response/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl Future for HttpResponse<BoxBody> {

#[cfg(feature = "cookies")]
pub struct CookieIter<'a> {
iter: header::map::GetAll<'a>,
iter: std::slice::Iter<'a, HeaderValue>,
}

#[cfg(feature = "cookies")]
Expand Down

0 comments on commit 84ea9e7

Please sign in to comment.