From 9fa58e316d759ac00a14923ebbacde44ed2a5a22 Mon Sep 17 00:00:00 2001 From: Ibraheem Ahmed Date: Mon, 1 Mar 2021 18:50:29 -0500 Subject: [PATCH] Implement IntoUrl for String (#1201) Also change from blanket impl to improve docs. --- src/into_url.rs | 38 +++++++++++++++++++++++++------------- src/proxy.rs | 6 +++--- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/into_url.rs b/src/into_url.rs index f76b73f..ef0db94 100644 --- a/src/into_url.rs +++ b/src/into_url.rs @@ -3,21 +3,23 @@ use url::Url; /// A trait to try to convert some type into a `Url`. /// /// This trait is "sealed", such that only types within reqwest can -/// implement it. The reason is that it will eventually be deprecated -/// and removed, when `std::convert::TryFrom` is stabilized. -pub trait IntoUrl: PolyfillTryInto {} +/// implement it. +pub trait IntoUrl: IntoUrlSealed {} -impl IntoUrl for T {} +impl IntoUrl for Url {} +impl IntoUrl for String {} +impl<'a> IntoUrl for &'a str {} +impl<'a> IntoUrl for &'a String {} -pub trait PolyfillTryInto { +pub trait IntoUrlSealed { // Besides parsing as a valid `Url`, the `Url` must be a valid // `http::Uri`, in that it makes sense to use in a network request. fn into_url(self) -> crate::Result; - fn _as_str(&self) -> &str; + fn as_str(&self) -> &str; } -impl PolyfillTryInto for Url { +impl IntoUrlSealed for Url { fn into_url(self) -> crate::Result { if self.has_host() { Ok(self) @@ -26,27 +28,37 @@ impl PolyfillTryInto for Url { } } - fn _as_str(&self) -> &str { + fn as_str(&self) -> &str { self.as_ref() } } -impl<'a> PolyfillTryInto for &'a str { +impl<'a> IntoUrlSealed for &'a str { fn into_url(self) -> crate::Result { Url::parse(self).map_err(crate::error::builder)?.into_url() } - fn _as_str(&self) -> &str { - self.as_ref() + fn as_str(&self) -> &str { + self } } -impl<'a> PolyfillTryInto for &'a String { +impl<'a> IntoUrlSealed for &'a String { fn into_url(self) -> crate::Result { (&**self).into_url() } - fn _as_str(&self) -> &str { + fn as_str(&self) -> &str { + self.as_ref() + } +} + +impl<'a> IntoUrlSealed for String { + fn into_url(self) -> crate::Result { + (&*self).into_url() + } + + fn as_str(&self) -> &str { self.as_ref() } } diff --git a/src/proxy.rs b/src/proxy.rs index d266e56..f2afabe 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -3,7 +3,7 @@ use std::fmt; use std::net::SocketAddr; use std::sync::Arc; -use crate::into_url::{IntoUrl, PolyfillTryInto}; +use crate::into_url::{IntoUrl, IntoUrlSealed}; use crate::Url; use http::{header::HeaderValue, Uri}; use ipnet::IpNet; @@ -111,11 +111,11 @@ pub trait IntoProxyScheme { impl IntoProxyScheme for S { fn into_proxy_scheme(self) -> crate::Result { // validate the URL - let url = match self._as_str().into_url() { + let url = match self.as_str().into_url() { Ok(ok) => ok, Err(e) => { // the issue could have been caused by a missing scheme, so we try adding http:// - format!("http://{}", self._as_str()) + format!("http://{}", self.as_str()) .into_url() .map_err(|_| { // return the original error