fix(header): add Cookie::iter()

This commit is contained in:
mayah
2017-05-30 03:16:09 +09:00
committed by Sean McArthur
parent bdd2e1a3ad
commit edc1c0dd01
2 changed files with 36 additions and 1 deletions

View File

@@ -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 {
@@ -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;

View File

@@ -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;