Skip to content

Commit 4ce5e6a

Browse files
committed
Make all usage of Bytes private
Removes `from_shared` and `try_from` constructors for all types.
1 parent 0591bba commit 4ce5e6a

File tree

6 files changed

+72
-229
lines changed

6 files changed

+72
-229
lines changed

src/header/name.rs

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

129129
// Test upper case
130130
let upper = name.to_uppercase().to_string();
131131
let bytes: Bytes =
132-
HeaderName::from_bytes(upper.as_bytes()).unwrap().into();
132+
HeaderName::from_bytes(upper.as_bytes()).unwrap().inner.into();
133133
assert_eq!(bytes, name.as_bytes());
134134
assert_eq!(HeaderName::from_bytes(upper.as_bytes()).unwrap(),
135135
std);
@@ -1809,6 +1809,10 @@ impl HeaderName {
18091809
Repr::Custom(ref v) => &*v.0,
18101810
}
18111811
}
1812+
1813+
pub(super) fn into_bytes(self) -> Bytes {
1814+
self.inner.into()
1815+
}
18121816
}
18131817

18141818
impl FromStr for HeaderName {
@@ -1881,13 +1885,6 @@ impl From<Custom> for Bytes {
18811885
}
18821886
}
18831887

1884-
impl From<HeaderName> for Bytes {
1885-
#[inline]
1886-
fn from(name: HeaderName) -> Bytes {
1887-
name.inner.into()
1888-
}
1889-
}
1890-
18911888
impl<'a> TryFrom<&'a str> for HeaderName {
18921889
type Error = InvalidHeaderName;
18931890
#[inline]
@@ -1912,14 +1909,6 @@ impl<'a> TryFrom<&'a [u8]> for HeaderName {
19121909
}
19131910
}
19141911

