Skip to content

Commit

Permalink
Upgrade to bytes 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Nov 26, 2019
1 parent 22448cd commit 43dffa1
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -25,7 +25,7 @@ edition = "2018"
publish = false

[dependencies]
bytes = "0.4"
bytes = "0.5"
fnv = "1.0.5"
itoa = "0.4.1"

Expand Down
2 changes: 1 addition & 1 deletion src/byte_str.rs
Expand Up @@ -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()),
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/header/name.rs
Expand Up @@ -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())
}
Expand All @@ -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()) };
Expand Down Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand Down Expand Up @@ -2084,7 +2084,7 @@ impl<'a> From<HdrName<'a>> 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 {
Expand All @@ -2095,7 +2095,7 @@ impl<'a> From<HdrName<'a>> 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()) };
Expand Down
19 changes: 14 additions & 5 deletions src/header/value.rs
Expand Up @@ -103,7 +103,7 @@ impl HeaderValue {
/// ```
#[inline]
pub fn from_str(src: &str) -> Result<HeaderValue, InvalidHeaderValue> {
HeaderValue::try_from(src)
HeaderValue::try_from_generic(src, |s| Bytes::copy_from_slice(s.as_bytes()))
}

/// Converts a HeaderName into a HeaderValue
Expand Down Expand Up @@ -149,7 +149,7 @@ impl HeaderValue {
/// ```
#[inline]
pub fn from_bytes(src: &[u8]) -> Result<HeaderValue, InvalidHeaderValue> {
HeaderValue::try_from(src)
HeaderValue::try_from_generic(src, Bytes::copy_from_slice)
}

/// Attempt to convert a `Bytes` buffer to a `HeaderValue`.
Expand All @@ -162,7 +162,7 @@ impl HeaderValue {
/// implementation once the trait is stabilized in std.
#[inline]
pub fn from_shared(src: Bytes) -> Result<HeaderValue, InvalidHeaderValueBytes> {
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.
Expand All @@ -188,14 +188,14 @@ impl HeaderValue {
}
}

fn try_from<T: AsRef<[u8]> + Into<Bytes>>(src: T) -> Result<HeaderValue, InvalidHeaderValue> {
fn try_from_generic<T: AsRef<[u8]>, F: FnOnce(T) -> Bytes>(src: T, into: F) -> Result<HeaderValue, InvalidHeaderValue> {
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,
})
}
Expand Down Expand Up @@ -524,6 +524,15 @@ impl TryFrom<String> for HeaderValue {
}
}

impl TryFrom<Vec<u8>> for HeaderValue {
type Error = InvalidHeaderValueBytes;

#[inline]
fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
HeaderValue::from_shared(vec.into())
}
}

impl TryFrom<Bytes> for HeaderValue {
type Error = InvalidHeaderValueBytes;

Expand Down
7 changes: 3 additions & 4 deletions 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;
Expand Down Expand Up @@ -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))
},
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/uri/mod.rs
Expand Up @@ -878,7 +878,7 @@ impl FromStr for Uri {

#[inline]
fn from_str(s: &str) -> Result<Uri, InvalidUri> {
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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/uri/path.rs
Expand Up @@ -295,7 +295,7 @@ impl<'a> TryFrom<&'a [u8]> for PathAndQuery {
type Error = InvalidUri;
#[inline]
fn try_from(s: &'a [u8]) -> Result<Self, Self::Error> {
PathAndQuery::from_shared(s.into()).map_err(|e| e.0)
PathAndQuery::from_shared(Bytes::copy_from_slice(s)).map_err(|e| e.0)
}
}

Expand Down
7 changes: 3 additions & 4 deletions 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};
Expand Down Expand Up @@ -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())
}
}
}
Expand Down

0 comments on commit 43dffa1

Please sign in to comment.