From 75ece05dbf894d5c6b396973499159ea239ca277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Wed, 3 Nov 2021 18:08:22 -0300 Subject: [PATCH] Avoid allocating in `nice_directory_display` when possible --- src/utils.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 79bdf8c..60248ab 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,7 @@ //! Random stuff used on ouch. use std::{ + borrow::Cow, cmp, env, ffi::OsStr, io, @@ -109,12 +110,12 @@ pub fn concatenate_list_of_os_str(os_strs: &[impl AsRef]) -> String { } /// Display the directory name, but change to "current directory" when necessary. -pub fn nice_directory_display(os_str: impl AsRef) -> String { - let text = to_utf(os_str); - if text == "." { - "current directory".to_string() +pub fn nice_directory_display(os_str: impl AsRef) -> Cow<'static, str> { + if os_str.as_ref() == "." { + Cow::Borrowed("current directory") } else { - format!("'{}'", text) + let text = to_utf(os_str); + Cow::Owned(format!("'{}'", text)) } }