diff --git a/core/http/src/header/content_type.rs b/core/http/src/header/content_type.rs index 74f728ec..262941ef 100644 --- a/core/http/src/header/content_type.rs +++ b/core/http/src/header/content_type.rs @@ -235,39 +235,37 @@ impl ContentType { known_extensions!(from_extension); - /// Creates a new `ContentType` with top-level type `top`, subtype `sub`, - /// and parameters `ps`. This should _only_ be used to construct uncommon or - /// custom content types. Use an associated constant for everything else. + /// Sets the parameters `parameters` on `self`. /// /// # Example /// - /// Create a custom `application/x-id; id=1` content type: + /// Create a custom `application/x-id; id=1` media type: /// /// ```rust /// # extern crate rocket; /// use rocket::http::ContentType; /// - /// let id = ContentType::with_params("application", "x-id", ("id", "1")); + /// let id = ContentType::new("application", "x-id").with_params(("id", "1")); /// assert_eq!(id.to_string(), "application/x-id; id=1".to_string()); /// ``` /// - /// Create a custom `text/person; name=bob; weight=175` content type: + /// Create a custom `text/person; name=bob; weight=175` media type: /// /// ```rust /// # extern crate rocket; /// use rocket::http::ContentType; /// - /// let params = vec![("name", "bob"), ("ref", "2382")]; - /// let mt = ContentType::with_params("text", "person", params); + /// let mt = ContentType::new("text", "person") + /// .with_params([("name", "bob"), ("ref", "2382")]); + /// /// assert_eq!(mt.to_string(), "text/person; name=bob; ref=2382".to_string()); /// ``` - #[inline] - pub fn with_params(top: T, sub: S, ps: P) -> ContentType - where T: Into>, S: Into>, - K: Into>, V: Into>, + pub fn with_params(self, parameters: P) -> ContentType + where K: Into>, + V: Into>, P: IntoCollection<(K, V)> { - ContentType(MediaType::with_params(top, sub, ps)) + ContentType(self.0.with_params(parameters)) } /// Borrows the inner `MediaType` of `self`. diff --git a/core/http/src/header/media_type.rs b/core/http/src/header/media_type.rs index 7e8f219a..2db0c284 100644 --- a/core/http/src/header/media_type.rs +++ b/core/http/src/header/media_type.rs @@ -305,9 +305,7 @@ impl MediaType { } } - /// Creates a new `MediaType` with top-level type `top`, subtype `sub`, and - /// parameters `ps`. This should _only_ be used to construct uncommon or - /// custom media types. Use an associated constant for everything else. + /// Sets the parameters `parameters` on `self`. /// /// # Example /// @@ -317,7 +315,7 @@ impl MediaType { /// # extern crate rocket; /// use rocket::http::MediaType; /// - /// let id = MediaType::with_params("application", "x-id", ("id", "1")); + /// let id = MediaType::new("application", "x-id").with_params(("id", "1")); /// assert_eq!(id.to_string(), "application/x-id; id=1".to_string()); /// ``` /// @@ -327,27 +325,21 @@ impl MediaType { /// # extern crate rocket; /// use rocket::http::MediaType; /// - /// let params = vec![("name", "bob"), ("ref", "2382")]; - /// let mt = MediaType::with_params("text", "person", params); + /// let mt = MediaType::new("text", "person") + /// .with_params([("name", "bob"), ("ref", "2382")]); + /// /// assert_eq!(mt.to_string(), "text/person; name=bob; ref=2382".to_string()); /// ``` - #[inline] - pub fn with_params(top: T, sub: S, ps: P) -> MediaType - where T: Into>, S: Into>, - K: Into>, V: Into>, + pub fn with_params(mut self, ps: P) -> MediaType + where K: Into>, + V: Into>, P: IntoCollection<(K, V)> { - let params = ps.mapped(|(key, val)| ( - Indexed::Concrete(key.into()), - Indexed::Concrete(val.into()) - )); + use Indexed::Concrete; - MediaType { - source: Source::None, - top: Indexed::Concrete(top.into()), - sub: Indexed::Concrete(sub.into()), - params: MediaParams::Dynamic(params) - } + let params = ps.mapped(|(k, v)| (Concrete(k.into()), Concrete(v.into()))); + self.params = MediaParams::Dynamic(params); + self } /// A `const` variant of [`MediaType::with_params()`]. Creates a new @@ -490,7 +482,7 @@ impl MediaType { /// use rocket::http::MediaType; /// /// let plain = MediaType::Plain; - /// let plain2 = MediaType::with_params("text", "plain", ("charset", "utf-8")); + /// let plain2 = MediaType::new("text", "plain").with_params(("charset", "utf-8")); /// let just_plain = MediaType::new("text", "plain"); /// /// // The `PartialEq` implementation doesn't consider parameters. @@ -504,18 +496,7 @@ impl MediaType { /// assert!(plain.exact_eq(&plain2)); /// ``` pub fn exact_eq(&self, other: &MediaType) -> bool { - self == other && { - let (mut a_params, mut b_params) = (self.params(), other.params()); - loop { - match (a_params.next(), b_params.next()) { - (Some(a), Some(b)) if a != b => return false, - (Some(_), Some(_)) => continue, - (Some(_), None) => return false, - (None, Some(_)) => return false, - (None, None) => return true - } - } - } + self == other && self.params().eq(other.params()) } /// Returns an iterator over the (key, value) pairs of the media type's