Skip to content

Commit 3c6dbe9

Browse files
committed
Avoid extra clone in config if possible
Using `impl Into<String>` instead of `&str` in a fn arg allows both `&str` and `String` as parameters - thus if the caller already has a String object that it doesn't need, it can pass it in without extra cloning. The same might be done with the password, but may require closer look.
1 parent 98f5a11 commit 3c6dbe9

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

tokio-postgres/src/config.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ impl Config {
248248
/// Sets the user to authenticate with.
249249
///
250250
/// Defaults to the user executing this process.
251-
pub fn user(&mut self, user: &str) -> &mut Config {
252-
self.user = Some(user.to_string());
251+
pub fn user(&mut self, user: impl Into<String>) -> &mut Config {
252+
self.user = Some(user.into());
253253
self
254254
}
255255

@@ -277,8 +277,8 @@ impl Config {
277277
/// Sets the name of the database to connect to.
278278
///
279279
/// Defaults to the user.
280-
pub fn dbname(&mut self, dbname: &str) -> &mut Config {
281-
self.dbname = Some(dbname.to_string());
280+
pub fn dbname(&mut self, dbname: impl Into<String>) -> &mut Config {
281+
self.dbname = Some(dbname.into());
282282
self
283283
}
284284

@@ -289,8 +289,8 @@ impl Config {
289289
}
290290

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

@@ -301,8 +301,8 @@ impl Config {
301301
}
302302

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

@@ -330,15 +330,17 @@ impl Config {
330330
/// Multiple hosts can be specified by calling this method multiple times, and each will be tried in order. On Unix
331331
/// systems, a host starting with a `/` is interpreted as a path to a directory containing Unix domain sockets.
332332
/// There must be either no hosts, or the same number of hosts as hostaddrs.
333-
pub fn host(&mut self, host: &str) -> &mut Config {
333+
pub fn host(&mut self, host: impl Into<String>) -> &mut Config {
334+
let host = host.into();
335+
334336
#[cfg(unix)]
335337
{
336338
if host.starts_with('/') {
337339
return self.host_path(host);
338340
}
339341
}
340342

341-
self.host.push(Host::Tcp(host.to_string()));
343+
self.host.push(Host::Tcp(host));
342344
self
343345
}
344346

@@ -990,7 +992,7 @@ impl<'a> UrlParser<'a> {
990992

991993
let mut it = creds.splitn(2, ':');
992994
let user = self.decode(it.next().unwrap())?;
993-
self.config.user(&user);
995+
self.config.user(user);
994996

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

10551057
if !dbname.is_empty() {
1056-
self.config.dbname(&self.decode(dbname)?);
1058+
self.config.dbname(self.decode(dbname)?);
10571059
}
10581060

10591061
Ok(())

0 commit comments

Comments
 (0)