Skip to content

Commit

Permalink
impl From<CowStr> for String
Browse files Browse the repository at this point in the history
This matches the corresponding Cow impl that was stabilized in Rust 1.14:
https://doc.rust-lang.org/std/string/struct.String.html#impl-From%3CCow%3C'a,+str%3E%3E-for-String
  • Loading branch information
oconnor663 committed Aug 20, 2024
1 parent ecf248d commit 13cf238
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pulldown-cmark/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ impl<'a> From<Cow<'a, char>> for CowStr<'a> {
}
}

impl<'a> From<CowStr<'a>> for String {
fn from(s: CowStr<'a>) -> Self {
match s {
CowStr::Boxed(s) => s.into(),
CowStr::Inlined(s) => s.as_ref().into(),
CowStr::Borrowed(s) => s.into(),
}
}
}

impl<'a> Deref for CowStr<'a> {
type Target = str;

Expand Down Expand Up @@ -366,6 +376,28 @@ mod test_special_string {
assert!(variant_eq(&actual, &expected));
}

#[test]
fn cow_str_to_string() {
let s = "some text";
let cow_str = CowStr::Borrowed(s);
let actual = String::from(cow_str);
let expected = String::from("some text");
assert_eq!(actual, expected);

let s = "s";
let inline_str: InlineStr = InlineStr::try_from(s).unwrap();
let cow_str = CowStr::Inlined(inline_str);
let actual = String::from(cow_str);
let expected = String::from("s");
assert_eq!(actual, expected);

let s = "s";
let cow_str = CowStr::Boxed(s.to_string().into_boxed_str());
let actual = String::from(cow_str);
let expected = String::from("s");
assert_eq!(actual, expected);
}

#[test]
fn cow_char_to_cow_str() {
let c = 'c';
Expand Down

0 comments on commit 13cf238

Please sign in to comment.