Skip to content

Commit 875cdfa

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 95ad79b commit 875cdfa

7 files changed

Lines changed: 186 additions & 38 deletions

File tree

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
@@ -128,14 +128,14 @@ macro_rules! standard_headers {
128128
let std = HeaderName::from(std);
129129
// Test lower case
130130
let bytes: Bytes =
131-
HeaderName::from_bytes(name_bytes).unwrap().inner.into();
131+
HeaderName::from_bytes(name_bytes).unwrap().into();
132132
assert_eq!(bytes, name);
133133
assert_eq!(HeaderName::from_bytes(name_bytes).unwrap(), std);
134134

135135
// Test upper case
136136
let upper = name.to_uppercase();
137137
let bytes: Bytes =
138-
HeaderName::from_bytes(upper.as_bytes()).unwrap().inner.into();
138+
HeaderName::from_bytes(upper.as_bytes()).unwrap().into();
139139
assert_eq!(bytes, name_bytes);
140140
assert_eq!(HeaderName::from_bytes(upper.as_bytes()).unwrap(),
141141
std);
@@ -1256,10 +1256,6 @@ impl HeaderName {
12561256
Repr::Custom(ref v) => &*v.0,
12571257
}
12581258
}
1259-
1260-
pub(super) fn into_bytes(self) -> Bytes {
1261-
self.inner.into()
1262-
}
12631259
}
12641260

12651261
impl FromStr for HeaderName {
@@ -1332,6 +1328,13 @@ impl From<Custom> for Bytes {
13321328
}
13331329
}
13341330

1331+
impl From<HeaderName> for Bytes {
1332+
#[inline]
1333+
fn from(name: HeaderName) -> Bytes {
1334+
name.inner.into()
1335+
}
1336+
}
1337+
13351338
impl<'a> TryFrom<&'a str> for HeaderName {
13361339
type Error = InvalidHeaderName;
13371340
#[inline]
@@ -1374,6 +1377,14 @@ impl TryFrom<Vec<u8>> for HeaderName {
13741377
}
13751378
}
13761379

1380+
impl TryFrom<Bytes> for HeaderName {
1381+
type Error = InvalidHeaderName;
1382+
#[inline]
1383+
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
1384+
Self::from_bytes(bytes.as_ref())
1385+
}
1386+
}
1387+
13771388
#[doc(hidden)]
13781389
impl From<StandardHeader> for HeaderName {
13791390
fn from(src: StandardHeader) -> HeaderName {

src/header/value.rs

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

187187
HeaderValue::from_bytes(src.as_ref())
@@ -397,7 +397,7 @@ impl From<HeaderName> for HeaderValue {
397397
#[inline]
398398
fn from(h: HeaderName) -> HeaderValue {
399399
HeaderValue {
400-
inner: h.into_bytes(),
400+
inner: h.into(),
401401
is_sensitive: false,
402402
}
403403
}
@@ -515,6 +515,13 @@ impl FromStr for HeaderValue {
515515
}
516516
}
517517

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

554561
#[inline]
555562
fn try_from(t: String) -> Result<Self, Self::Error> {
556-
HeaderValue::from_shared(t.into())
563+
HeaderValue::try_from(Bytes::from(t))
557564
}
558565
}
559566

@@ -562,7 +569,16 @@ impl TryFrom<Vec<u8>> for HeaderValue {
562569

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

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
@@ -283,13 +283,12 @@ impl Uri {
283283
T: AsRef<[u8]> + 'static,
284284
{
285285
if_downcast_into!(T, Bytes, src, {
286-
return Uri::from_shared(src);
286+
return Uri::try_from(src);
287287
});
288288

289289
Uri::try_from(src.as_ref())
290290
}
291291

292-
// Not public while `bytes` is unstable.
293292
fn from_shared(s: Bytes) -> Result<Uri, InvalidUri> {
294293
use self::ErrorKind::*;
295294

@@ -317,7 +316,7 @@ impl Uri {
317316
});
318317
}
319318
_ => {
320-
let authority = Authority::from_shared(s)?;
319+
let authority = Authority::try_from(s)?;
321320

322321
return Ok(Uri {
323322
scheme: Scheme::empty(),
@@ -333,7 +332,7 @@ impl Uri {
333332
return Ok(Uri {
334333
scheme: Scheme::empty(),
335334
authority: Authority::empty(),
336-
path_and_query: PathAndQuery::from_shared(s)?,
335+
path_and_query: PathAndQuery::try_from(s)?,
337336
});
338337
}
339338

@@ -360,7 +359,7 @@ impl Uri {
360359
/// ```
361360
pub fn from_static(src: &'static str) -> Self {
362361
let s = Bytes::from_static(src.as_bytes());
363-
match Uri::from_shared(s) {
362+
match Uri::try_from(s) {
364363
Ok(uri) => uri,
365364
Err(e) => panic!("static str is not valid URI: {}", e),
366365
}
@@ -706,12 +705,40 @@ impl Uri {
706705
}
707706
}
708707

708+
impl TryFrom<Bytes> for Uri {
709+
type Error = InvalidUri;
710+
711+
/// Attempt to convert a `Uri` from `Bytes`
712+
///
713+
/// # Examples
714+
///
715+
/// ```
716+
/// # extern crate http;
717+
/// # use http::uri::*;
718+
/// extern crate bytes;
719+
///
720+
/// use std::convert::TryFrom;
721+
/// use bytes::Bytes;
722+
///
723+
/// # pub fn main() {
724+
/// let bytes = Bytes::from("http://example.com/foo");
725+
/// let uri = Uri::try_from(bytes).unwrap();
726+
///
727+
/// assert_eq!(uri.host().unwrap(), "example.com");
728+
/// assert_eq!(uri.path(), "/foo");
729+
/// # }
730+
/// ```
731+
fn try_from(t: Bytes) -> Result<Uri, Self::Error> {
732+
Uri::from_shared(t)
733+
}
734+
}
735+
709736
impl<'a> TryFrom<&'a [u8]> for Uri {
710737
type Error = InvalidUri;
711738

712739
#[inline]
713740
fn try_from(t: &'a [u8]) -> Result<Self, Self::Error> {
714-
Uri::from_shared(Bytes::copy_from_slice(t))
741+
Uri::try_from(Bytes::copy_from_slice(t))
715742
}
716743
}
717744

@@ -738,7 +765,7 @@ impl TryFrom<String> for Uri {
738765

739766
#[inline]
740767
fn try_from(t: String) -> Result<Self, Self::Error> {
741-
Uri::from_shared(Bytes::from(t))
768+
Uri::try_from(Bytes::from(t))
742769
}
743770
}
744771

@@ -747,7 +774,7 @@ impl<'a> TryFrom<Vec<u8>> for Uri {
747774

748775
#[inline]
749776
fn try_from(vec: Vec<u8>) -> Result<Self, Self::Error> {
750-
Uri::from_shared(Bytes::from(vec))
777+
Uri::try_from(Bytes::from(vec))
751778
}
752779
}
753780

@@ -876,7 +903,7 @@ fn parse_full(mut s: Bytes) -> Result<Uri, InvalidUri> {
876903
Ok(Uri {
877904
scheme: scheme.into(),
878905
authority: authority,
879-
path_and_query: PathAndQuery::from_shared(s)?,
906+
path_and_query: PathAndQuery::try_from(s)?,
880907
})
881908
}
882909

0 commit comments

Comments
 (0)