Skip to content

Commit a69fe7c

Browse files
authored
Merge pull request #6154 from germangarces/impl-from-cow
feat(api): impl From<Cow<'static, str>> for Id,OsStr, Str
2 parents 0bb3ad7 + 55fafb4 commit a69fe7c

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

clap_builder/src/builder/os_str.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::builder::Str;
2+
#[cfg(feature = "string")]
3+
use std::borrow::Cow;
24

35
/// A UTF-8-encoded fixed string
46
///
@@ -126,6 +128,16 @@ impl From<&'_ &'static str> for OsStr {
126128
}
127129
}
128130

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+
129141
impl From<OsStr> for std::ffi::OsString {
130142
fn from(name: OsStr) -> Self {
131143
name.name.into_os_string()
@@ -328,3 +340,25 @@ impl std::hash::Hash for Inner {
328340
self.as_os_str().hash(state);
329341
}
330342
}
343+
344+
#[cfg(test)]
345+
#[cfg(feature = "string")]
346+
mod tests {
347+
use super::*;
348+
349+
#[test]
350+
#[cfg(feature = "string")]
351+
fn from_cow_borrowed() {
352+
let cow = Cow::Borrowed("hello");
353+
let osstr = OsStr::from(cow);
354+
assert_eq!(osstr, OsStr::from("hello"));
355+
}
356+
357+
#[test]
358+
#[cfg(feature = "string")]
359+
fn from_cow_owned() {
360+
let cow = Cow::Owned("world".to_string());
361+
let osstr = OsStr::from(cow);
362+
assert_eq!(osstr, OsStr::from("world"));
363+
}
364+
}

clap_builder/src/builder/str.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(feature = "string")]
2+
use std::borrow::Cow;
3+
14
/// A UTF-8-encoded fixed string
25
///
36
/// <div class="warning">
@@ -74,6 +77,16 @@ impl From<&'_ &'static str> for Str {
7477
}
7578
}
7679

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+
7790
impl From<Str> for String {
7891
fn from(name: Str) -> Self {
7992
name.name.into_string()
@@ -312,3 +325,25 @@ impl std::hash::Hash for Inner {
312325
self.as_str().hash(state);
313326
}
314327
}
328+
329+
#[cfg(test)]
330+
#[cfg(feature = "string")]
331+
mod tests {
332+
use super::*;
333+
334+
#[test]
335+
#[cfg(feature = "string")]
336+
fn from_cow_borrowed() {
337+
let cow = Cow::Borrowed("hello");
338+
let str = Str::from(cow);
339+
assert_eq!(str, Str::from("hello"));
340+
}
341+
342+
#[test]
343+
#[cfg(feature = "string")]
344+
fn from_cow_owned() {
345+
let cow = Cow::Owned("world".to_string());
346+
let str = Str::from(cow);
347+
assert_eq!(str, Str::from("world"));
348+
}
349+
}

clap_builder/src/builder/styled_str.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![cfg_attr(not(feature = "usage"), allow(dead_code))]
2+
use std::borrow::Cow;
23

34
/// Terminal-styling container
45
///
@@ -185,6 +186,15 @@ impl From<&'_ &'static str> for StyledStr {
185186
}
186187
}
187188

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+
188198
impl std::fmt::Write for StyledStr {
189199
#[inline]
190200
fn write_str(&mut self, s: &str) -> Result<(), std::fmt::Error> {
@@ -252,3 +262,22 @@ mod wrap_tests {
252262
);
253263
}
254264
}
265+
266+
#[cfg(test)]
267+
mod tests {
268+
use super::*;
269+
270+
#[test]
271+
fn from_cow_borrowed() {
272+
let cow = Cow::Borrowed("hello");
273+
let styled = StyledStr::from(cow);
274+
assert_eq!(styled, StyledStr::from("hello"));
275+
}
276+
277+
#[test]
278+
fn from_cow_owned() {
279+
let cow = Cow::Owned("world".to_string());
280+
let styled = StyledStr::from(cow);
281+
assert_eq!(styled, StyledStr::from("world"));
282+
}
283+
}

clap_builder/src/util/id.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::builder::Str;
2+
#[cfg(feature = "string")]
3+
use std::borrow::Cow;
24

35
/// [`Arg`][crate::Arg] or [`ArgGroup`][crate::ArgGroup] identifier
46
///
@@ -77,6 +79,13 @@ impl From<Id> for Str {
7779
}
7880
}
7981

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+
8089
impl From<Id> for String {
8190
fn from(name: Id) -> Self {
8291
Str::from(name).into()
@@ -162,3 +171,25 @@ impl PartialEq<Id> for String {
162171
PartialEq::eq(other, self)
163172
}
164173
}
174+
175+
#[cfg(test)]
176+
#[cfg(feature = "string")]
177+
mod tests {
178+
use super::*;
179+
180+
#[test]
181+
#[cfg(feature = "string")]
182+
fn from_cow_borrowed() {
183+
let cow = Cow::Borrowed("hello");
184+
let id = Id::from(cow);
185+
assert_eq!(id, Id::from("hello"));
186+
}
187+
188+
#[test]
189+
#[cfg(feature = "string")]
190+
fn from_cow_owned() {
191+
let cow = Cow::Owned("world".to_string());
192+
let id = Id::from(cow);
193+
assert_eq!(id, Id::from("world"));
194+
}
195+
}

0 commit comments

Comments
 (0)