Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid extra clone in config if possible #1137

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions tokio-postgres/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ impl Config {
/// Sets the user to authenticate with.
///
/// Defaults to the user executing this process.
pub fn user(&mut self, user: &str) -> &mut Config {
self.user = Some(user.to_string());
pub fn user(&mut self, user: impl Into<String>) -> &mut Config {
self.user = Some(user.into());
self
}

Expand Down Expand Up @@ -277,8 +277,8 @@ impl Config {
/// Sets the name of the database to connect to.
///
/// Defaults to the user.
pub fn dbname(&mut self, dbname: &str) -> &mut Config {
self.dbname = Some(dbname.to_string());
pub fn dbname(&mut self, dbname: impl Into<String>) -> &mut Config {
self.dbname = Some(dbname.into());
self
}

Expand All @@ -289,8 +289,8 @@ impl Config {
}

/// Sets command line options used to configure the server.
pub fn options(&mut self, options: &str) -> &mut Config {
self.options = Some(options.to_string());
pub fn options(&mut self, options: impl Into<String>) -> &mut Config {
self.options = Some(options.into());
self
}

Expand All @@ -301,8 +301,8 @@ impl Config {
}

/// Sets the value of the `application_name` runtime parameter.
pub fn application_name(&mut self, application_name: &str) -> &mut Config {
self.application_name = Some(application_name.to_string());
pub fn application_name(&mut self, application_name: impl Into<String>) -> &mut Config {
self.application_name = Some(application_name.into());
self
}

Expand Down Expand Up @@ -330,15 +330,17 @@ impl Config {
/// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix
/// systems, a host starting with a `/` is interpreted as a path to a directory containing Unix domain sockets.
/// There must be either no hosts, or the same number of hosts as hostaddrs.
pub fn host(&mut self, host: &str) -> &mut Config {
pub fn host(&mut self, host: impl Into<String>) -> &mut Config {
let host = host.into();

#[cfg(unix)]
{
if host.starts_with('/') {
return self.host_path(host);
}
}

self.host.push(Host::Tcp(host.to_string()));
self.host.push(Host::Tcp(host));
self
}

Expand Down Expand Up @@ -990,7 +992,7 @@ impl<'a> UrlParser<'a> {

let mut it = creds.splitn(2, ':');
let user = self.decode(it.next().unwrap())?;
self.config.user(&user);
self.config.user(user);

if let Some(password) = it.next() {
let password = Cow::from(percent_encoding::percent_decode(password.as_bytes()));
Expand Down Expand Up @@ -1053,7 +1055,7 @@ impl<'a> UrlParser<'a> {
};

if !dbname.is_empty() {
self.config.dbname(&self.decode(dbname)?);
self.config.dbname(self.decode(dbname)?);
}

Ok(())
Expand Down