Skip to content

Commit 8edb94c

Browse files
committed
Bring back TryFrom<Bytes> implementations
This is mostly a revert of commit 4ce5e6a, but this does not bring back the error variants suffixed with `Bytes` (db9b1b9). This also replaces usage of the internal `from_shared` associated functions with `try_from`. Closes <#459>.
1 parent e54da71 commit 8edb94c

File tree

7 files changed

+186
-38
lines changed

7 files changed

+186
-38
lines changed

benches/header_value.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
extern crate test;
44

5+
use std::convert::TryFrom;
6+
57
use bytes::Bytes;
68
use http::HeaderValue;
79
use test::Bencher;
@@ -14,7 +16,7 @@ fn from_shared_short(b: &mut Bencher) {
1416
b.bytes = SHORT.len() as u64;
1517
let bytes = Bytes::from_static(SHORT);
1618
b.iter(|| {
17-
HeaderValue::from_maybe_shared(bytes.clone()).unwrap();
19+
HeaderValue::try_from(bytes.clone()).unwrap();
1820
});
1921
}
2022

@@ -23,7 +25,7 @@ fn from_shared_long(b: &mut Bencher) {
2325
b.bytes = LONG.len() as u64;
2426
let bytes = Bytes::from_static(LONG);
2527
b.iter(|| {
26-
HeaderValue::from_maybe_shared(bytes.clone()).unwrap();
28+
HeaderValue::try_from(bytes.clone()).unwrap();
2729
});
2830
}
2931

src/header/name.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ macro_rules! standard_headers {
118118
// Test lower case
119119
let name_bytes = name.as_bytes();
120120
let bytes: Bytes =
121-
HeaderName::from_bytes(name_bytes).unwrap().inner.into();
121+
HeaderName::from_bytes(name_bytes).unwrap().into();
122122
assert_eq!(bytes, name_bytes);
123123
assert_eq!(HeaderName::from_bytes(name_bytes).unwrap(), std);
124124

125125
// Test upper case
126126
let upper = name.to_uppercase().to_string();
127127
let bytes: Bytes =
128-
HeaderName::from_bytes(upper.as_bytes()).unwrap().inner.into();
128+
HeaderName::from_bytes(upper.as_bytes()).unwrap().into();
129129
assert_eq!(bytes, name.as_bytes());
130130
assert_eq!(HeaderName::from_bytes(upper.as_bytes()).unwrap(),
131131
std);
@@ -1800,10 +1800,6 @@ impl HeaderName {
18001800
Repr::Custom(ref v) => &*v.0,
18011801
}
18021802
}
1803-
1804-
pub(super) fn into_bytes(self) -> Bytes {
1805-
self.inner.into()
1806-
}
18071803
}
18081804

18091805
impl FromStr for HeaderName {
@@ -1876,6 +1872,13 @@ impl From<Custom> for Bytes {
18761872
}
18771873
}
18781874

1875+
impl From<HeaderName> for Bytes {
1876+
#[inline]
1877+
fn from(name: HeaderName) -> Bytes {
1878+
name.inner.into()
1879+
}
1880+
}
1881+
18791882
impl<'a> TryFrom<&'a str> for HeaderName {
18801883
type Error = InvalidHeaderName;
18811884
#[inline]
@@ -1918,6 +1921,14 @@ impl TryFrom<Vec<u8>> for HeaderName {
19181921
}
19191922
}
19201923

