Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IString: implement From<std::fmt::Arguments> for IString #49

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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!");
}
}