Skip to content

Commit

Permalink
feat: change to TryInto bounds for Builder Methods (#730)
Browse files Browse the repository at this point in the history
This makes requests::Builder trait bound easily readable and consistent w/ stdlib recommendations. It also technically _increases_ the amount of types that could meet the bounds, because of how TryFrom/TryInto interact.

Closes #727 

Co-authored-by: rob <mdnlss@outlook.com>
  • Loading branch information
SmolPatches and rob authored Nov 28, 2024
1 parent 300f91d commit 5a1a5e8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
62 changes: 31 additions & 31 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
//! ```
use std::any::Any;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::fmt;

use crate::header::{HeaderMap, HeaderName, HeaderValue};
Expand Down Expand Up @@ -231,8 +231,8 @@ impl Request<()> {
/// ```
pub fn get<T>(uri: T) -> Builder
where
Uri: TryFrom<T>,
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Uri>,
<T as TryInto<Uri>>::Error: Into<crate::Error>,
{
Builder::new().method(Method::GET).uri(uri)
}
Expand All @@ -253,8 +253,8 @@ impl Request<()> {
/// ```
pub fn put<T>(uri: T) -> Builder
where
Uri: TryFrom<T>,
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Uri>,
<T as TryInto<Uri>>::Error: Into<crate::Error>,
{
Builder::new().method(Method::PUT).uri(uri)
}
Expand All @@ -275,8 +275,8 @@ impl Request<()> {
/// ```
pub fn post<T>(uri: T) -> Builder
where
Uri: TryFrom<T>,
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Uri>,
<T as TryInto<Uri>>::Error: Into<crate::Error>,
{
Builder::new().method(Method::POST).uri(uri)
}
Expand All @@ -297,8 +297,8 @@ impl Request<()> {
/// ```
pub fn delete<T>(uri: T) -> Builder
where
Uri: TryFrom<T>,
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Uri>,
<T as TryInto<Uri>>::Error: Into<crate::Error>,
{
Builder::new().method(Method::DELETE).uri(uri)
}
Expand All @@ -320,8 +320,8 @@ impl Request<()> {
/// ```
pub fn options<T>(uri: T) -> Builder
where
Uri: TryFrom<T>,
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Uri>,
<T as TryInto<Uri>>::Error: Into<crate::Error>,
{
Builder::new().method(Method::OPTIONS).uri(uri)
}
Expand All @@ -342,8 +342,8 @@ impl Request<()> {
/// ```
pub fn head<T>(uri: T) -> Builder
where
Uri: TryFrom<T>,
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Uri>,
<T as TryInto<Uri>>::Error: Into<crate::Error>,
{
Builder::new().method(Method::HEAD).uri(uri)
}
Expand All @@ -364,8 +364,8 @@ impl Request<()> {
/// ```
pub fn connect<T>(uri: T) -> Builder
where
Uri: TryFrom<T>,
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Uri>,
<T as TryInto<Uri>>::Error: Into<crate::Error>,
{
Builder::new().method(Method::CONNECT).uri(uri)
}
Expand All @@ -386,8 +386,8 @@ impl Request<()> {
/// ```
pub fn patch<T>(uri: T) -> Builder
where
Uri: TryFrom<T>,
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Uri>,
<T as TryInto<Uri>>::Error: Into<crate::Error>,
{
Builder::new().method(Method::PATCH).uri(uri)
}
Expand All @@ -408,8 +408,8 @@ impl Request<()> {
/// ```
pub fn trace<T>(uri: T) -> Builder
where
Uri: TryFrom<T>,
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Uri>,
<T as TryInto<Uri>>::Error: Into<crate::Error>,
{
Builder::new().method(Method::TRACE).uri(uri)
}
Expand Down Expand Up @@ -767,11 +767,11 @@ impl Builder {
/// ```
pub fn method<T>(self, method: T) -> Builder
where
Method: TryFrom<T>,
<Method as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Method>,
<T as TryInto<Method>>::Error: Into<crate::Error>,
{
self.and_then(move |mut head| {
let method = TryFrom::try_from(method).map_err(Into::into)?;
let method = method.try_into().map_err(Into::into)?;
head.method = method;
Ok(head)
})
Expand Down Expand Up @@ -812,11 +812,11 @@ impl Builder {
/// ```
pub fn uri<T>(self, uri: T) -> Builder
where
Uri: TryFrom<T>,
<Uri as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Uri>,
<T as TryInto<Uri>>::Error: Into<crate::Error>,
{
self.and_then(move |mut head| {
head.uri = TryFrom::try_from(uri).map_err(Into::into)?;
head.uri = uri.try_into().map_err(Into::into)?;
Ok(head)
})
}
Expand Down Expand Up @@ -900,14 +900,14 @@ impl Builder {
/// ```
pub fn header<K, V>(self, key: K, value: V) -> Builder
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<crate::Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<crate::Error>,
K: TryInto<HeaderName>,
<K as TryInto<HeaderName>>::Error: Into<crate::Error>,
V: TryInto<HeaderValue>,
<V as TryInto<HeaderValue>>::Error: Into<crate::Error>,
{
self.and_then(move |mut head| {
let name = <HeaderName as TryFrom<K>>::try_from(key).map_err(Into::into)?;
let value = <HeaderValue as TryFrom<V>>::try_from(value).map_err(Into::into)?;
let name = key.try_into().map_err(Into::into)?;
let value = value.try_into().map_err(Into::into)?;
head.headers.try_append(name, value)?;
Ok(head)
})
Expand Down
20 changes: 10 additions & 10 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
//! ```
use std::any::Any;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::fmt;

use crate::header::{HeaderMap, HeaderName, HeaderValue};
Expand Down Expand Up @@ -559,11 +559,11 @@ impl Builder {
/// ```
pub fn status<T>(self, status: T) -> Builder
where
StatusCode: TryFrom<T>,
<StatusCode as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<StatusCode>,
<T as TryInto<StatusCode>>::Error: Into<crate::Error>,
{
self.and_then(move |mut head| {
head.status = TryFrom::try_from(status).map_err(Into::into)?;
head.status = status.try_into().map_err(Into::into)?;
Ok(head)
})
}
Expand Down Expand Up @@ -610,14 +610,14 @@ impl Builder {
/// ```
pub fn header<K, V>(self, key: K, value: V) -> Builder
where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<crate::Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<crate::Error>,
K: TryInto<HeaderName>,
<K as TryInto<HeaderName>>::Error: Into<crate::Error>,
V: TryInto<HeaderValue>,
<V as TryInto<HeaderValue>>::Error: Into<crate::Error>,
{
self.and_then(move |mut head| {
let name = <HeaderName as TryFrom<K>>::try_from(key).map_err(Into::into)?;
let value = <HeaderValue as TryFrom<V>>::try_from(value).map_err(Into::into)?;
let name = key.try_into().map_err(Into::into)?;
let value = value.try_into().map_err(Into::into)?;
head.headers.try_append(name, value)?;
Ok(head)
})
Expand Down
14 changes: 7 additions & 7 deletions src/uri/builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::convert::{TryFrom, TryInto};
use std::convert::TryInto;

use super::{Authority, Parts, PathAndQuery, Scheme};
use crate::Uri;
Expand Down Expand Up @@ -44,8 +44,8 @@ impl Builder {
/// ```
pub fn scheme<T>(self, scheme: T) -> Self
where
Scheme: TryFrom<T>,
<Scheme as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Scheme>,
<T as TryInto<Scheme>>::Error: Into<crate::Error>,
{
self.map(move |mut parts| {
let scheme = scheme.try_into().map_err(Into::into)?;
Expand All @@ -68,8 +68,8 @@ impl Builder {
/// ```
pub fn authority<T>(self, auth: T) -> Self
where
Authority: TryFrom<T>,
<Authority as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<Authority>,
<T as TryInto<Authority>>::Error: Into<crate::Error>,
{
self.map(move |mut parts| {
let auth = auth.try_into().map_err(Into::into)?;
Expand All @@ -92,8 +92,8 @@ impl Builder {
/// ```
pub fn path_and_query<T>(self, p_and_q: T) -> Self
where
PathAndQuery: TryFrom<T>,
<PathAndQuery as TryFrom<T>>::Error: Into<crate::Error>,
T: TryInto<PathAndQuery>,
<T as TryInto<PathAndQuery>>::Error: Into<crate::Error>,
{
self.map(move |mut parts| {
let p_and_q = p_and_q.try_into().map_err(Into::into)?;
Expand Down

0 comments on commit 5a1a5e8

Please sign in to comment.