Skip to content

Commit

Permalink
Make all usage of Bytes private
Browse files Browse the repository at this point in the history
Removes `from_shared` and `try_from` constructors for all types.
  • Loading branch information
seanmonstar committed Dec 2, 2019
1 parent 0591bba commit 4ce5e6a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 229 deletions.
23 changes: 6 additions & 17 deletions src/header/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ macro_rules! standard_headers {
// Test lower case
let name_bytes = name.as_bytes();
let bytes: Bytes =
HeaderName::from_bytes(name_bytes).unwrap().into();
HeaderName::from_bytes(name_bytes).unwrap().inner.into();
assert_eq!(bytes, name_bytes);
assert_eq!(HeaderName::from_bytes(name_bytes).unwrap(), std);

// Test upper case
let upper = name.to_uppercase().to_string();
let bytes: Bytes =
HeaderName::from_bytes(upper.as_bytes()).unwrap().into();
HeaderName::from_bytes(upper.as_bytes()).unwrap().inner.into();
assert_eq!(bytes, name.as_bytes());
assert_eq!(HeaderName::from_bytes(upper.as_bytes()).unwrap(),
std);
Expand Down Expand Up @@ -1809,6 +1809,10 @@ impl HeaderName {
Repr::Custom(ref v) => &*v.0,
}
}

pub(super) fn into_bytes(self) -> Bytes {
self.inner.into()
}
}

impl FromStr for HeaderName {
Expand Down Expand Up @@ -1881,13 +1885,6 @@ impl From<Custom> for Bytes {
}
}

impl From<HeaderName> for Bytes {
#[inline]
fn from(name: HeaderName) -> Bytes {
name.inner.into()
}
}

impl<'a> TryFrom<&'a str> for HeaderName {
type Error = InvalidHeaderName;
#[inline]
Expand All @@ -1912,14 +1909,6 @@ impl<'a> TryFrom<&'a [u8]> for HeaderName {
}
}

impl TryFrom<Bytes> for HeaderName {
type Error = InvalidHeaderNameBytes;
#[inline]
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
Self::from_bytes(bytes.as_ref()).map_err(InvalidHeaderNameBytes)
}
}

