1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#![no_std]
extern crate alloc;
use alloc::string::ToString;
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use syn::{parse_macro_input, spanned::Spanned, Data, DataStruct, DeriveInput, Fields, Ident};
#[proc_macro_derive(WeightDebug)]
pub fn derive_weight_debug(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
derive_debug(input, format_weight)
}
#[proc_macro_derive(ScheduleDebug)]
pub fn derive_schedule_debug(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
derive_debug(input, format_default)
}
fn derive_debug(
input: proc_macro::TokenStream,
fmt: impl Fn(&Ident) -> TokenStream,
) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let data = if let Data::Struct(data) = &input.data {
data
} else {
return quote_spanned! {
name.span() =>
compile_error!("WeightDebug is only supported for structs.");
}
.into()
};
#[cfg(feature = "full")]
let fields = iterate_fields(data, fmt);
#[cfg(not(feature = "full"))]
let fields = {
drop(fmt);
drop(data);
TokenStream::new()
};
let tokens = quote! {
impl #impl_generics core::fmt::Debug for #name #ty_generics #where_clause {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use ::sp_runtime::{FixedPointNumber, FixedU128 as Fixed};
let mut formatter = formatter.debug_struct(stringify!(#name));
#fields
formatter.finish()
}
}
};
tokens.into()
}
#[cfg(feature = "full")]
fn iterate_fields(data: &DataStruct, fmt: impl Fn(&Ident) -> TokenStream) -> TokenStream {
match &data.fields {
Fields::Named(fields) => {
let recurse = fields.named.iter().filter_map(|f| {
let name = f.ident.as_ref()?;
if name.to_string().starts_with('_') {
return None
}
let value = fmt(name);
let ret = quote_spanned! { f.span() =>
formatter.field(stringify!(#name), #value);
};
Some(ret)
});
quote! {
#( #recurse )*
}
},
Fields::Unnamed(fields) => quote_spanned! {
fields.span() =>
compile_error!("Unnamed fields are not supported")
},
Fields::Unit => quote!(),
}
}
fn format_weight(field: &Ident) -> TokenStream {
quote_spanned! { field.span() =>
&if self.#field > 1_000_000_000 {
format!(
"{:.1?} ms",
Fixed::saturating_from_rational(self.#field, 1_000_000_000).to_float()
)
} else if self.#field > 1_000_000 {
format!(
"{:.1?} µs",
Fixed::saturating_from_rational(self.#field, 1_000_000).to_float()
)
} else if self.#field > 1_000 {
format!(
"{:.1?} ns",
Fixed::saturating_from_rational(self.#field, 1_000).to_float()
)
} else {
format!("{} ps", self.#field)
}
}
}
fn format_default(field: &Ident) -> TokenStream {
quote_spanned! { field.span() =>
&self.#field
}
}