Skip to content

Commit

Permalink
fix(header): add Cookie::iter()
Browse files Browse the repository at this point in the history
  • Loading branch information
mayah authored and seanmonstar committed May 29, 2017
1 parent bdd2e1a commit edc1c0d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/header/common/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ impl Cookie {
pub fn get(&self, key: &str) -> Option<&str> {
self.0.get(key).map(AsRef::as_ref)
}

/// Iterate cookies.
///
/// Iterate cookie (key, value) in insertion order.
///
/// ```
/// use hyper::header::Cookie;
/// let mut cookie = Cookie::new();
/// cookie.append("foo", "bar");
/// cookie.append(String::from("dyn"), String::from("amic"));
///
/// let mut keys = Vec::new();
/// let mut values = Vec::new();
/// for (k, v) in cookie.iter() {
/// keys.push(k);
/// values.push(v);
/// }
/// assert_eq!(keys, vec!["foo", "dyn"]);
/// assert_eq!(values, vec!["bar", "amic"]);
/// ```
pub fn iter(&self) -> CookieIter {
CookieIter(self.0.iter())
}
}

impl Header for Cookie {
Expand Down Expand Up @@ -154,6 +177,18 @@ impl fmt::Display for Cookie {
}
}

/// Iterator for cookie.
#[derive(Debug)]
pub struct CookieIter<'a>(::std::slice::Iter<'a, (Cow<'static, str>, Cow<'static, str>)>);

impl<'a> Iterator for CookieIter<'a> {
type Item = (&'a str, &'a str);

fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|kv| (kv.0.as_ref(), kv.1.as_ref()))
}
}

#[cfg(test)]
mod tests {
use header::Header;
Expand Down
2 changes: 1 addition & 1 deletion src/header/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use self::content_language::ContentLanguage;
pub use self::content_location::ContentLocation;
pub use self::content_range::{ContentRange, ContentRangeSpec};
pub use self::content_type::ContentType;
pub use self::cookie::Cookie;
pub use self::cookie::{Cookie, CookieIter};
pub use self::date::Date;
pub use self::etag::ETag;
pub use self::expect::Expect;
Expand Down

0 comments on commit edc1c0d

Please sign in to comment.