Skip to content

Commit 7261c4f

Browse files
committed
feat(api): impl From<Cow<'static, str>> for Id,OsStr, Str
1 parent a4756ee commit 7261c4f

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

clap_builder/src/builder/os_str.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ impl From<&'_ &'static str> for OsStr {
127127
}
128128
}
129129

130+
#[cfg(feature = "string")]
131+
impl From<Cow<'static, str>> for OsStr {
132+
fn from(cow: Cow<'static, str>) -> Self {
133+
match cow {
134+
Cow::Borrowed(s) => Self::from(s),
135+
Cow::Owned(s) => Self::from(s),
136+
}
137+
}
138+
}
139+
130140
impl From<OsStr> for std::ffi::OsString {
131141
fn from(name: OsStr) -> Self {
132142
name.name.into_os_string()

clap_builder/src/builder/str.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ impl From<&'_ &'static str> for Str {
7676
}
7777
}
7878

79+
#[cfg(feature = "string")]
80+
impl From<Cow<'static, str>> for Str {
81+
fn from(cow: Cow<'static, str>) -> Self {
82+
match cow {
83+
Cow::Borrowed(s) => Self::from(s),
84+
Cow::Owned(s) => Self::from(s),
85+
}
86+
}
87+
}
88+
7989
impl From<Str> for String {
8090
fn from(name: Str) -> Self {
8191
name.name.into_string()

clap_builder/src/builder/styled_str.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ impl From<&'_ &'static str> for StyledStr {
187187
}
188188
}
189189

190+
impl From<Cow<'static, str>> for StyledStr {
191+
fn from(cow: Cow<'static, str>) -> Self {
192+
match cow {
193+
Cow::Borrowed(s) => StyledStr::from(s),
194+
Cow::Owned(s) => StyledStr::from(s),
195+
}
196+
}
197+
}
198+
190199
impl std::fmt::Write for StyledStr {
191200
#[inline]
192201
fn write_str(&mut self, s: &str) -> Result<(), std::fmt::Error> {

clap_builder/src/util/id.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ impl From<Id> for Str {
7878
}
7979
}
8080

81+
#[cfg(feature = "string")]
82+
impl From<Cow<'static, str>> for Id {
83+
fn from(name: Cow<'static, str>) -> Self {
84+
Self(name.into())
85+
}
86+
}
87+
8188
impl From<Id> for String {
8289
fn from(name: Id) -> Self {
8390
Str::from(name).into()

0 commit comments

Comments
 (0)