Merge pull request #60 from carllerche/header-value-from-shared

Add HeaderValue::try_from_shared.
This commit is contained in:
Alex Crichton
2017-07-24 17:59:33 -05:00
committed by GitHub
+18 -17
View File
@@ -94,20 +94,7 @@ impl HeaderValue {
/// assert!(val.is_err());
/// ```
pub fn try_from_str(src: &str) -> Result<HeaderValue, InvalidValueError> {
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<HeaderValue, InvalidValueError> {
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, InvalidValueError> {
HeaderValue::try_from(src)
}
fn try_from<T: AsRef<[u8]> + Into<Bytes>>(src: T) -> Result<HeaderValue, InvalidValueError> {
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,
})
}