Skip to content

Commit

Permalink
refactor: Simplify generated Debug impl
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayJack committed May 16, 2024
1 parent abacd20 commit 0b2a399
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
42 changes: 22 additions & 20 deletions src/example_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,18 +297,18 @@ impl core::fmt::Octal for Flags {
}
impl core::fmt::Debug for Flags {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
#[derive(Debug, Clone, Copy)]
#[allow(clippy::upper_case_acronyms)]
enum AuxEnum {
A,
B,
C,
ABC,
#[derive(Clone, Copy)]
struct AuxItem(&'static str);

impl core::fmt::Debug for AuxItem {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.pad(self.0)
}
}
struct Set([Option<AuxEnum>; 4]);
struct Set([Option<AuxItem>; 4]);

impl Set {
fn insert(&mut self, val: AuxEnum) {
fn insert(&mut self, val: AuxItem) {
for i in self.0.iter_mut() {
if i.is_none() {
*i = Some(val);
Expand All @@ -328,17 +328,19 @@ impl core::fmt::Debug for Flags {
}
let name = "Flags";
let mut set = Set([None; 4]);
if self.contains(Self::A) {
set.insert(AuxEnum::A);
}
if self.contains(Self::B) {
set.insert(AuxEnum::B);
}
if self.contains(Self::C) {
set.insert(AuxEnum::C);
}
if self.contains(Self::ABC) {
set.insert(AuxEnum::ABC);
{
if self.contains(Self::A) {
set.insert(AuxItem("A"));
}
if self.contains(Self::B) {
set.insert(AuxItem("B"));
}
if self.contains(Self::C) {
set.insert(AuxItem("C"));
}
if self.contains(Self::ABC) {
set.insert(AuxItem("ABC"));
}
}
f.debug_tuple(name)
.field(&format_args!("0b{:b}", self.0))
Expand Down
35 changes: 21 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro::{Span, TokenStream};
use quote::quote;
use syn::{spanned::Spanned, Error, Ident, ItemEnum, Result};
use syn::{Error, Ident, ItemEnum, Result};

/// An attribute macro that transforms an C-like enum into a bitflag struct implementing an type API
/// similar to the `bitflags` crate, and implementing traits as listed below.
Expand Down Expand Up @@ -93,10 +93,7 @@ fn bitflag_impl(attr: TokenStream, item: TokenStream) -> Result<TokenStream> {
};

all_flags.push(quote!(Self::#var_name));

let span = vis.span();

all_flags_names.push(quote::quote_spanned!(span=> #var_name));
all_flags_names.push(quote!(stringify!(#var_name)));

flags.push(quote! {
#(#var_attrs)*
Expand Down Expand Up @@ -421,16 +418,26 @@ fn bitflag_impl(attr: TokenStream, item: TokenStream) -> Result<TokenStream> {

impl core::fmt::Debug for #ty_name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
#[derive(Debug, Clone, Copy)]
#[allow(clippy::upper_case_acronyms)]
enum AuxEnum {
#(#all_flags_names, )*
// #[derive(Debug, Clone, Copy)]
// #[allow(clippy::upper_case_acronyms)]
// enum AuxEnum {
// #(#all_flags_names, )*
// }

#[derive(Clone, Copy)]
struct AuxItem(&'static str);

impl core::fmt::Debug for AuxItem {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.pad(self.0)
}
}

struct Set([Option<AuxEnum>; #num_flags]);
// struct Set([Option<AuxEnum>; #num_flags]);
struct Set([Option<AuxItem>; #num_flags]);

impl Set {
fn insert(&mut self, val: AuxEnum) {
fn insert(&mut self, val: AuxItem) {
for i in self.0.iter_mut() {
if i.is_none() {
*i = Some(val);
Expand All @@ -455,11 +462,11 @@ fn bitflag_impl(attr: TokenStream, item: TokenStream) -> Result<TokenStream> {
let name = stringify!(#ty_name);

let mut set = Set([None; #num_flags]);
{

#(if self.contains(#all_flags) {
set.insert(AuxEnum::#all_flags_names);
set.insert(AuxItem(#all_flags_names));
})*
}

f.debug_tuple(name)
.field(&format_args!("0b{:b}", self.0))
.field(&set)
Expand Down

0 comments on commit 0b2a399

Please sign in to comment.