From 5a1a5e8ee54fc2e2f274faf318d9a8b1129c386a Mon Sep 17 00:00:00 2001 From: Robert <102612257+SmolPatches@users.noreply.github.com> Date: Thu, 28 Nov 2024 10:30:29 -0500 Subject: [PATCH] feat: change to TryInto bounds for Builder Methods (#730) 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 --- src/request.rs | 62 +++++++++++++++++++++++----------------------- src/response.rs | 20 +++++++-------- src/uri/builder.rs | 14 +++++------ 3 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/request.rs b/src/request.rs index d4c5bf54..324b676c 100644 --- a/src/request.rs +++ b/src/request.rs @@ -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}; @@ -231,8 +231,8 @@ impl Request<()> { /// ``` pub fn get(uri: T) -> Builder where - Uri: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { Builder::new().method(Method::GET).uri(uri) } @@ -253,8 +253,8 @@ impl Request<()> { /// ``` pub fn put(uri: T) -> Builder where - Uri: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { Builder::new().method(Method::PUT).uri(uri) } @@ -275,8 +275,8 @@ impl Request<()> { /// ``` pub fn post(uri: T) -> Builder where - Uri: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { Builder::new().method(Method::POST).uri(uri) } @@ -297,8 +297,8 @@ impl Request<()> { /// ``` pub fn delete(uri: T) -> Builder where - Uri: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { Builder::new().method(Method::DELETE).uri(uri) } @@ -320,8 +320,8 @@ impl Request<()> { /// ``` pub fn options(uri: T) -> Builder where - Uri: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { Builder::new().method(Method::OPTIONS).uri(uri) } @@ -342,8 +342,8 @@ impl Request<()> { /// ``` pub fn head(uri: T) -> Builder where - Uri: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { Builder::new().method(Method::HEAD).uri(uri) } @@ -364,8 +364,8 @@ impl Request<()> { /// ``` pub fn connect(uri: T) -> Builder where - Uri: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { Builder::new().method(Method::CONNECT).uri(uri) } @@ -386,8 +386,8 @@ impl Request<()> { /// ``` pub fn patch(uri: T) -> Builder where - Uri: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { Builder::new().method(Method::PATCH).uri(uri) } @@ -408,8 +408,8 @@ impl Request<()> { /// ``` pub fn trace(uri: T) -> Builder where - Uri: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { Builder::new().method(Method::TRACE).uri(uri) } @@ -767,11 +767,11 @@ impl Builder { /// ``` pub fn method(self, method: T) -> Builder where - Method: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { 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) }) @@ -812,11 +812,11 @@ impl Builder { /// ``` pub fn uri(self, uri: T) -> Builder where - Uri: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { 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) }) } @@ -900,14 +900,14 @@ impl Builder { /// ``` pub fn header(self, key: K, value: V) -> Builder where - HeaderName: TryFrom, - >::Error: Into, - HeaderValue: TryFrom, - >::Error: Into, + K: TryInto, + >::Error: Into, + V: TryInto, + >::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 = key.try_into().map_err(Into::into)?; + let value = value.try_into().map_err(Into::into)?; head.headers.try_append(name, value)?; Ok(head) }) diff --git a/src/response.rs b/src/response.rs index 312cc5f8..ab9e49bc 100644 --- a/src/response.rs +++ b/src/response.rs @@ -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}; @@ -559,11 +559,11 @@ impl Builder { /// ``` pub fn status(self, status: T) -> Builder where - StatusCode: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { 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) }) } @@ -610,14 +610,14 @@ impl Builder { /// ``` pub fn header(self, key: K, value: V) -> Builder where - HeaderName: TryFrom, - >::Error: Into, - HeaderValue: TryFrom, - >::Error: Into, + K: TryInto, + >::Error: Into, + V: TryInto, + >::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 = key.try_into().map_err(Into::into)?; + let value = value.try_into().map_err(Into::into)?; head.headers.try_append(name, value)?; Ok(head) }) diff --git a/src/uri/builder.rs b/src/uri/builder.rs index 9964d389..d5f7f49b 100644 --- a/src/uri/builder.rs +++ b/src/uri/builder.rs @@ -1,4 +1,4 @@ -use std::convert::{TryFrom, TryInto}; +use std::convert::TryInto; use super::{Authority, Parts, PathAndQuery, Scheme}; use crate::Uri; @@ -44,8 +44,8 @@ impl Builder { /// ``` pub fn scheme(self, scheme: T) -> Self where - Scheme: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { self.map(move |mut parts| { let scheme = scheme.try_into().map_err(Into::into)?; @@ -68,8 +68,8 @@ impl Builder { /// ``` pub fn authority(self, auth: T) -> Self where - Authority: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { self.map(move |mut parts| { let auth = auth.try_into().map_err(Into::into)?; @@ -92,8 +92,8 @@ impl Builder { /// ``` pub fn path_and_query(self, p_and_q: T) -> Self where - PathAndQuery: TryFrom, - >::Error: Into, + T: TryInto, + >::Error: Into, { self.map(move |mut parts| { let p_and_q = p_and_q.try_into().map_err(Into::into)?;