Skip to content

Commit

Permalink
Switch HttpTryFrom to TryFrom.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Barsky authored and seanmonstar committed Nov 26, 2019
1 parent 9d8058a commit 22448cd
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 276 deletions.
75 changes: 0 additions & 75 deletions src/convert.rs

This file was deleted.

30 changes: 3 additions & 27 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,33 +167,9 @@ impl From<header::InvalidHeaderValueBytes> 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<Never> 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<std::convert::Infallible> for Error {
fn from(err: std::convert::Infallible) -> Error {
match err {}
}
}

Expand Down
21 changes: 12 additions & 9 deletions src/header/map.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -1815,27 +1815,30 @@ impl<T> FromIterator<(HeaderName, T)> for HeaderMap<T> {
///
/// ```
/// 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<K, V>> for HeaderMap<T>
impl<'a, K, V, T> TryFrom<&'a HashMap<K, V>> for HeaderMap<T>
where
K: Eq + Hash,
HeaderName: HttpTryFrom<&'a K>,
T: HttpTryFrom<&'a V>
HeaderName: TryFrom<&'a K>,
<HeaderName as TryFrom<&'a K>>::Error: Into<crate::Error>,
T: TryFrom<&'a V>,
T::Error: Into<crate::Error>,
{
type Error = Error;

fn try_from(c: &'a HashMap<K, V>) -> Result<Self, Self::Error> {
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()
Expand Down
19 changes: 5 additions & 14 deletions src/header/name.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -1888,40 +1888,31 @@ impl From<HeaderName> for Bytes {
}
}

impl<'a> HttpTryFrom<&'a HeaderName> for HeaderName {
type Error = crate::error::Never;

#[inline]
fn try_from(t: &'a HeaderName) -> Result<Self, Self::Error> {
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<Self, Self::Error> {
Self::from_bytes(s.as_bytes())
}
}

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<Self, Self::Error> {
Self::from_bytes(s.as_bytes())
}
}

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<Self, Self::Error> {
Self::from_bytes(s)
}
}

impl HttpTryFrom<Bytes> for HeaderName {
impl TryFrom<Bytes> for HeaderName {
type Error = InvalidHeaderNameBytes;
#[inline]
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
Expand Down
41 changes: 6 additions & 35 deletions src/header/value.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -397,15 +396,6 @@ macro_rules! from_integers {
}
}

impl HttpTryFrom<$t> for HeaderValue {
type Error = Never;

#[inline]
fn try_from(num: $t) -> Result<Self, Self::Error> {
Ok(num.into())
}
}

#[test]
fn $name() {
let n: $t = 55;
Expand Down Expand Up @@ -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<Self, Self::Error> {
Ok(t.clone())
}
}

impl<'a> HttpTryFrom<&'a str> for HeaderValue {
impl<'a> TryFrom<&'a str> for HeaderValue {
type Error = InvalidHeaderValue;

#[inline]
Expand All @@ -517,15 +498,15 @@ 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<Self, Self::Error> {
Self::from_bytes(s.as_bytes())
}
}

impl<'a> HttpTryFrom<&'a [u8]> for HeaderValue {
impl<'a> TryFrom<&'a [u8]> for HeaderValue {
type Error = InvalidHeaderValue;

#[inline]
Expand All @@ -534,7 +515,7 @@ impl<'a> HttpTryFrom<&'a [u8]> for HeaderValue {
}
}

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

#[inline]
Expand All @@ -543,7 +524,7 @@ impl HttpTryFrom<String> for HeaderValue {
}
}

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

#[inline]
Expand All @@ -552,16 +533,6 @@ impl HttpTryFrom<Bytes> for HeaderValue {
}
}

impl HttpTryFrom<HeaderName> for HeaderValue {
type Error = InvalidHeaderValue;

#[inline]
fn try_from(name: HeaderName) -> Result<Self, Self::Error> {
// Infallable as header names have the same validations
Ok(name.into())
}
}

#[cfg(test)]
mod try_from_header_name_tests {
use super::*;
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
19 changes: 5 additions & 14 deletions src/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<Self, Self::Error> {
Ok(t.clone())
}
}

impl<'a> HttpTryFrom<&'a [u8]> for Method {
impl<'a> TryFrom<&'a [u8]> for Method {
type Error = InvalidMethod;

#[inline]
Expand All @@ -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<Self, Self::Error> {
HttpTryFrom::try_from(t.as_bytes())
TryFrom::try_from(t.as_bytes())
}
}

Expand All @@ -354,7 +345,7 @@ impl FromStr for Method {

#[inline]
fn from_str(t: &str) -> Result<Self, Self::Err> {
HttpTryFrom::try_from(t)
TryFrom::try_from(t)
}
}

Expand Down
Loading

0 comments on commit 22448cd

Please sign in to comment.