diff --git a/src/header/value.rs b/src/header/value.rs index 8a1074a..a47c908 100644 --- a/src/header/value.rs +++ b/src/header/value.rs @@ -94,20 +94,7 @@ impl HeaderValue { /// assert!(val.is_err()); /// ``` pub fn try_from_str(src: &str) -> Result { - let bytes = src.as_bytes(); - - for &b in bytes { - if !is_visible_ascii(b) { - return Err(InvalidValueError { - _priv: (), - }); - } - } - - Ok(HeaderValue { - inner: Bytes::from(bytes), - is_sensitive: false, - }) + HeaderValue::try_from(src) } /// Attempt to convert a byte slice to a `HeaderValue`. @@ -134,16 +121,30 @@ impl HeaderValue { /// assert!(val.is_err()); /// ``` pub fn try_from_bytes(src: &[u8]) -> Result { - for &b in src { + HeaderValue::try_from(src) + } + + /// Attempt to convert a `Bytes` buffer to a `HeaderValue`. + /// + /// If the argument contains invalid header value bytes, an error is + /// returned. Only byte values between 32 and 255 (inclusive) are permitted. + /// + /// This function is intended to be replaced in the future by a `TryFrom` + /// implementation once the trait is stabilized in std. + pub fn try_from_shared(src: Bytes) -> Result { + HeaderValue::try_from(src) + } + + fn try_from + Into>(src: T) -> Result { + for &b in src.as_ref() { if !is_valid(b) { return Err(InvalidValueError { _priv: (), }); } } - Ok(HeaderValue { - inner: Bytes::from(src), + inner: src.into(), is_sensitive: false, }) }