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

Implement IdentFragment for Cow<T> #158

Merged
merged 2 commits into from
May 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/ident_fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ impl IdentFragment for Ident {
}
}

impl<'a, T> IdentFragment for std::borrow::Cow<'a, T>
where
T: 'a + IdentFragment + ToOwned + ?Sized,
{
fn span(&self) -> Option<Span> {
T::span(&self)
}

fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
T::fmt(&self, f)
}
}

// Limited set of types which this is implemented for, as we want to avoid types
// which will often include non-identifier characters in their `Display` impl.
macro_rules! ident_fragment_display {
Expand Down
2 changes: 2 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,14 @@ fn test_format_ident() {
let id2 = format_ident!("Hello{x}", x = 5usize);
let id3 = format_ident!("Hello{}_{x}", id0, x = 10usize);
let id4 = format_ident!("Aa", span = Span::call_site());
let id5 = format_ident!("Hello{}", Cow::Borrowed("World"));

assert_eq!(id0, "Aa");
assert_eq!(id1, "HelloAa");
assert_eq!(id2, "Hello5");
assert_eq!(id3, "HelloAa_10");
assert_eq!(id4, "Aa");
assert_eq!(id5, "HelloWorld");
}

#[test]
Expand Down