Skip to content

Commit

Permalink
[derive] Add support for fmt-debug=none (#1798)
Browse files Browse the repository at this point in the history
The unstable feature fmt-debug [1] allows to influence `derive(Debug)`
implementations, for example by making it return an empty string. As the
standard library also does not make any guarantees on the outputs of
`derive(Debug)` , this change does replace the reliance on it by an
explicit conversion to the variant name.
  • Loading branch information
bluec0re authored Oct 4, 2024
1 parent 68ccb1f commit 7f2d45d
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,26 @@ enum Trait {

impl ToTokens for Trait {
fn to_tokens(&self, tokens: &mut TokenStream) {
let ident = Ident::new(&format!("{:?}", self), Span::call_site());
// According to [1], the format of the derived `Debug`` output is not
// stable and therefore not guaranteed to represent the variant names.
// Indeed with the (unstable) `fmt-debug` compiler flag [2], it can
// return only a minimalized output or empty string. To make sure this
// code will work in the future and independet of the compiler flag, we
// translate the variants to their names manually here.
//
// [1] https://doc.rust-lang.org/1.81.0/std/fmt/trait.Debug.html#stability
// [2] https://doc.rust-lang.org/beta/unstable-book/compiler-flags/fmt-debug.html
let s = match self {
Trait::KnownLayout => "KnownLayout",
Trait::Immutable => "Immutable",
Trait::TryFromBytes => "TryFromBytes",
Trait::FromZeros => "FromZeros",
Trait::FromBytes => "FromBytes",
Trait::IntoBytes => "IntoBytes",
Trait::Unaligned => "Unaligned",
Trait::Sized => "Sized",
};
let ident = Ident::new(s, Span::call_site());
tokens.extend(core::iter::once(TokenTree::Ident(ident)));
}
}
Expand Down

0 comments on commit 7f2d45d

Please sign in to comment.