From a23d8edcfb4e3bce349a3d4f8f9084010ebc5e77 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Wed, 14 Aug 2019 14:58:27 -0400 Subject: [PATCH] Switch HttpTryFrom to TryFrom. --- src/convert.rs | 75 -------------------------------------------- src/error.rs | 30 ++---------------- src/header/map.rs | 21 +++++++------ src/header/name.rs | 19 +++-------- src/header/value.rs | 41 ++++-------------------- src/lib.rs | 2 -- src/method.rs | 19 +++-------- src/request.rs | 58 +++++++++++++++++++++++----------- src/response.rs | 18 ++++++----- src/status.rs | 18 +++-------- src/uri/authority.rs | 17 +++------- src/uri/builder.rs | 34 +++++++++++--------- src/uri/mod.rs | 20 +++--------- src/uri/path.rs | 12 +++---- src/uri/scheme.rs | 17 +++------- 15 files changed, 125 insertions(+), 276 deletions(-) delete mode 100644 src/convert.rs diff --git a/src/convert.rs b/src/convert.rs deleted file mode 100644 index 4303743..0000000 --- a/src/convert.rs +++ /dev/null @@ -1,75 +0,0 @@ -use crate::header::{HeaderMap, HeaderName, HeaderValue}; -use crate::method::Method; -use crate::sealed::Sealed; -use crate::status::StatusCode; -use crate::uri::{Authority, PathAndQuery, Scheme, Uri}; -use crate::Error; - -/// Private trait for the `http` crate to have generic methods with fallible -/// conversions. -/// -/// This trait is similar to the `TryFrom` trait proposed in the standard -/// library, except this is specialized for the `http` crate and isn't intended -/// for general consumption. -/// -/// This trait cannot be implemented types outside of the `http` crate, and is -/// only intended for use as a generic bound on methods in the `http` crate. -pub trait HttpTryFrom: Sized + Sealed { - /// Associated error with the conversion this implementation represents. - type Error: Into; - - #[doc(hidden)] - fn try_from(t: T) -> Result; -} - -pub(crate) trait HttpTryInto: Sized { - fn http_try_into(self) -> Result; -} - -#[doc(hidden)] -impl HttpTryInto for T -where - U: HttpTryFrom, - T: Sized, -{ - fn http_try_into(self) -> Result { - HttpTryFrom::try_from(self).map_err(|e: U::Error| e.into()) - } -} - -macro_rules! reflexive { - ($($t:ty,)*) => ($( - impl HttpTryFrom<$t> for $t { - type Error = Error; - - fn try_from(t: Self) -> Result { - Ok(t) - } - } - - impl Sealed for $t {} - )*) -} - -reflexive! { - Uri, - Method, - StatusCode, - HeaderName, - HeaderValue, - Scheme, - Authority, - PathAndQuery, -} - -// HeaderMap can't use reflexive easily due to the generic T - -impl HttpTryFrom> for HeaderMap { - type Error = Error; - - fn try_from(t: Self) -> Result { - Ok(t) - } -} - -impl Sealed for HeaderMap {} diff --git a/src/error.rs b/src/error.rs index 1dfe282..3d06fbc 100644 --- a/src/error.rs +++ b/src/error.rs @@ -167,33 +167,9 @@ impl From for Error { } } -// A crate-private type until we can use !. -// -// Being crate-private, we should be able to swap the type out in a -// backwards compatible way. -pub enum Never {} - -impl From for Error { - fn from(never: Never) -> Error { - match never {} - } -} - -impl fmt::Debug for Never { - fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self {} - } -} - -impl fmt::Display for Never { - fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self {} - } -} - -impl error::Error for Never { - fn description(&self) -> &str { - match *self {} +impl From for Error { + fn from(err: std::convert::Infallible) -> Error { + match err {} } } diff --git a/src/header/map.rs b/src/header/map.rs index 5dd5ef6..81dee95 100644 --- a/src/header/map.rs +++ b/src/header/map.rs @@ -1,11 +1,11 @@ use std::collections::HashMap; use std::collections::hash_map::RandomState; +use std::convert::TryFrom; use std::hash::{BuildHasher, Hash, Hasher}; use std::iter::{FromIterator, FusedIterator}; use std::marker::PhantomData; use std::{fmt, mem, ops, ptr, vec}; -use crate::convert::{HttpTryFrom, HttpTryInto}; use crate::Error; use super::HeaderValue; @@ -1815,27 +1815,30 @@ impl FromIterator<(HeaderName, T)> for HeaderMap { /// /// ``` /// use std::collections::HashMap; -/// use http::{HttpTryFrom, header::HeaderMap}; +/// use std::convert::TryInto; +/// use http::HeaderMap; /// /// let mut map = HashMap::new(); /// map.insert("X-Custom-Header".to_string(), "my value".to_string()); /// -/// let headers: HeaderMap = HttpTryFrom::try_from(&map).expect("valid headers"); +/// let headers: HeaderMap = (&map).try_into().expect("valid headers"); /// assert_eq!(headers["X-Custom-Header"], "my value"); /// ``` -impl<'a, K, V, T> HttpTryFrom<&'a HashMap> for HeaderMap +impl<'a, K, V, T> TryFrom<&'a HashMap> for HeaderMap where K: Eq + Hash, - HeaderName: HttpTryFrom<&'a K>, - T: HttpTryFrom<&'a V> + HeaderName: TryFrom<&'a K>, + >::Error: Into, + T: TryFrom<&'a V>, + T::Error: Into, { type Error = Error; fn try_from(c: &'a HashMap) -> Result { c.into_iter() - .map(|(k, v)| { - let name = k.http_try_into()?; - let value = v.http_try_into()?; + .map(|(k, v)| -> crate::Result<(HeaderName, T)> { + let name = TryFrom::try_from(k).map_err(Into::into)?; + let value = TryFrom::try_from(v).map_err(Into::into)?; Ok((name, value)) }) .collect() diff --git a/src/header/name.rs b/src/header/name.rs index 01d4361..5cbd822 100644 --- a/src/header/name.rs +++ b/src/header/name.rs @@ -1,9 +1,9 @@ use crate::byte_str::ByteStr; -use crate::HttpTryFrom; use bytes::{Bytes, BytesMut}; use std::borrow::Borrow; use std::error::Error; +use std::convert::{TryFrom}; use std::hash::{Hash, Hasher}; use std::str::FromStr; use std::{fmt, mem}; @@ -1888,16 +1888,7 @@ impl From for Bytes { } } -impl<'a> HttpTryFrom<&'a HeaderName> for HeaderName { - type Error = crate::error::Never; - - #[inline] - fn try_from(t: &'a HeaderName) -> Result { - Ok(t.clone()) - } -} - -impl<'a> HttpTryFrom<&'a str> for HeaderName { +impl<'a> TryFrom<&'a str> for HeaderName { type Error = InvalidHeaderName; #[inline] fn try_from(s: &'a str) -> Result { @@ -1905,7 +1896,7 @@ impl<'a> HttpTryFrom<&'a str> for HeaderName { } } -impl<'a> HttpTryFrom<&'a String> for HeaderName { +impl<'a> TryFrom<&'a String> for HeaderName { type Error = InvalidHeaderName; #[inline] fn try_from(s: &'a String) -> Result { @@ -1913,7 +1904,7 @@ impl<'a> HttpTryFrom<&'a String> for HeaderName { } } -impl<'a> HttpTryFrom<&'a [u8]> for HeaderName { +impl<'a> TryFrom<&'a [u8]> for HeaderName { type Error = InvalidHeaderName; #[inline] fn try_from(s: &'a [u8]) -> Result { @@ -1921,7 +1912,7 @@ impl<'a> HttpTryFrom<&'a [u8]> for HeaderName { } } -impl HttpTryFrom for HeaderName { +impl TryFrom for HeaderName { type Error = InvalidHeaderNameBytes; #[inline] fn try_from(bytes: Bytes) -> Result { diff --git a/src/header/value.rs b/src/header/value.rs index 245947e..8b3c29c 100644 --- a/src/header/value.rs +++ b/src/header/value.rs @@ -1,11 +1,10 @@ use bytes::{Bytes, BytesMut}; +use std::convert::TryFrom; use std::error::Error; use std::str::FromStr; use std::{cmp, fmt, mem, str}; -use crate::convert::HttpTryFrom; -use crate::error::Never; use crate::header::name::HeaderName; /// Represents an HTTP header field value. @@ -397,15 +396,6 @@ macro_rules! from_integers { } } - impl HttpTryFrom<$t> for HeaderValue { - type Error = Never; - - #[inline] - fn try_from(num: $t) -> Result { - Ok(num.into()) - } - } - #[test] fn $name() { let n: $t = 55; @@ -499,16 +489,7 @@ impl<'a> From<&'a HeaderValue> for HeaderValue { } } -impl<'a> HttpTryFrom<&'a HeaderValue> for HeaderValue { - type Error = crate::error::Never; - - #[inline] - fn try_from(t: &'a HeaderValue) -> Result { - Ok(t.clone()) - } -} - -impl<'a> HttpTryFrom<&'a str> for HeaderValue { +impl<'a> TryFrom<&'a str> for HeaderValue { type Error = InvalidHeaderValue; #[inline] @@ -517,7 +498,7 @@ impl<'a> HttpTryFrom<&'a str> for HeaderValue { } } -impl<'a> HttpTryFrom<&'a String> for HeaderValue { +impl<'a> TryFrom<&'a String> for HeaderValue { type Error = InvalidHeaderValue; #[inline] fn try_from(s: &'a String) -> Result { @@ -525,7 +506,7 @@ impl<'a> HttpTryFrom<&'a String> for HeaderValue { } } -impl<'a> HttpTryFrom<&'a [u8]> for HeaderValue { +impl<'a> TryFrom<&'a [u8]> for HeaderValue { type Error = InvalidHeaderValue; #[inline] @@ -534,7 +515,7 @@ impl<'a> HttpTryFrom<&'a [u8]> for HeaderValue { } } -impl HttpTryFrom for HeaderValue { +impl TryFrom for HeaderValue { type Error = InvalidHeaderValueBytes; #[inline] @@ -543,7 +524,7 @@ impl HttpTryFrom for HeaderValue { } } -impl HttpTryFrom for HeaderValue { +impl TryFrom for HeaderValue { type Error = InvalidHeaderValueBytes; #[inline] @@ -552,16 +533,6 @@ impl HttpTryFrom for HeaderValue { } } -impl HttpTryFrom for HeaderValue { - type Error = InvalidHeaderValue; - - #[inline] - fn try_from(name: HeaderName) -> Result { - // Infallable as header names have the same validations - Ok(name.into()) - } -} - #[cfg(test)] mod try_from_header_name_tests { use super::*; diff --git a/src/lib.rs b/src/lib.rs index df5fe4e..c4d202e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -176,11 +176,9 @@ pub mod uri; pub mod version; mod byte_str; -mod convert; mod error; mod extensions; -pub use crate::convert::HttpTryFrom; pub use crate::error::{Error, Result}; pub use crate::extensions::Extensions; #[doc(no_inline)] diff --git a/src/method.rs b/src/method.rs index 626b651..8580a62 100644 --- a/src/method.rs +++ b/src/method.rs @@ -16,11 +16,11 @@ //! ``` use self::Inner::*; -use crate::HttpTryFrom; use std::convert::AsRef; use std::error::Error; use std::str::FromStr; +use std::convert::TryFrom; use std::{fmt, str}; /// The Request Method (VERB) @@ -322,16 +322,7 @@ impl<'a> From<&'a Method> for Method { } } -impl<'a> HttpTryFrom<&'a Method> for Method { - type Error = crate::error::Never; - - #[inline] - fn try_from(t: &'a Method) -> Result { - Ok(t.clone()) - } -} - -impl<'a> HttpTryFrom<&'a [u8]> for Method { +impl<'a> TryFrom<&'a [u8]> for Method { type Error = InvalidMethod; #[inline] @@ -340,12 +331,12 @@ impl<'a> HttpTryFrom<&'a [u8]> for Method { } } -impl<'a> HttpTryFrom<&'a str> for Method { +impl<'a> TryFrom<&'a str> for Method { type Error = InvalidMethod; #[inline] fn try_from(t: &'a str) -> Result { - HttpTryFrom::try_from(t.as_bytes()) + TryFrom::try_from(t.as_bytes()) } } @@ -354,7 +345,7 @@ impl FromStr for Method { #[inline] fn from_str(t: &str) -> Result { - HttpTryFrom::try_from(t) + TryFrom::try_from(t) } } diff --git a/src/request.rs b/src/request.rs index 6b00071..c249fb9 100644 --- a/src/request.rs +++ b/src/request.rs @@ -53,12 +53,13 @@ //! ``` use std::any::Any; +use std::convert::{TryFrom}; use std::fmt; use crate::header::{HeaderMap, HeaderName, HeaderValue}; use crate::method::Method; use crate::version::Version; -use crate::{Extensions, HttpTryFrom, Result, Uri}; +use crate::{Extensions, Result, Uri}; /// Represents an HTTP request. /// @@ -228,7 +229,9 @@ impl Request<()> { /// ``` pub fn get(uri: T) -> Builder where - Uri: HttpTryFrom, + Uri: TryFrom, + >::Error: Into, + { Builder::new().method(Method::GET).uri(uri) } @@ -249,7 +252,9 @@ impl Request<()> { /// ``` pub fn put(uri: T) -> Builder where - Uri: HttpTryFrom, + Uri: TryFrom, + >::Error: Into, + { Builder::new().method(Method::PUT).uri(uri) } @@ -270,7 +275,9 @@ impl Request<()> { /// ``` pub fn post(uri: T) -> Builder where - Uri: HttpTryFrom, + Uri: TryFrom, + >::Error: Into, + { Builder::new().method(Method::POST).uri(uri) } @@ -291,7 +298,9 @@ impl Request<()> { /// ``` pub fn delete(uri: T) -> Builder where - Uri: HttpTryFrom, + Uri: TryFrom, + >::Error: Into, + { Builder::new().method(Method::DELETE).uri(uri) } @@ -313,7 +322,9 @@ impl Request<()> { /// ``` pub fn options(uri: T) -> Builder where - Uri: HttpTryFrom, + Uri: TryFrom, + >::Error: Into, + { Builder::new().method(Method::OPTIONS).uri(uri) } @@ -334,7 +345,9 @@ impl Request<()> { /// ``` pub fn head(uri: T) -> Builder where - Uri: HttpTryFrom, + Uri: TryFrom, + >::Error: Into, + { Builder::new().method(Method::HEAD).uri(uri) } @@ -355,7 +368,9 @@ impl Request<()> { /// ``` pub fn connect(uri: T) -> Builder where - Uri: HttpTryFrom, + Uri: TryFrom, + >::Error: Into, + { Builder::new().method(Method::CONNECT).uri(uri) } @@ -376,7 +391,8 @@ impl Request<()> { /// ``` pub fn patch(uri: T) -> Builder where - Uri: HttpTryFrom, + Uri: TryFrom, + >::Error: Into, { Builder::new().method(Method::PATCH).uri(uri) } @@ -397,7 +413,8 @@ impl Request<()> { /// ``` pub fn trace(uri: T) -> Builder where - Uri: HttpTryFrom, + Uri: TryFrom, + >::Error: Into, { Builder::new().method(Method::TRACE).uri(uri) } @@ -761,10 +778,12 @@ impl Builder { /// ``` pub fn method(self, method: T) -> Builder where - Method: HttpTryFrom, + Method: TryFrom, + >::Error: Into, { self.and_then(move |mut head| { - head.method = HttpTryFrom::try_from(method).map_err(Into::into)?; + let method = TryFrom::try_from(method).map_err(Into::into)?; + head.method = method; Ok(head) }) } @@ -807,10 +826,11 @@ impl Builder { /// ``` pub fn uri(self, uri: T) -> Builder where - Uri: HttpTryFrom, + Uri: TryFrom, + >::Error: Into, { self.and_then(move |mut head| { - head.uri = HttpTryFrom::try_from(uri).map_err(Into::into)?; + head.uri = TryFrom::try_from(uri).map_err(Into::into)?; Ok(head) }) } @@ -878,12 +898,14 @@ impl Builder { /// ``` pub fn header(self, key: K, value: V) -> Builder where - HeaderName: HttpTryFrom, - HeaderValue: HttpTryFrom, + HeaderName: TryFrom, + >::Error: Into, + HeaderValue: TryFrom, + >::Error: Into, { self.and_then(move |mut head| { - let name = >::try_from(key).map_err(Into::into)?; - let value = >::try_from(value).map_err(Into::into)?; + let name = >::try_from(key).map_err(Into::into)?; + let value = >::try_from(value).map_err(Into::into)?; head.headers.append(name, value); Ok(head) }) diff --git a/src/response.rs b/src/response.rs index 7bdbb57..7078b81 100644 --- a/src/response.rs +++ b/src/response.rs @@ -62,12 +62,13 @@ //! ``` use std::any::Any; +use std::convert::TryFrom; use std::fmt; use crate::header::{HeaderMap, HeaderName, HeaderValue}; use crate::status::StatusCode; use crate::version::Version; -use crate::{Extensions, HttpTryFrom, Result}; +use crate::{Extensions, Result}; /// Represents an HTTP response /// @@ -562,10 +563,11 @@ impl Builder { /// ``` pub fn status(self, status: T) -> Builder where - StatusCode: HttpTryFrom, + StatusCode: TryFrom, + >::Error: Into, { self.and_then(move |mut head| { - head.status = HttpTryFrom::try_from(status).map_err(Into::into)?; + head.status = TryFrom::try_from(status).map_err(Into::into)?; Ok(head) }) } @@ -615,12 +617,14 @@ impl Builder { /// ``` pub fn header(self, key: K, value: V) -> Builder where - HeaderName: HttpTryFrom, - HeaderValue: HttpTryFrom, + HeaderName: TryFrom, + >::Error: Into, + HeaderValue: TryFrom, + >::Error: Into, { self.and_then(move |mut head| { - let name = >::try_from(key).map_err(Into::into)?; - let value = >::try_from(value).map_err(Into::into)?; + let name = >::try_from(key).map_err(Into::into)?; + let value = >::try_from(value).map_err(Into::into)?; head.headers.append(name, value); Ok(head) }) diff --git a/src/status.rs b/src/status.rs index 91d18ef..5b4c41f 100644 --- a/src/status.rs +++ b/src/status.rs @@ -14,12 +14,11 @@ //! assert!(StatusCode::OK.is_success()); //! ``` +use std::convert::TryFrom; use std::error::Error; use std::fmt; use std::str::FromStr; -use crate::HttpTryFrom; - /// An HTTP status code (`status-code` in RFC 7230 et al.). /// /// This type contains constants for all common status codes. @@ -251,16 +250,7 @@ impl<'a> From<&'a StatusCode> for StatusCode { } } -impl<'a> HttpTryFrom<&'a StatusCode> for StatusCode { - type Error = crate::error::Never; - - #[inline] - fn try_from(t: &'a StatusCode) -> Result { - Ok(t.clone()) - } -} - -impl<'a> HttpTryFrom<&'a [u8]> for StatusCode { +impl<'a> TryFrom<&'a [u8]> for StatusCode { type Error = InvalidStatusCode; #[inline] @@ -269,7 +259,7 @@ impl<'a> HttpTryFrom<&'a [u8]> for StatusCode { } } -impl<'a> HttpTryFrom<&'a str> for StatusCode { +impl<'a> TryFrom<&'a str> for StatusCode { type Error = InvalidStatusCode; #[inline] @@ -278,7 +268,7 @@ impl<'a> HttpTryFrom<&'a str> for StatusCode { } } -impl HttpTryFrom for StatusCode { +impl TryFrom for StatusCode { type Error = InvalidStatusCode; #[inline] diff --git a/src/uri/authority.rs b/src/uri/authority.rs index f086528..9ec54d1 100644 --- a/src/uri/authority.rs +++ b/src/uri/authority.rs @@ -10,7 +10,6 @@ use bytes::Bytes; use super::{ErrorKind, InvalidUri, InvalidUriBytes, Port, URI_CHARS}; use crate::byte_str::ByteStr; -use crate::convert::HttpTryFrom; /// Represents the authority component of a URI. #[derive(Clone)] @@ -463,15 +462,7 @@ impl Hash for Authority { } } -impl HttpTryFrom for Authority { - type Error = InvalidUriBytes; - #[inline] - fn try_from(bytes: Bytes) -> Result { - Authority::from_shared(bytes) - } -} - -impl<'a> HttpTryFrom<&'a [u8]> for Authority { +impl<'a> TryFrom<&'a [u8]> for Authority { type Error = InvalidUri; #[inline] fn try_from(s: &'a [u8]) -> Result { @@ -488,11 +479,11 @@ impl<'a> HttpTryFrom<&'a [u8]> for Authority { } } -impl<'a> HttpTryFrom<&'a str> for Authority { +impl<'a> TryFrom<&'a str> for Authority { type Error = InvalidUri; #[inline] fn try_from(s: &'a str) -> Result { - HttpTryFrom::try_from(s.as_bytes()) + TryFrom::try_from(s.as_bytes()) } } @@ -500,7 +491,7 @@ impl FromStr for Authority { type Err = InvalidUri; fn from_str(s: &str) -> Result { - HttpTryFrom::try_from(s) + TryFrom::try_from(s) } } diff --git a/src/uri/builder.rs b/src/uri/builder.rs index f980964..56a061a 100644 --- a/src/uri/builder.rs +++ b/src/uri/builder.rs @@ -1,6 +1,7 @@ +use std::convert::{TryFrom, TryInto}; + use super::{Authority, Parts, PathAndQuery, Scheme}; -use crate::convert::{HttpTryFrom, HttpTryInto}; -use crate::{Result, Uri}; +use crate::Uri; /// A builder for `Uri`s. /// @@ -8,7 +9,7 @@ use crate::{Result, Uri}; /// through a builder pattern. #[derive(Debug)] pub struct Builder { - parts: Result, + parts: Result, } impl Builder { @@ -43,10 +44,12 @@ impl Builder { /// ``` pub fn scheme(self, scheme: T) -> Self where - Scheme: HttpTryFrom, + Scheme: TryFrom, + >::Error: Into, { self.map(move |mut parts| { - parts.scheme = Some(scheme.http_try_into()?); + let scheme = scheme.try_into().map_err(Into::into)?; + parts.scheme = Some(scheme); Ok(parts) }) } @@ -65,10 +68,12 @@ impl Builder { /// ``` pub fn authority(self, auth: T) -> Self where - Authority: HttpTryFrom, + Authority: TryFrom, + >::Error: Into, { self.map(move |mut parts| { - parts.authority = Some(auth.http_try_into()?); + let auth = auth.try_into().map_err(Into::into)?; + parts.authority = Some(auth); Ok(parts) }) } @@ -87,10 +92,12 @@ impl Builder { /// ``` pub fn path_and_query(self, p_and_q: T) -> Self where - PathAndQuery: HttpTryFrom, + PathAndQuery: TryFrom, + >::Error: Into, { self.map(move |mut parts| { - parts.path_and_query = Some(p_and_q.http_try_into()?); + let p_and_q = p_and_q.try_into().map_err(Into::into)?; + parts.path_and_query = Some(p_and_q); Ok(parts) }) } @@ -119,17 +126,16 @@ impl Builder { /// .build() /// .unwrap(); /// ``` - pub fn build(self) -> Result { - self - .parts - .and_then(|parts| parts.http_try_into()) + pub fn build(self) -> Result { + let parts = self.parts?; + Uri::from_parts(parts).map_err(Into::into) } // private fn map(self, func: F) -> Self where - F: FnOnce(Parts) -> Result, + F: FnOnce(Parts) -> Result, { Builder { diff --git a/src/uri/mod.rs b/src/uri/mod.rs index d4625fb..26b10dd 100644 --- a/src/uri/mod.rs +++ b/src/uri/mod.rs @@ -23,7 +23,6 @@ //! ``` use crate::byte_str::ByteStr; -use crate::HttpTryFrom; use std::convert::TryFrom; use bytes::Bytes; @@ -707,7 +706,7 @@ impl TryFrom for Uri { } } -impl<'a> HttpTryFrom<&'a str> for Uri { +impl<'a> TryFrom<&'a str> for Uri { type Error = InvalidUri; #[inline] @@ -716,7 +715,7 @@ impl<'a> HttpTryFrom<&'a str> for Uri { } } -impl<'a> HttpTryFrom<&'a String> for Uri { +impl<'a> TryFrom<&'a String> for Uri { type Error = InvalidUri; #[inline] @@ -725,7 +724,7 @@ impl<'a> HttpTryFrom<&'a String> for Uri { } } -impl HttpTryFrom for Uri { +impl TryFrom for Uri { type Error = InvalidUriBytes; #[inline] @@ -734,16 +733,7 @@ impl HttpTryFrom for Uri { } } -impl HttpTryFrom for Uri { - type Error = InvalidUriBytes; - - #[inline] - fn try_from(t: Bytes) -> Result { - Uri::from_shared(t) - } -} - -impl HttpTryFrom for Uri { +impl TryFrom for Uri { type Error = InvalidUriParts; #[inline] @@ -752,7 +742,7 @@ impl HttpTryFrom for Uri { } } -impl<'a> HttpTryFrom<&'a Uri> for Uri { +impl<'a> TryFrom<&'a Uri> for Uri { type Error = crate::Error; #[inline] diff --git a/src/uri/path.rs b/src/uri/path.rs index 6cd7342..6981be8 100644 --- a/src/uri/path.rs +++ b/src/uri/path.rs @@ -1,3 +1,4 @@ +use std::convert::TryFrom; use std::str::FromStr; use std::{cmp, fmt, str}; @@ -5,7 +6,6 @@ use bytes::Bytes; use super::{ErrorKind, InvalidUri, InvalidUriBytes}; use crate::byte_str::ByteStr; -use crate::convert::HttpTryFrom; /// Represents the path component of a URI #[derive(Clone)] @@ -283,7 +283,7 @@ impl PathAndQuery { } } -impl HttpTryFrom for PathAndQuery { +impl TryFrom for PathAndQuery { type Error = InvalidUriBytes; #[inline] fn try_from(bytes: Bytes) -> Result { @@ -291,7 +291,7 @@ impl HttpTryFrom for PathAndQuery { } } -impl<'a> HttpTryFrom<&'a [u8]> for PathAndQuery { +impl<'a> TryFrom<&'a [u8]> for PathAndQuery { type Error = InvalidUri; #[inline] fn try_from(s: &'a [u8]) -> Result { @@ -299,11 +299,11 @@ impl<'a> HttpTryFrom<&'a [u8]> for PathAndQuery { } } -impl<'a> HttpTryFrom<&'a str> for PathAndQuery { +impl<'a> TryFrom<&'a str> for PathAndQuery { type Error = InvalidUri; #[inline] fn try_from(s: &'a str) -> Result { - HttpTryFrom::try_from(s.as_bytes()) + TryFrom::try_from(s.as_bytes()) } } @@ -311,7 +311,7 @@ impl FromStr for PathAndQuery { type Err = InvalidUri; #[inline] fn from_str(s: &str) -> Result { - HttpTryFrom::try_from(s) + TryFrom::try_from(s) } } diff --git a/src/uri/scheme.rs b/src/uri/scheme.rs index 44f8880..8b48182 100644 --- a/src/uri/scheme.rs +++ b/src/uri/scheme.rs @@ -10,7 +10,6 @@ use bytes::Bytes; use super::{ErrorKind, InvalidUri, InvalidUriBytes}; use crate::byte_str::ByteStr; -use crate::convert::HttpTryFrom; /// Represents the scheme component of a URI #[derive(Clone)] @@ -101,14 +100,6 @@ impl Scheme { } } -impl HttpTryFrom for Scheme { - type Error = InvalidUriBytes; - #[inline] - fn try_from(bytes: Bytes) -> Result { - Scheme::from_shared(bytes) - } -} - impl TryFrom for Scheme { type Error = InvalidUriBytes; @@ -145,7 +136,7 @@ impl TryFrom for Scheme { } } -impl<'a> HttpTryFrom<&'a [u8]> for Scheme { +impl<'a> TryFrom<&'a [u8]> for Scheme { type Error = InvalidUri; #[inline] fn try_from(s: &'a [u8]) -> Result { @@ -162,11 +153,11 @@ impl<'a> HttpTryFrom<&'a [u8]> for Scheme { } } -impl<'a> HttpTryFrom<&'a str> for Scheme { +impl<'a> TryFrom<&'a str> for Scheme { type Error = InvalidUri; #[inline] fn try_from(s: &'a str) -> Result { - HttpTryFrom::try_from(s.as_bytes()) + TryFrom::try_from(s.as_bytes()) } } @@ -174,7 +165,7 @@ impl FromStr for Scheme { type Err = InvalidUri; fn from_str(s: &str) -> Result { - HttpTryFrom::try_from(s) + TryFrom::try_from(s) } }