#[doc(hidden)]
impl From<StandardHeader> for HeaderName {
fn from(src: StandardHeader) -> HeaderName {
Expand Down
22 changes: 4 additions & 18 deletions src/header/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,11 @@ impl HeaderValue {
/// This function is intended to be replaced in the future by a `TryFrom`
/// implementation once the trait is stabilized in std.
#[inline]
pub fn from_shared(src: Bytes) -> Result<HeaderValue, InvalidHeaderValueBytes> {
fn from_shared(src: Bytes) -> Result<HeaderValue, InvalidHeaderValueBytes> {
HeaderValue::try_from_generic(src, std::convert::identity).map_err(InvalidHeaderValueBytes)
}

/*
/// Convert a `Bytes` directly into a `HeaderValue` without validating.
///
/// This function does NOT validate that illegal bytes are not contained
Expand All @@ -187,6 +188,7 @@ impl HeaderValue {
}
}
}
*/

fn try_from_generic<T: AsRef<[u8]>, F: FnOnce(T) -> Bytes>(src: T, into: F) -> Result<HeaderValue, InvalidHeaderValue> {
for &b in src.as_ref() {
Expand Down Expand Up @@ -357,7 +359,7 @@ impl From<HeaderName> for HeaderValue {
#[inline]
fn from(h: HeaderName) -> HeaderValue {
HeaderValue {
inner: h.into(),
inner: h.into_bytes(),
is_sensitive: false,
}
}
Expand Down Expand Up @@ -475,13 +477,6 @@ impl FromStr for HeaderValue {
}
}

impl From<HeaderValue> for Bytes {
#[inline]
fn from(value: HeaderValue) -> Bytes {
value.inner
}
}

impl<'a> From<&'a HeaderValue> for HeaderValue {
#[inline]
fn from(t: &'a HeaderValue) -> Self {
Expand Down Expand Up @@ -533,15 +528,6 @@ impl TryFrom<Vec<u8>> for HeaderValue {
}
}

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

#[inline]
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
HeaderValue::from_shared(bytes)
}
}

#[cfg(test)]
mod try_from_header_name_tests {
use super::*;
Expand Down
27 changes: 12 additions & 15 deletions src/uri/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,16 @@ impl Authority {
/// assert_eq!(authority.host(), "example.com");
/// # }
/// ```
pub fn from_shared(s: Bytes) -> Result<Self, InvalidUriBytes> {
TryFrom::try_from(s)
pub(super) fn from_shared(s: Bytes) -> Result<Self, InvalidUriBytes> {
let authority_end = Authority::parse_non_empty(&s[..]).map_err(InvalidUriBytes)?;

if authority_end != s.len() {
return Err(ErrorKind::InvalidUriChar.into());
}

Ok(Authority {
data: unsafe { ByteStr::from_utf8_unchecked(s) },
})
}

/// Attempt to convert an `Authority` from a static string.
Expand Down Expand Up @@ -257,14 +265,9 @@ impl Authority {
pub fn as_str(&self) -> &str {
&self.data[..]
}

/// Converts this `Authority` back to a sequence of bytes
#[inline]
pub fn into_bytes(self) -> Bytes {
self.into()
}
}

/*
impl TryFrom<Bytes> for Authority {
type Error = InvalidUriBytes;
/// Attempt to convert an `Authority` from `Bytes`.
Expand Down Expand Up @@ -298,6 +301,7 @@ impl TryFrom<Bytes> for Authority {
})
}
}
*/

impl AsRef<str> for Authority {
fn as_ref(&self) -> &str {
Expand Down Expand Up @@ -494,13 +498,6 @@ impl FromStr for Authority {
}
}

impl From<Authority> for Bytes {
#[inline]
fn from(src: Authority) -> Bytes {
src.data.into()
}
}

impl fmt::Debug for Authority {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
Expand Down
124 changes: 48 additions & 76 deletions src/uri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,54 @@ impl Uri {
/// assert_eq!(uri.path(), "/foo");
/// # }
/// ```
pub fn from_shared(s: Bytes) -> Result<Uri, InvalidUriBytes> {
TryFrom::try_from(s)
fn from_shared(s: Bytes) -> Result<Uri, InvalidUriBytes> {
use self::ErrorKind::*;

if s.len() > MAX_LEN {
return Err(TooLong.into());
}

match s.len() {
0 => {
return Err(Empty.into());
}
1 => match s[0] {
b'/' => {
return Ok(Uri {
scheme: Scheme::empty(),
authority: Authority::empty(),
path_and_query: PathAndQuery::slash(),
});
}
b'*' => {
return Ok(Uri {
scheme: Scheme::empty(),
authority: Authority::empty(),
path_and_query: PathAndQuery::star(),
});
}
_ => {
let authority = Authority::from_shared(s)?;

return Ok(Uri {
scheme: Scheme::empty(),
authority: authority,
path_and_query: PathAndQuery::empty(),
});
}
},
_ => {}
}

if s[0] == b'/' {
return Ok(Uri {
scheme: Scheme::empty(),
authority: Authority::empty(),
path_and_query: PathAndQuery::from_shared(s)?,
});
}

parse_full(s)
}

/// Convert a `Uri` from a static string.
Expand Down Expand Up @@ -632,80 +678,6 @@ impl Uri {
}
}

impl TryFrom<Bytes> for Uri {
type Error = InvalidUriBytes;

/// Attempt to convert a `Uri` from `Bytes`
///
/// # Examples
///
/// ```
/// # extern crate http;
/// # use http::uri::*;
/// extern crate bytes;
///
/// use std::convert::TryFrom;
/// use bytes::Bytes;
///
/// # pub fn main() {
/// let bytes = Bytes::from("http://example.com/foo");
/// let uri = Uri::try_from(bytes).unwrap();
///
/// assert_eq!(uri.host().unwrap(), "example.com");
/// assert_eq!(uri.path(), "/foo");
/// # }
/// ```
fn try_from(s: Bytes) -> Result<Uri, Self::Error> {
use self::ErrorKind::*;

if s.len() > MAX_LEN {
return Err(TooLong.into());
}

match s.len() {
0 => {
return Err(Empty.into());
}
1 => match s[0] {
b'/' => {
return Ok(Uri {
scheme: Scheme::empty(),
authority: Authority::empty(),
path_and_query: PathAndQuery::slash(),
});
}
b'*' => {
return Ok(Uri {
scheme: Scheme::empty(),
authority: Authority::empty(),
path_and_query: PathAndQuery::star(),
});
}
_ => {
let authority = Authority::from_shared(s)?;

return Ok(Uri {
scheme: Scheme::empty(),
authority: authority,
path_and_query: PathAndQuery::empty(),
});
}
},
_ => {}
}

if s[0] == b'/' {
return Ok(Uri {
scheme: Scheme::empty(),
authority: Authority::empty(),
path_and_query: PathAndQuery::from_shared(s)?,
});
}

parse_full(s)
}
}

impl<'a> TryFrom<&'a str> for Uri {
type Error = InvalidUri;

Expand Down
22 changes: 1 addition & 21 deletions src/uri/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl PathAndQuery {
/// assert_eq!(path_and_query.query(), Some("world"));
/// # }
/// ```
pub fn from_shared(mut src: Bytes) -> Result<Self, InvalidUriBytes> {
pub(super) fn from_shared(mut src: Bytes) -> Result<Self, InvalidUriBytes> {
let mut query = NONE;
let mut fragment = None;

Expand Down Expand Up @@ -275,20 +275,6 @@ impl PathAndQuery {
}
ret
}

/// Converts this `PathAndQuery` back to a sequence of bytes
#[inline]
pub fn into_bytes(self) -> Bytes {
self.into()
}
}

impl TryFrom<Bytes> for PathAndQuery {
type Error = InvalidUriBytes;
#[inline]
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
PathAndQuery::from_shared(bytes)
}
}

impl<'a> TryFrom<&'a [u8]> for PathAndQuery {
Expand All @@ -315,12 +301,6 @@ impl FromStr for PathAndQuery {
}
}

impl From<PathAndQuery> for Bytes {
fn from(src: PathAndQuery) -> Bytes {
src.data.into()
}
}

impl fmt::Debug for PathAndQuery {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
Expand Down
Loading

0 comments on commit 4ce5e6a

Please sign in to comment.