Skip to content

Commit 43dffa1

Browse files
committed
Upgrade to bytes 0.5
1 parent 22448cd commit 43dffa1

File tree

9 files changed

+31
-24
lines changed

9 files changed

+31
-24
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ matrix:
1414
- rustup target add wasm32-unknown-unknown
1515
- cargo build --target=wasm32-unknown-unknown
1616
# minimum rustc version
17-
- rust: 1.36.0
17+
- rust: 1.39.0
1818
script: cargo build
1919

2020
script:

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ edition = "2018"
2525
publish = false
2626

2727
[dependencies]
28-
bytes = "0.4"
28+
bytes = "0.5"
2929
fnv = "1.0.5"
3030
itoa = "0.4.1"
3131

src/byte_str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'a> From<&'a str> for ByteStr {
6060
#[inline]
6161
fn from(src: &'a str) -> ByteStr {
6262
ByteStr {
63-
bytes: Bytes::from(src),
63+
bytes: Bytes::copy_from_slice(src.as_bytes()),
6464
}
6565
}
6666
}

src/header/name.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ impl HeaderName {
16661666
match parse_hdr(src, &mut buf, &HEADER_CHARS)?.inner {
16671667
Repr::Standard(std) => Ok(std.into()),
16681668
Repr::Custom(MaybeLower { buf, lower: true }) => {
1669-
let buf = Bytes::from(buf);
1669+
let buf = Bytes::copy_from_slice(buf);
16701670
let val = unsafe { ByteStr::from_utf8_unchecked(buf) };
16711671
Ok(Custom(val).into())
16721672
}
@@ -1681,7 +1681,7 @@ impl HeaderName {
16811681
return Err(InvalidHeaderName::new());
16821682
}
16831683

1684-
dst.put(b);
1684+
dst.put_u8(b);
16851685
}
16861686

16871687
let val = unsafe { ByteStr::from_utf8_unchecked(dst.freeze()) };
@@ -1716,7 +1716,7 @@ impl HeaderName {
17161716
match parse_hdr(src, &mut buf, &HEADER_CHARS_H2)?.inner {
17171717
Repr::Standard(std) => Ok(std.into()),
17181718
Repr::Custom(MaybeLower { buf, lower: true }) => {
1719-
let buf = Bytes::from(buf);
1719+
let buf = Bytes::copy_from_slice(buf);
17201720
let val = unsafe { ByteStr::from_utf8_unchecked(buf) };
17211721
Ok(Custom(val).into())
17221722
}
@@ -1727,7 +1727,7 @@ impl HeaderName {
17271727
}
17281728
}
17291729

1730-
let buf = Bytes::from(buf);
1730+
let buf = Bytes::copy_from_slice(buf);
17311731
let val = unsafe { ByteStr::from_utf8_unchecked(buf) };
17321732
Ok(Custom(val).into())
17331733
}
@@ -2084,7 +2084,7 @@ impl<'a> From<HdrName<'a>> for HeaderName {
20842084
},
20852085
Repr::Custom(maybe_lower) => {
20862086
if maybe_lower.lower {
2087-
let buf = Bytes::from(&maybe_lower.buf[..]);
2087+
let buf = Bytes::copy_from_slice(&maybe_lower.buf[..]);
20882088
let byte_str = unsafe { ByteStr::from_utf8_unchecked(buf) };
20892089

20902090
HeaderName {
@@ -2095,7 +2095,7 @@ impl<'a> From<HdrName<'a>> for HeaderName {
20952095
let mut dst = BytesMut::with_capacity(maybe_lower.buf.len());
20962096

20972097
for b in maybe_lower.buf.iter() {
2098-
dst.put(HEADER_CHARS[*b as usize]);
2098+
dst.put_u8(HEADER_CHARS[*b as usize]);
20992099
}
21002100

21012101
let buf = unsafe { ByteStr::from_utf8_unchecked(dst.freeze()) };

src/header/value.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl HeaderValue {
103103
/// ```
104104
#[inline]
105105
pub fn from_str(src: &str) -> Result<HeaderValue, InvalidHeaderValue> {
106-
HeaderValue::try_from(src)
106+
HeaderValue::try_from_generic(src, |s| Bytes::copy_from_slice(s.as_bytes()))
107107
}
108108

109109
/// Converts a HeaderName into a HeaderValue
@@ -149,7 +149,7 @@ impl HeaderValue {
149149
/// ```
150150
#[inline]
151151
pub fn from_bytes(src: &[u8]) -> Result<HeaderValue, InvalidHeaderValue> {
152-
HeaderValue::try_from(src)
152+
HeaderValue::try_from_generic(src, Bytes::copy_from_slice)
153153
}
154154

155155
/// Attempt to convert a `Bytes` buffer to a `HeaderValue`.
@@ -162,7 +162,7 @@ impl HeaderValue {
162162
/// implementation once the trait is stabilized in std.
163163
#[inline]
164164
pub fn from_shared(src: Bytes) -> Result<HeaderValue, InvalidHeaderValueBytes> {
165-
HeaderValue::try_from(src).map_err(InvalidHeaderValueBytes)
165+
HeaderValue::try_from_generic(src, std::convert::identity).map_err(InvalidHeaderValueBytes)
166166
}
167167

168168
/// Convert a `Bytes` directly into a `HeaderValue` without validating.
@@ -188,14 +188,14 @@ impl HeaderValue {
188188
}
189189
}
190190

191-
fn try_from<T: AsRef<[u8]> + Into<Bytes>>(src: T) -> Result<HeaderValue, InvalidHeaderValue> {
191+
fn try_from_generic<T: AsRef<[u8]>, F: FnOnce(T) -> Bytes>(src: T, into: F) -> Result<HeaderValue, InvalidHeaderValue> {
192192
for &b in src.as_ref() {
193193
if !is_valid(b) {
194194
return Err(InvalidHeaderValue { _priv: () });
195195
}
196196
}
197197
Ok(HeaderValue {
198-
inner: src.into(),
198+
inner: into(src),
199199
is_sensitive: false,
200200
})
201201
}
@@ -524,6 +524,15 @@ impl TryFrom<String> for HeaderValue {
524524
}
525525
}
526526

527+
impl TryFrom<Vec<u8>> for HeaderValue {
528+
type Error = InvalidHeaderValueBytes;
529+
530+
#[inline]
531+
fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
532+
HeaderValue::from_shared(vec.into())
533+
}
534+
}
535+
527536
impl TryFrom<Bytes> for HeaderValue {
528537
type Error = InvalidHeaderValueBytes;
529538

src/uri/authority.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Deprecated in 1.26, needed until our minimum version is >=1.23.
2-
#[allow(unused, deprecated)]
3-
use std::ascii::AsciiExt;
41
use std::convert::TryFrom;
52
use std::hash::{Hash, Hasher};
63
use std::str::FromStr;
@@ -474,7 +471,9 @@ impl<'a> TryFrom<&'a [u8]> for Authority {
474471
}
475472

476473
Ok(Authority {
477-
data: unsafe { ByteStr::from_utf8_unchecked(s.into()) },
474+
data: unsafe {
475+
ByteStr::from_utf8_unchecked(Bytes::copy_from_slice(s))
476+
},
478477
})
479478
}
480479
}

src/uri/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ impl FromStr for Uri {
878878

879879
#[inline]
880880
fn from_str(s: &str) -> Result<Uri, InvalidUri> {
881-
Uri::from_shared(s.into()).map_err(|e| e.0)
881+
Uri::from_shared(Bytes::copy_from_slice(s.as_bytes())).map_err(|e| e.0)
882882
}
883883
}
884884

src/uri/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl<'a> TryFrom<&'a [u8]> for PathAndQuery {
295295
type Error = InvalidUri;
296296
#[inline]
297297
fn try_from(s: &'a [u8]) -> Result<Self, Self::Error> {
298-
PathAndQuery::from_shared(s.into()).map_err(|e| e.0)
298+
PathAndQuery::from_shared(Bytes::copy_from_slice(s)).map_err(|e| e.0)
299299
}
300300
}
301301

src/uri/scheme.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Deprecated in 1.26, needed until our minimum version is >=1.23.
2-
#[allow(unused, deprecated)]
3-
use std::ascii::AsciiExt;
41
use std::convert::TryFrom;
52
use std::fmt;
63
use std::hash::{Hash, Hasher};
@@ -147,7 +144,9 @@ impl<'a> TryFrom<&'a [u8]> for Scheme {
147144
Standard(p) => Ok(Standard(p).into()),
148145
Other(_) => {
149146
// Unsafe: parse_exact already checks for a strict subset of UTF-8
150-
Ok(Other(Box::new(unsafe { ByteStr::from_utf8_unchecked(s.into()) })).into())
147+
Ok(Other(Box::new(unsafe {
148+
ByteStr::from_utf8_unchecked(Bytes::copy_from_slice(s))
149+
})).into())
151150
}
152151
}
153152
}

0 commit comments

Comments
 (0)