Skip to content

Commit

Permalink
Implied bounds for the remaining std::fmt traits
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Sep 5, 2021
1 parent 0a1c5bd commit cc65053
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 cc65053

Please sign in to comment.