From 43dffa1eb79f6801e5e07f3338fa56191dc454bb Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 25 Nov 2019 12:39:51 -0800 Subject: [PATCH] Upgrade to bytes 0.5 --- .travis.yml | 2 +- Cargo.toml | 2 +- src/byte_str.rs | 2 +- src/header/name.rs | 12 ++++++------ src/header/value.rs | 19 ++++++++++++++----- src/uri/authority.rs | 7 +++---- src/uri/mod.rs | 2 +- src/uri/path.rs | 2 +- src/uri/scheme.rs | 7 +++---- 9 files changed, 31 insertions(+), 24 deletions(-) diff --git a/.travis.yml b/.travis.yml index e510609b..d32bf418 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ matrix: - rustup target add wasm32-unknown-unknown - cargo build --target=wasm32-unknown-unknown # minimum rustc version - - rust: 1.36.0 + - rust: 1.39.0 script: cargo build script: diff --git a/Cargo.toml b/Cargo.toml index 5087eba3..aa2e2a8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ edition = "2018" publish = false [dependencies] -bytes = "0.4" +bytes = "0.5" fnv = "1.0.5" itoa = "0.4.1" diff --git a/src/byte_str.rs b/src/byte_str.rs index 41a532e3..686c661a 100644 --- a/src/byte_str.rs +++ b/src/byte_str.rs @@ -60,7 +60,7 @@ impl<'a> From<&'a str> for ByteStr { #[inline] fn from(src: &'a str) -> ByteStr { ByteStr { - bytes: Bytes::from(src), + bytes: Bytes::copy_from_slice(src.as_bytes()), } } } diff --git a/src/header/name.rs b/src/header/name.rs index 5cbd8229..ec2c4434 100644 --- a/src/header/name.rs +++ b/src/header/name.rs @@ -1666,7 +1666,7 @@ impl HeaderName { match parse_hdr(src, &mut buf, &HEADER_CHARS)?.inner { Repr::Standard(std) => Ok(std.into()), Repr::Custom(MaybeLower { buf, lower: true }) => { - let buf = Bytes::from(buf); + let buf = Bytes::copy_from_slice(buf); let val = unsafe { ByteStr::from_utf8_unchecked(buf) }; Ok(Custom(val).into()) } @@ -1681,7 +1681,7 @@ impl HeaderName { return Err(InvalidHeaderName::new()); } - dst.put(b); + dst.put_u8(b); } let val = unsafe { ByteStr::from_utf8_unchecked(dst.freeze()) }; @@ -1716,7 +1716,7 @@ impl HeaderName { match parse_hdr(src, &mut buf, &HEADER_CHARS_H2)?.inner { Repr::Standard(std) => Ok(std.into()), Repr::Custom(MaybeLower { buf, lower: true }) => { - let buf = Bytes::from(buf); + let buf = Bytes::copy_from_slice(buf); let val = unsafe { ByteStr::from_utf8_unchecked(buf) }; Ok(Custom(val).into()) } @@ -1727,7 +1727,7 @@ impl HeaderName { } } - let buf = Bytes::from(buf); + let buf = Bytes::copy_from_slice(buf); let val = unsafe { ByteStr::from_utf8_unchecked(buf) }; Ok(Custom(val).into()) } @@ -2084,7 +2084,7 @@ impl<'a> From> for HeaderName { }, Repr::Custom(maybe_lower) => { if maybe_lower.lower { - let buf = Bytes::from(&maybe_lower.buf[..]); + let buf = Bytes::copy_from_slice(&maybe_lower.buf[..]); let byte_str = unsafe { ByteStr::from_utf8_unchecked(buf) }; HeaderName { @@ -2095,7 +2095,7 @@ impl<'a> From> for HeaderName { let mut dst = BytesMut::with_capacity(maybe_lower.buf.len()); for b in maybe_lower.buf.iter() { - dst.put(HEADER_CHARS[*b as usize]); + dst.put_u8(HEADER_CHARS[*b as usize]); } let buf = unsafe { ByteStr::from_utf8_unchecked(dst.freeze()) }; diff --git a/src/header/value.rs b/src/header/value.rs index 8b3c29c9..7379e468 100644 --- a/src/header/value.rs +++ b/src/header/value.rs @@ -103,7 +103,7 @@ impl HeaderValue { /// ``` #[inline] pub fn from_str(src: &str) -> Result { - HeaderValue::try_from(src) + HeaderValue::try_from_generic(src, |s| Bytes::copy_from_slice(s.as_bytes())) } /// Converts a HeaderName into a HeaderValue @@ -149,7 +149,7 @@ impl HeaderValue { /// ``` #[inline] pub fn from_bytes(src: &[u8]) -> Result { - HeaderValue::try_from(src) + HeaderValue::try_from_generic(src, Bytes::copy_from_slice) } /// Attempt to convert a `Bytes` buffer to a `HeaderValue`. @@ -162,7 +162,7 @@ impl HeaderValue { /// implementation once the trait is stabilized in std. #[inline] pub fn from_shared(src: Bytes) -> Result { - HeaderValue::try_from(src).map_err(InvalidHeaderValueBytes) + HeaderValue::try_from_generic(src, std::convert::identity).map_err(InvalidHeaderValueBytes) } /// Convert a `Bytes` directly into a `HeaderValue` without validating. @@ -188,14 +188,14 @@ impl HeaderValue { } } - fn try_from + Into>(src: T) -> Result { + fn try_from_generic, F: FnOnce(T) -> Bytes>(src: T, into: F) -> Result { for &b in src.as_ref() { if !is_valid(b) { return Err(InvalidHeaderValue { _priv: () }); } } Ok(HeaderValue { - inner: src.into(), + inner: into(src), is_sensitive: false, }) } @@ -524,6 +524,15 @@ impl TryFrom for HeaderValue { } } +impl TryFrom> for HeaderValue { + type Error = InvalidHeaderValueBytes; + + #[inline] + fn try_from(vec: Vec) -> Result { + HeaderValue::from_shared(vec.into()) + } +} + impl TryFrom for HeaderValue { type Error = InvalidHeaderValueBytes; diff --git a/src/uri/authority.rs b/src/uri/authority.rs index 9ec54d1c..66087b2a 100644 --- a/src/uri/authority.rs +++ b/src/uri/authority.rs @@ -1,6 +1,3 @@ -// Deprecated in 1.26, needed until our minimum version is >=1.23. -#[allow(unused, deprecated)] -use std::ascii::AsciiExt; use std::convert::TryFrom; use std::hash::{Hash, Hasher}; use std::str::FromStr; @@ -474,7 +471,9 @@ impl<'a> TryFrom<&'a [u8]> for Authority { } Ok(Authority { - data: unsafe { ByteStr::from_utf8_unchecked(s.into()) }, + data: unsafe { + ByteStr::from_utf8_unchecked(Bytes::copy_from_slice(s)) + }, }) } } diff --git a/src/uri/mod.rs b/src/uri/mod.rs index 26b10ddd..0d022c48 100644 --- a/src/uri/mod.rs +++ b/src/uri/mod.rs @@ -878,7 +878,7 @@ impl FromStr for Uri { #[inline] fn from_str(s: &str) -> Result { - Uri::from_shared(s.into()).map_err(|e| e.0) + Uri::from_shared(Bytes::copy_from_slice(s.as_bytes())).map_err(|e| e.0) } } diff --git a/src/uri/path.rs b/src/uri/path.rs index 6981be88..851db5b7 100644 --- a/src/uri/path.rs +++ b/src/uri/path.rs @@ -295,7 +295,7 @@ impl<'a> TryFrom<&'a [u8]> for PathAndQuery { type Error = InvalidUri; #[inline] fn try_from(s: &'a [u8]) -> Result { - PathAndQuery::from_shared(s.into()).map_err(|e| e.0) + PathAndQuery::from_shared(Bytes::copy_from_slice(s)).map_err(|e| e.0) } } diff --git a/src/uri/scheme.rs b/src/uri/scheme.rs index 8b481827..f8f32ae0 100644 --- a/src/uri/scheme.rs +++ b/src/uri/scheme.rs @@ -1,6 +1,3 @@ -// Deprecated in 1.26, needed until our minimum version is >=1.23. -#[allow(unused, deprecated)] -use std::ascii::AsciiExt; use std::convert::TryFrom; use std::fmt; use std::hash::{Hash, Hasher}; @@ -147,7 +144,9 @@ impl<'a> TryFrom<&'a [u8]> for Scheme { Standard(p) => Ok(Standard(p).into()), Other(_) => { // Unsafe: parse_exact already checks for a strict subset of UTF-8 - Ok(Other(Box::new(unsafe { ByteStr::from_utf8_unchecked(s.into()) })).into()) + Ok(Other(Box::new(unsafe { + ByteStr::from_utf8_unchecked(Bytes::copy_from_slice(s)) + })).into()) } } }