Make all usage of Bytes private

Removes `from_shared` and `try_from` constructors for all types.
This commit is contained in:
Sean McArthur
2019-11-27 17:31:45 -08:00
parent 0421354979
commit bcccb669a4
6 changed files with 72 additions and 229 deletions
+6 -17
View File
@@ -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);
@@ -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 {
@@ -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]
@@ -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 {
+4 -18
View File
@@ -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
@@ -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() {
@@ -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,
}
}
@@ -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 {
@@ -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::*;
+12 -15
View File
@@ -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.
@@ -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`.
@@ -298,6 +301,7 @@ impl TryFrom<Bytes> for Authority {
})
}
}
*/
impl AsRef<str> for Authority {
fn as_ref(&self) -> &str {
@@ -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())
+48 -76
View File
@@ -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.
@@ -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;
+1 -21
View File
@@ -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;
@@ -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 {
@@ -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)
+1 -82
View File
@@ -5,7 +5,7 @@ use std::str::FromStr;
use bytes::Bytes;
use super::{ErrorKind, InvalidUri, InvalidUriBytes};
use super::{ErrorKind, InvalidUri};
use crate::byte_str::ByteStr;
/// Represents the scheme component of a URI
@@ -38,30 +38,6 @@ impl Scheme {
inner: Scheme2::Standard(Protocol::Https),
};
/// Attempt to convert a `Scheme` from `Bytes`
///
/// This function has been replaced by `TryFrom` implementation
///
/// # Examples
///
/// ```
/// # extern crate http;
/// # use http::uri::*;
/// extern crate bytes;
///
/// use bytes::Bytes;
///
/// # pub fn main() {
/// let bytes = Bytes::from("http");
/// let scheme = Scheme::from_shared(bytes).unwrap();
///
/// assert_eq!(scheme.as_str(), "http");
/// # }
/// ```
pub fn from_shared(s: Bytes) -> Result<Self, InvalidUriBytes> {
TryFrom::try_from(s)
}
pub(super) fn empty() -> Self {
Scheme {
inner: Scheme2::None,
@@ -89,48 +65,6 @@ impl Scheme {
None => unreachable!(),
}
}
/// Converts this `Scheme` back to a sequence of bytes
#[inline]
pub fn into_bytes(self) -> Bytes {
self.into()
}
}
impl TryFrom<Bytes> for Scheme {
type Error = InvalidUriBytes;
/// Attempt to convert a `Scheme` 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");
/// let scheme = Scheme::try_from(bytes).unwrap();
///
/// assert_eq!(scheme.as_str(), "http");
/// # }
/// ```
fn try_from(s: Bytes) -> Result<Self, Self::Error> {
use self::Scheme2::*;
match Scheme2::parse_exact(&s[..]).map_err(InvalidUriBytes)? {
None => Err(ErrorKind::InvalidScheme.into()),
Standard(p) => Ok(Standard(p).into()),
Other(_) => {
let b = unsafe { ByteStr::from_utf8_unchecked(s) };
Ok(Other(Box::new(b)).into())
}
}
}
}
impl<'a> TryFrom<&'a [u8]> for Scheme {
@@ -168,21 +102,6 @@ impl FromStr for Scheme {
}
}
impl From<Scheme> for Bytes {
#[inline]
fn from(src: Scheme) -> Self {
use self::Protocol::*;
use self::Scheme2::*;
match src.inner {
None => Bytes::new(),
Standard(Http) => Bytes::from_static(b"http"),
Standard(Https) => Bytes::from_static(b"https"),
Other(v) => (*v).into(),
}
}
}
impl fmt::Debug for Scheme {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_str(), f)