1924+
impl TryFrom<Bytes> for HeaderName {
1925+
type Error = InvalidHeaderName;
1926+
#[inline]
1927+
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
1928+
Self::from_bytes(bytes.as_ref())
1929+
}
1930+
}
1931+
19211932
#[doc(hidden)]
19221933
impl From<StandardHeader> for HeaderName {
19231934
fn from(src: StandardHeader) -> HeaderName {

src/header/value.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl HeaderValue {
180180
T: AsRef<[u8]> + 'static,
181181
{
182182
if_downcast_into!(T, Bytes, src, {
183-
return HeaderValue::from_shared(src);
183+
return HeaderValue::try_from(src);
184184
});
185185

186186
HeaderValue::from_bytes(src.as_ref())
@@ -396,7 +396,7 @@ impl From<HeaderName> for HeaderValue {
396396
#[inline]
397397
fn from(h: HeaderName) -> HeaderValue {
398398
HeaderValue {
399-
inner: h.into_bytes(),
399+
inner: h.into(),
400400
is_sensitive: false,
401401
}
402402
}
@@ -514,6 +514,13 @@ impl FromStr for HeaderValue {
514514
}
515515
}
516516

517+
impl From<HeaderValue> for Bytes {
518+
#[inline]
519+
fn from(value: HeaderValue) -> Bytes {
520+
value.inner
521+
}
522+
}
523+
517524
impl<'a> From<&'a HeaderValue> for HeaderValue {
518525
#[inline]
519526
fn from(t: &'a HeaderValue) -> Self {
@@ -552,7 +559,7 @@ impl TryFrom<String> for HeaderValue {
552559

553560
#[inline]
554561
fn try_from(t: String) -> Result<Self, Self::Error> {
555-
HeaderValue::from_shared(t.into())
562+
HeaderValue::try_from(Bytes::from(t))
556563
}
557564
}
558565

@@ -561,7 +568,16 @@ impl TryFrom<Vec<u8>> for HeaderValue {
561568

562569
#[inline]
563570
fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
564-
HeaderValue::from_shared(vec.into())
571+
HeaderValue::try_from(Bytes::from(vec))
572+
}
573+
}
574+
575+
impl TryFrom<Bytes> for HeaderValue {
576+
type Error = InvalidHeaderValue;
577+
578+
#[inline]
579+
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
580+
HeaderValue::from_shared(bytes)
565581
}
566582
}
567583

src/uri/authority.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ impl Authority {
2121
}
2222
}
2323

24-
// Not public while `bytes` is unstable.
25-
pub(super) fn from_shared(s: Bytes) -> Result<Self, InvalidUri> {
24+
fn from_shared(s: Bytes) -> Result<Self, InvalidUri> {
2625
// Precondition on create_authority: trivially satisfied by the
2726
// identity clousre
2827
create_authority(s, |s| s)
@@ -46,7 +45,7 @@ impl Authority {
4645
/// assert_eq!(authority.host(), "example.com");
4746
/// ```
4847
pub fn from_static(src: &'static str) -> Self {
49-
Authority::from_shared(Bytes::from_static(src.as_bytes()))
48+
Authority::try_from(Bytes::from_static(src.as_bytes()))
5049
.expect("static str is not valid authority")
5150
}
5251

@@ -59,7 +58,7 @@ impl Authority {
5958
T: AsRef<[u8]> + 'static,
6059
{
6160
if_downcast_into!(T, Bytes, src, {
62-
return Authority::from_shared(src);
61+
return Authority::try_from(src);
6362
});
6463

6564
Authority::try_from(src.as_ref())
@@ -259,8 +258,31 @@ impl Authority {
259258
}
260259
}
261260

262-
// Purposefully not public while `bytes` is unstable.
263-
// impl TryFrom<Bytes> for Authority
261+
impl TryFrom<Bytes> for Authority {
262+
type Error = InvalidUri;
263+
/// Attempt to convert an `Authority` from `Bytes`.
264+
///
265+
/// # Examples
266+
///
267+
/// ```
268+
/// # extern crate http;
269+
/// # use http::uri::*;
270+
/// extern crate bytes;
271+
///
272+
/// use std::convert::TryFrom;
273+
/// use bytes::Bytes;
274+
///
275+
/// # pub fn main() {
276+
/// let bytes = Bytes::from("example.com");
277+
/// let authority = Authority::try_from(bytes).unwrap();
278+
///
279+
/// assert_eq!(authority.host(), "example.com");
280+
/// # }
281+
/// ```
282+
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
283+
Authority::from_shared(bytes)
284+
}
285+
}
264286

265287
impl AsRef<str> for Authority {
266288
fn as_ref(&self) -> &str {
@@ -447,7 +469,7 @@ impl TryFrom<Vec<u8>> for Authority {
447469

448470
#[inline]
449471
fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
450-
Authority::from_shared(vec.into())
472+
Authority::try_from(Bytes::from(vec))
451473
}
452474
}
453475

@@ -456,7 +478,7 @@ impl TryFrom<String> for Authority {
456478

457479
#[inline]
458480
fn try_from(t: String) -> Result<Self, Self::Error> {
459-
Authority::from_shared(t.into())
481+
Authority::try_from(Bytes::from(t))
460482
}
461483
}
462484

@@ -468,6 +490,13 @@ impl FromStr for Authority {
468490
}
469491
}
470492

493+
impl From<Authority> for Bytes {
494+
#[inline]
495+
fn from(src: Authority) -> Bytes {
496+
src.data.into()
497+
}
498+
}
499+
471500
impl fmt::Debug for Authority {
472501
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
473502
f.write_str(self.as_str())
@@ -658,8 +687,7 @@ mod tests {
658687
let err = Authority::try_from([0xc0u8].as_ref()).unwrap_err();
659688
assert_eq!(err.0, ErrorKind::InvalidUriChar);
660689

661-
let err = Authority::from_shared(Bytes::from_static([0xc0u8].as_ref()))
662-
.unwrap_err();
690+
let err = Authority::try_from(Bytes::from_static([0xc0u8].as_ref())).unwrap_err();
663691
assert_eq!(err.0, ErrorKind::InvalidUriChar);
664692
}
665693

src/uri/mod.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,12 @@ impl Uri {
250250
T: AsRef<[u8]> + 'static,
251251
{
252252
if_downcast_into!(T, Bytes, src, {
253-
return Uri::from_shared(src);
253+
return Uri::try_from(src);
254254
});
255255

256256
Uri::try_from(src.as_ref())
257257
}
258258

259-
// Not public while `bytes` is unstable.
260259
fn from_shared(s: Bytes) -> Result<Uri, InvalidUri> {
261260
use self::ErrorKind::*;
262261

@@ -284,7 +283,7 @@ impl Uri {
284283
});
285284
}
286285
_ => {
287-
let authority = Authority::from_shared(s)?;
286+
let authority = Authority::try_from(s)?;
288287

289288
return Ok(Uri {
290289
scheme: Scheme::empty(),
@@ -300,7 +299,7 @@ impl Uri {
300299
return Ok(Uri {
301300
scheme: Scheme::empty(),
302301
authority: Authority::empty(),
303-
path_and_query: PathAndQuery::from_shared(s)?,
302+
path_and_query: PathAndQuery::try_from(s)?,
304303
});
305304
}
306305

@@ -327,7 +326,7 @@ impl Uri {
327326
/// ```
328327
pub fn from_static(src: &'static str) -> Self {
329328
let s = Bytes::from_static(src.as_bytes());
330-
match Uri::from_shared(s) {
329+
match Uri::try_from(s) {
331330
Ok(uri) => uri,
332331
Err(e) => panic!("static str is not valid URI: {}", e),
333332
}
@@ -675,12 +674,40 @@ impl Uri {
675674
}
676675
}
677676

677+
impl TryFrom<Bytes> for Uri {
678+
type Error = InvalidUri;
679+
680+
/// Attempt to convert a `Uri` from `Bytes`
681+
///
682+
/// # Examples
683+
///
684+
/// ```
685+
/// # extern crate http;
686+
/// # use http::uri::*;
687+
/// extern crate bytes;
688+
///
689+
/// use std::convert::TryFrom;
690+
/// use bytes::Bytes;
691+
///
692+
/// # pub fn main() {
693+
/// let bytes = Bytes::from("http://example.com/foo");
694+
/// let uri = Uri::try_from(bytes).unwrap();
695+
///
696+
/// assert_eq!(uri.host().unwrap(), "example.com");
697+
/// assert_eq!(uri.path(), "/foo");
698+
/// # }
699+
/// ```
700+
fn try_from(t: Bytes) -> Result<Uri, Self::Error> {
701+
Uri::from_shared(t)
702+
}
703+
}
704+
678705
impl<'a> TryFrom<&'a [u8]> for Uri {
679706
type Error = InvalidUri;
680707

681708
#[inline]
682709
fn try_from(t: &'a [u8]) -> Result<Self, Self::Error> {
683-
Uri::from_shared(Bytes::copy_from_slice(t))
710+
Uri::try_from(Bytes::copy_from_slice(t))
684711
}
685712
}
686713

@@ -707,7 +734,7 @@ impl TryFrom<String> for Uri {
707734

708735
#[inline]
709736
fn try_from(t: String) -> Result<Self, Self::Error> {
710-
Uri::from_shared(Bytes::from(t))
737+
Uri::try_from(Bytes::from(t))
711738
}
712739
}
713740

@@ -716,7 +743,7 @@ impl<'a> TryFrom<Vec<u8>> for Uri {
716743

717744
#[inline]
718745
fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
719-
Uri::from_shared(Bytes::from(vec))
746+
Uri::try_from(Bytes::from(vec))
720747
}
721748
}
722749

@@ -856,7 +883,7 @@ fn parse_full(mut s: Bytes) -> Result<Uri, InvalidUri> {
856883
Ok(Uri {
857884
scheme: scheme.into(),
858885
authority: authority,
859-
path_and_query: PathAndQuery::from_shared(s)?,
886+
path_and_query: PathAndQuery::try_from(s)?,
860887
})
861888
}
862889

0 commit comments

Comments
 (0)