Skip to content

Commit

Permalink
IString: implement From<std::fmt::Arguments> for IString (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
cecton committed Jan 26, 2024
1 parent c95b92e commit b757b45
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ impl From<Cow<'static, str>> for IString {
}
}

impl From<std::fmt::Arguments<'_>> for IString {
fn from(args: std::fmt::Arguments) -> IString {
if let Some(s) = args.as_str() {
IString::Static(s)
} else {
IString::from(args.to_string())
}
}
}

impl From<&IString> for IString {
fn from(s: &IString) -> IString {
s.clone()
Expand Down Expand Up @@ -406,4 +416,15 @@ mod test_string {
let s = IString::Static("foo");
let _out = IString::from(&s);
}

#[test]
fn from_fmt_arguments() {
let s = IString::from(format_args!("Hello World!"));
assert!(matches!(s, IString::Static("Hello World!")));

let name = "Jane";
let s = IString::from(format_args!("Hello {name}!"));
assert!(matches!(s, IString::Rc(_)));
assert_eq!(s, "Hello Jane!");
}
}

0 comments on commit b757b45

Please sign in to comment.