Skip to content

Commit

Permalink
Merge pull request #150 from dtolnay/bounds
Browse files Browse the repository at this point in the history
Implied bounds for the remaining std::fmt traits
  • Loading branch information
dtolnay committed Sep 5, 2021
2 parents 0a1c5bd + cc65053 commit 34f5931
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
15 changes: 10 additions & 5 deletions impl/src/attr.rs
Expand Up @@ -31,10 +31,17 @@ pub struct Transparent<'a> {
pub span: Span,
}

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
pub enum Trait {
Debug,
Display,
Octal,
LowerHex,
UpperHex,
Pointer,
Binary,
LowerExp,
UpperExp,
}

pub fn get(input: &[Attribute]) -> Result<Attrs> {
Expand Down Expand Up @@ -200,9 +207,7 @@ impl ToTokens for Display<'_> {

impl ToTokens for Trait {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.extend(match self {
Trait::Debug => quote!(std::fmt::Debug),
Trait::Display => quote!(std::fmt::Display),
});
let trait_name = format_ident!("{}", format!("{:?}", self));
tokens.extend(quote!(std::fmt::#trait_name));
}
}
7 changes: 7 additions & 0 deletions impl/src/fmt.rs
Expand Up @@ -93,6 +93,13 @@ impl Display<'_> {
};
let bound = match read[..end_spec].chars().next_back() {
Some('?') => Trait::Debug,
Some('o') => Trait::Octal,
Some('x') => Trait::LowerHex,
Some('X') => Trait::UpperHex,
Some('p') => Trait::Pointer,
Some('b') => Trait::Binary,
Some('e') => Trait::LowerExp,
Some('E') => Trait::UpperExp,
Some(_) => Trait::Display,
None => {
has_bonus_display = true;
Expand Down

0 comments on commit 34f5931

Please sign in to comment.