Skip to content

Commit

Permalink
Use AsRef<str> instead of Into<String>
Browse files Browse the repository at this point in the history
Saves allocations and/or taking ownership
Potentially a breaking change on some really weird types
  • Loading branch information
alpha-tango-kilo committed Jan 16, 2022
1 parent 878e7cb commit 92a108c
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ impl Request {
/// If `urlencoding` is enabled, the resource part of the URL will be
/// encoded. Any URL special characters (e.g. &, #, =) are not encoded
/// as they are assumed to be meaningful parameters etc.
pub fn new<T: Into<URL>>(method: Method, url: T) -> Request {
let (https, host, port, resource) = parse_url(url.into());
pub fn new<U: AsRef<str>>(method: Method, url: U) -> Request {
let (https, host, port, resource) = parse_url(url);
Request {
method,
host,
Expand Down Expand Up @@ -158,7 +158,7 @@ impl Request {
/// key or value.
///
/// If `urlencoding` is enabled, the key and value are both encoded.
pub fn with_param<T: Into<String>, U: Into<String>>(mut self, key: T, value: U) -> Request {
pub fn with_param<T: AsRef<str>, U: AsRef<str>>(mut self, key: T, value: U) -> Request {
// Checks if the resource already has a query parameter
// mentioned in url and if true, adds '&' to add one more
// parameter or adds '?' to add the first parameter
Expand All @@ -167,12 +167,12 @@ impl Request {
} else {
self.resource.push('?');
}
let key = key.into();
let key = key.as_ref();
#[cfg(feature = "urlencoding")]
let key = urlencoding::encode(&key);
let value = value.into();
let key = urlencoding::encode(key);
let value = value.as_ref();
#[cfg(feature = "urlencoding")]
let value = urlencoding::encode(&value);
let value = urlencoding::encode(value);
self.resource.push_str(&format!("{}={}", key, value));
self
}
Expand Down Expand Up @@ -438,7 +438,7 @@ impl Request {
}
}

fn parse_url(url: URL) -> (bool, URL, Port, URL) {
fn parse_url<U: AsRef<str>>(url: U) -> (bool, URL, Port, URL) {
enum UrlParseStatus {
Protocol,
AtFirstSlash,
Expand All @@ -451,7 +451,7 @@ fn parse_url(url: URL) -> (bool, URL, Port, URL) {
let mut port = String::new();
let mut resource = URL::new();
let mut status = UrlParseStatus::Protocol;
for c in url.chars() {
for c in url.as_ref().chars() {
match status {
UrlParseStatus::Protocol if c == '/' => {
status = UrlParseStatus::AtFirstSlash;
Expand Down Expand Up @@ -525,7 +525,7 @@ fn parse_url(url: URL) -> (bool, URL, Port, URL) {
resource += "/";
}
// Set appropriate port
let https = url.starts_with("https://");
let https = url.as_ref().starts_with("https://");
let port = port.parse::<u32>().map(Port::Explicit).unwrap_or_else(|_| {
if https {
Port::ImplicitHttps
Expand All @@ -548,55 +548,55 @@ fn to_hex_digit(digit: u8) -> char {

/// Alias for [Request::new](struct.Request.html#method.new) with `method` set to
/// [Method::Get](enum.Method.html).
pub fn get<T: Into<URL>>(url: T) -> Request {
pub fn get<U: AsRef<str>>(url: U) -> Request {
Request::new(Method::Get, url)
}

/// Alias for [Request::new](struct.Request.html#method.new) with `method` set to
/// [Method::Head](enum.Method.html).
pub fn head<T: Into<URL>>(url: T) -> Request {
pub fn head<U: AsRef<str>>(url: U) -> Request {
Request::new(Method::Head, url)
}

/// Alias for [Request::new](struct.Request.html#method.new) with `method` set to
/// [Method::Post](enum.Method.html).
pub fn post<T: Into<URL>>(url: T) -> Request {
pub fn post<U: AsRef<str>>(url: U) -> Request {
Request::new(Method::Post, url)
}

/// Alias for [Request::new](struct.Request.html#method.new) with `method` set to
/// [Method::Put](enum.Method.html).
pub fn put<T: Into<URL>>(url: T) -> Request {
pub fn put<U: AsRef<str>>(url: U) -> Request {
Request::new(Method::Put, url)
}

/// Alias for [Request::new](struct.Request.html#method.new) with `method` set to
/// [Method::Delete](enum.Method.html).
pub fn delete<T: Into<URL>>(url: T) -> Request {
pub fn delete<U: AsRef<str>>(url: U) -> Request {
Request::new(Method::Delete, url)
}

/// Alias for [Request::new](struct.Request.html#method.new) with `method` set to
/// [Method::Connect](enum.Method.html).
pub fn connect<T: Into<URL>>(url: T) -> Request {
pub fn connect<U: AsRef<str>>(url: U) -> Request {
Request::new(Method::Connect, url)
}

/// Alias for [Request::new](struct.Request.html#method.new) with `method` set to
/// [Method::Options](enum.Method.html).
pub fn options<T: Into<URL>>(url: T) -> Request {
pub fn options<U: AsRef<str>>(url: U) -> Request {
Request::new(Method::Options, url)
}

/// Alias for [Request::new](struct.Request.html#method.new) with `method` set to
/// [Method::Trace](enum.Method.html).
pub fn trace<T: Into<URL>>(url: T) -> Request {
pub fn trace<U: AsRef<str>>(url: U) -> Request {
Request::new(Method::Trace, url)
}

/// Alias for [Request::new](struct.Request.html#method.new) with `method` set to
/// [Method::Patch](enum.Method.html).
pub fn patch<T: Into<URL>>(url: T) -> Request {
pub fn patch<U: AsRef<str>>(url: U) -> Request {
Request::new(Method::Patch, url)
}

Expand Down

0 comments on commit 92a108c

Please sign in to comment.