Skip to content

Commit 7b8e84e

Browse files
committed
feat(api): impl From<Cow<'static, str>> for Id,OsStr, Str
1 parent 9ccdf04 commit 7b8e84e

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

clap_builder/src/builder/os_str.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ impl From<&'_ &'static str> for OsStr {
128128
}
129129
}
130130

131+
#[cfg(feature = "string")]
132+
impl From<Cow<'static, str>> for OsStr {
133+
fn from(cow: Cow<'static, str>) -> Self {
134+
match cow {
135+
Cow::Borrowed(s) => Self::from(s),
136+
Cow::Owned(s) => Self::from(s),
137+
}
138+
}
139+
}
140+
131141
impl From<OsStr> for std::ffi::OsString {
132142
fn from(name: OsStr) -> Self {
133143
name.name.into_os_string()
@@ -351,4 +361,4 @@ mod tests {
351361
let osstr = OsStr::from(cow);
352362
assert_eq!(osstr, OsStr::from("world"));
353363
}
354-
}
364+
}

clap_builder/src/builder/str.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ impl From<&'_ &'static str> for Str {
7777
}
7878
}
7979

80+
#[cfg(feature = "string")]
81+
impl From<Cow<'static, str>> for Str {
82+
fn from(cow: Cow<'static, str>) -> Self {
83+
match cow {
84+
Cow::Borrowed(s) => Self::from(s),
85+
Cow::Owned(s) => Self::from(s),
86+
}
87+
}
88+
}
89+
8090
impl From<Str> for String {
8191
fn from(name: Str) -> Self {
8292
name.name.into_string()
@@ -316,7 +326,6 @@ impl std::hash::Hash for Inner {
316326
}
317327
}
318328

319-
320329
#[cfg(test)]
321330
#[cfg(feature = "string")]
322331
mod tests {

clap_builder/src/builder/styled_str.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ impl From<&'_ &'static str> for StyledStr {
186186
}
187187
}
188188

189+
impl From<Cow<'static, str>> for StyledStr {
190+
fn from(cow: Cow<'static, str>) -> Self {
191+
match cow {
192+
Cow::Borrowed(s) => StyledStr::from(s),
193+
Cow::Owned(s) => StyledStr::from(s),
194+
}
195+
}
196+
}
197+
189198
impl std::fmt::Write for StyledStr {
190199
#[inline]
191200
fn write_str(&mut self, s: &str) -> Result<(), std::fmt::Error> {
@@ -271,4 +280,4 @@ mod tests {
271280
let styled = StyledStr::from(cow);
272281
assert_eq!(styled, StyledStr::from("world"));
273282
}
274-
}
283+
}

clap_builder/src/util/id.rs

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

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

0 commit comments

Comments
 (0)