1915-
impl TryFrom<Bytes> for HeaderName {
1916-
type Error = InvalidHeaderNameBytes;
1917-
#[inline]
1918-
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
1919-
Self::from_bytes(bytes.as_ref()).map_err(InvalidHeaderNameBytes)
1920-
}
1921-
}
1922-
19231912
#[doc(hidden)]
19241913
impl From<StandardHeader> for HeaderName {
19251914
fn from(src: StandardHeader) -> HeaderName {

src/header/value.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,11 @@ impl HeaderValue {
161161
/// This function is intended to be replaced in the future by a `TryFrom`
162162
/// implementation once the trait is stabilized in std.
163163
#[inline]
164-
pub fn from_shared(src: Bytes) -> Result<HeaderValue, InvalidHeaderValueBytes> {
164+
fn from_shared(src: Bytes) -> Result<HeaderValue, InvalidHeaderValueBytes> {
165165
HeaderValue::try_from_generic(src, std::convert::identity).map_err(InvalidHeaderValueBytes)
166166
}
167167

168+
/*
168169
/// Convert a `Bytes` directly into a `HeaderValue` without validating.
169170
///
170171
/// This function does NOT validate that illegal bytes are not contained
@@ -187,6 +188,7 @@ impl HeaderValue {
187188
}
188189
}
189190
}
191+
*/
190192

191193
fn try_from_generic<T: AsRef<[u8]>, F: FnOnce(T) -> Bytes>(src: T, into: F) -> Result<HeaderValue, InvalidHeaderValue> {
192194
for &b in src.as_ref() {
@@ -357,7 +359,7 @@ impl From<HeaderName> for HeaderValue {
357359
#[inline]
358360
fn from(h: HeaderName) -> HeaderValue {
359361
HeaderValue {
360-
inner: h.into(),
362+
inner: h.into_bytes(),
361363
is_sensitive: false,
362364
}
363365
}
@@ -475,13 +477,6 @@ impl FromStr for HeaderValue {
475477
}
476478
}
477479

478-
impl From<HeaderValue> for Bytes {
479-
#[inline]
480-
fn from(value: HeaderValue) -> Bytes {
481-
value.inner
482-
}
483-
}
484-
485480
impl<'a> From<&'a HeaderValue> for HeaderValue {
486481
#[inline]
487482
fn from(t: &'a HeaderValue) -> Self {
@@ -533,15 +528,6 @@ impl TryFrom<Vec<u8>> for HeaderValue {
533528
}
534529
}
535530

536-
impl TryFrom<Bytes> for HeaderValue {
537-
type Error = InvalidHeaderValueBytes;
538-
539-
#[inline]
540-
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
541-
HeaderValue::from_shared(bytes)
542-
}
543-
}
544-
545531
#[cfg(test)]
546532
mod try_from_header_name_tests {
547533
use super::*;

src/uri/authority.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,16 @@ impl Authority {
4141
/// assert_eq!(authority.host(), "example.com");
4242
/// # }
4343
/// ```
44-
pub fn from_shared(s: Bytes) -> Result<Self, InvalidUriBytes> {
45-
TryFrom::try_from(s)
44+
pub(super) fn from_shared(s: Bytes) -> Result<Self, InvalidUriBytes> {
45+
let authority_end = Authority::parse_non_empty(&s[..]).map_err(InvalidUriBytes)?;
46+
47+
if authority_end != s.len() {
48+
return Err(ErrorKind::InvalidUriChar.into());
49+
}
50+
51+
Ok(Authority {
52+
data: unsafe { ByteStr::from_utf8_unchecked(s) },
53+
})
4654
}
4755

4856
/// Attempt to convert an `Authority` from a static string.
@@ -257,14 +265,9 @@ impl Authority {
257265
pub fn as_str(&self) -> &str {
258266
&self.data[..]
259267
}
260-
261-
/// Converts this `Authority` back to a sequence of bytes
262-
#[inline]
263-
pub fn into_bytes(self) -> Bytes {
264-
self.into()
265-
}
266268
}
267269

270+
/*
268271
impl TryFrom<Bytes> for Authority {
269272
type Error = InvalidUriBytes;
270273
/// Attempt to convert an `Authority` from `Bytes`.
@@ -298,6 +301,7 @@ impl TryFrom<Bytes> for Authority {
298301
})
299302
}
300303
}
304+
*/
301305

302306
impl AsRef<str> for Authority {
303307
fn as_ref(&self) -> &str {
@@ -494,13 +498,6 @@ impl FromStr for Authority {
494498
}
495499
}
496500

497-
impl From<Authority> for Bytes {
498-
#[inline]
499-
fn from(src: Authority) -> Bytes {
500-
src.data.into()
501-
}
502-
}
503-
504501
impl fmt::Debug for Authority {
505502
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
506503
f.write_str(self.as_str())

src/uri/mod.rs

Lines changed: 48 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,54 @@ impl Uri {
260260
/// assert_eq!(uri.path(), "/foo");
261261
/// # }
262262
/// ```
263-
pub fn from_shared(s: Bytes) -> Result<Uri, InvalidUriBytes> {
264-
TryFrom::try_from(s)
263+
fn from_shared(s: Bytes) -> Result<Uri, InvalidUriBytes> {
264+
use self::ErrorKind::*;
265+
266+
if s.len() > MAX_LEN {
267+
return Err(TooLong.into());
268+
}
269+
270+
match s.len() {
271+
0 => {
272+
return Err(Empty.into());
273+
}
274+
1 => match s[0] {
275+
b'/' => {
276+
return Ok(Uri {
277+
scheme: Scheme::empty(),
278+
authority: Authority::empty(),
279+
path_and_query: PathAndQuery::slash(),
280+
});
281+
}
282+
b'*' => {
283+
return Ok(Uri {
284+
scheme: Scheme::empty(),
285+
authority: Authority::empty(),
286+
path_and_query: PathAndQuery::star(),
287+
});
288+
}
289+
_ => {
290+
let authority = Authority::from_shared(s)?;
291+
292+
return Ok(Uri {
293+
scheme: Scheme::empty(),
294+
authority: authority,
295+
path_and_query: PathAndQuery::empty(),
296+
});
297+
}
298+
},
299+
_ => {}
300+
}
301+
302+
if s[0] == b'/' {
303+
return Ok(Uri {
304+
scheme: Scheme::empty(),
305+
authority: Authority::empty(),
306+
path_and_query: PathAndQuery::from_shared(s)?,
307+
});
308+
}
309+
310+
parse_full(s)
265311
}
266312

267313
/// Convert a `Uri` from a static string.
@@ -632,80 +678,6 @@ impl Uri {
632678
}
633679
}
634680

635-
impl TryFrom<Bytes> for Uri {
636-
type Error = InvalidUriBytes;
637-
638-
/// Attempt to convert a `Uri` from `Bytes`
639-
///
640-
/// # Examples
641-
///
642-
/// ```
643-
/// # extern crate http;
644-
/// # use http::uri::*;
645-
/// extern crate bytes;
646-
///
647-
/// use std::convert::TryFrom;
648-
/// use bytes::Bytes;
649-
///
650-
/// # pub fn main() {
651-
/// let bytes = Bytes::from("http://example.com/foo");
652-
/// let uri = Uri::try_from(bytes).unwrap();
653-
///
654-
/// assert_eq!(uri.host().unwrap(), "example.com");
655-
/// assert_eq!(uri.path(), "/foo");
656-
/// # }
657-
/// ```
658-
fn try_from(s: Bytes) -> Result<Uri, Self::Error> {
659-
use self::ErrorKind::*;
660-
661-
if s.len() > MAX_LEN {
662-
return Err(TooLong.into());
663-
}
664-
665-
match s.len() {
666-
0 => {
667-
return Err(Empty.into());
668-
}
669-
1 => match s[0] {
670-
b'/' => {
671-
return Ok(Uri {
672-
scheme: Scheme::empty(),
673-
authority: Authority::empty(),
674-
path_and_query: PathAndQuery::slash(),
675-
});
676-
}
677-
b'*' => {
678-
return Ok(Uri {
679-
scheme: Scheme::empty(),
680-
authority: Authority::empty(),
681-
path_and_query: PathAndQuery::star(),
682-
});
683-
}
684-
_ => {
685-
let authority = Authority::from_shared(s)?;
686-
687-
return Ok(Uri {
688-
scheme: Scheme::empty(),
689-
authority: authority,
690-
path_and_query: PathAndQuery::empty(),
691-
});
692-
}
693-
},
694-
_ => {}
695-
}
696-
697-
if s[0] == b'/' {
698-
return Ok(Uri {
699-
scheme: Scheme::empty(),
700-
authority: Authority::empty(),
701-
path_and_query: PathAndQuery::from_shared(s)?,
702-
});
703-
}
704-
705-
parse_full(s)
706-
}
707-
}
708-
709681
impl<'a> TryFrom<&'a str> for Uri {
710682
type Error = InvalidUri;
711683

src/uri/path.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl PathAndQuery {
3939
/// assert_eq!(path_and_query.query(), Some("world"));
4040
/// # }
4141
/// ```
42-
pub fn from_shared(mut src: Bytes) -> Result<Self, InvalidUriBytes> {
42+
pub(super) fn from_shared(mut src: Bytes) -> Result<Self, InvalidUriBytes> {
4343
let mut query = NONE;
4444
let mut fragment = None;
4545

@@ -275,20 +275,6 @@ impl PathAndQuery {
275275
}
276276
ret
277277
}
278-
279-
/// Converts this `PathAndQuery` back to a sequence of bytes
280-
#[inline]
281-
pub fn into_bytes(self) -> Bytes {
282-
self.into()
283-
}
284-
}
285-
286-
impl TryFrom<Bytes> for PathAndQuery {
287-
type Error = InvalidUriBytes;
288-
#[inline]
289-
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
290-
PathAndQuery::from_shared(bytes)
291-
}
292278
}
293279

294280
impl<'a> TryFrom<&'a [u8]> for PathAndQuery {
@@ -315,12 +301,6 @@ impl FromStr for PathAndQuery {
315301
}
316302
}
317303

318-
impl From<PathAndQuery> for Bytes {
319-
fn from(src: PathAndQuery) -> Bytes {
320-
src.data.into()
321-
}
322-
}
323-
324304
impl fmt::Debug for PathAndQuery {
325305
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
326306
fmt::Display::fmt(self, f)

0 commit comments

Comments
 (0)