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

feat: Implementation of From<Cow<'static, str>> for StyledStr #5799

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions clap_builder/src/builder/styled_str.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![cfg_attr(not(feature = "usage"), allow(dead_code))]

use std::borrow::Cow;

/// Terminal-styling container
///
/// Styling may be encoded as [ANSI Escape Code](https://en.wikipedia.org/wiki/ANSI_escape_code)
Expand Down Expand Up @@ -208,3 +210,9 @@ impl std::fmt::Display for StyledStr {
Ok(())
}
}

impl From<Cow<'static, str>> for StyledStr {
fn from(value: Cow<'static, str>) -> Self {
StyledStr(value.to_string())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use .into_owned()

to_string can work on any display and can make code more sloppy on refactor.

(sorry, I overlooked this last time and apparently a clippy lint didn't catch this case)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, Cow<'static, str>::to_string will cause unnecessary clone on owned case, into_owned is indeed what we want.

}
}