macro: support deriving Display for newtypes

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-08-12 14:01:21 +02:00
parent 50f5115718
commit 8e24ada75a

View File

@ -132,6 +132,7 @@ fn newtype_filter_derive_attrs(
) -> Result<TokenStream, Error> {
let mut code = TokenStream::new();
let mut had_from_str = false;
let mut had_display = false;
let cap = attrs.len();
for mut attr in mem::replace(attrs, Vec::with_capacity(cap)) {
@ -154,6 +155,12 @@ fn newtype_filter_derive_attrs(
}
had_from_str = true;
continue;
} else if exprpath.path.is_ident("Display") {
if !had_display {
code.extend(newtype_derive_display(exprpath.path.span(), type_ident));
}
had_display = true;
continue;
}
}
exprtuple.elems.push(ty);
@ -177,3 +184,13 @@ fn newtype_derive_from_str(span: Span, type_ident: &Ident, inner_type: &syn::Typ
}
}
}
fn newtype_derive_display(span: Span, type_ident: &Ident) -> TokenStream {
quote_spanned! { span =>
impl ::std::fmt::Display for #type_ident {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
::std::fmt::Display::fmt(&self.0, f)
}
}
}
}