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 allocating in nice_directory_display when possible, make Extension non-exhaustive #156

Merged
merged 2 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use self::CompressionFormat::*;

/// A wrapper around `CompressionFormat` that allows combinations like `tgz`
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Extension {
/// One extension like "tgz" can be made of multiple CompressionFormats ([Tar, Gz])
pub compression_formats: &'static [CompressionFormat],
Expand Down Expand Up @@ -35,7 +36,7 @@ impl Extension {

impl fmt::Display for Extension {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.display_text)
self.display_text.fmt(f)
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Random stuff used on ouch.

use std::{
borrow::Cow,
cmp, env,
ffi::OsStr,
io,
Expand Down Expand Up @@ -109,12 +110,12 @@ pub fn concatenate_list_of_os_str(os_strs: &[impl AsRef<OsStr>]) -> String {
}

/// Display the directory name, but change to "current directory" when necessary.
pub fn nice_directory_display(os_str: impl AsRef<OsStr>) -> String {
let text = to_utf(os_str);
if text == "." {
"current directory".to_string()
pub fn nice_directory_display(os_str: impl AsRef<OsStr>) -> 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))
}
}

Expand Down