@@ -27,12 +27,17 @@ fn s_err(span: proc_macro2::Span, msg: impl fmt::Display) -> syn::Error {
27
27
/// - `repr` specifies the bitfield's representation in memory
28
28
/// - `from` to specify a conversion function from repr to the bitfield's integer type
29
29
/// - `into` to specify a conversion function from the bitfield's integer type to repr
30
+ /// - `new` to disable the `new` function generation
30
31
/// - `debug` to disable the `Debug` trait generation
31
32
/// - `defmt` to enable the `defmt::Format` trait generation.
32
33
/// - `default` to disable the `Default` trait generation
33
34
/// - `order` to specify the bit order (Lsb, Msb)
34
35
/// - `conversion` to disable the generation of `into_bits` and `from_bits`
35
36
///
37
+ /// > For `new`, `debug`, `defmt` or `default`, you can either use booleans
38
+ /// (`#[bitfield(u8, debug = false)]`) or cfg attributes
39
+ /// (`#[bitfield(u8, debug = cfg(test))]`) to enable/disable them.
40
+ ///
36
41
/// Parameters of the `bits` attribute (for fields):
37
42
/// - the number of bits
38
43
/// - `access` to specify the access mode (RW, RO, WO, None)
@@ -55,6 +60,7 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
55
60
into,
56
61
from,
57
62
bits,
63
+ new,
58
64
debug,
59
65
defmt,
60
66
default,
@@ -114,15 +120,30 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
114
120
impl_debug. extend ( implement_defmt ( & name, & members, cfg) ) ;
115
121
}
116
122
117
- let defaults = members. iter ( ) . map ( Member :: default) ;
123
+ let defaults = members. iter ( ) . map ( Member :: default) . collect :: < Vec < _ > > ( ) ;
124
+
125
+ let impl_new = new. cfg ( ) . map ( |cfg| {
126
+ let attr = cfg. map ( |cfg| quote ! ( #[ cfg( #cfg) ] ) ) ;
127
+ quote ! {
128
+ /// Creates a new default initialized bitfield.
129
+ #attr
130
+ #vis const fn new( ) -> Self {
131
+ let mut this = Self ( #from( 0 ) ) ;
132
+ #( #defaults ) *
133
+ this
134
+ }
135
+ }
136
+ } ) ;
118
137
119
138
let impl_default = default. cfg ( ) . map ( |cfg| {
120
139
let attr = cfg. map ( |cfg| quote ! ( #[ cfg( #cfg) ] ) ) ;
121
140
quote ! {
122
141
#attr
123
142
impl Default for #name {
124
143
fn default ( ) -> Self {
125
- Self :: new( )
144
+ let mut this = Self ( #from( 0 ) ) ;
145
+ #( #defaults ) *
146
+ this
126
147
}
127
148
}
128
149
}
@@ -148,12 +169,8 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
148
169
#vis struct #name( #repr) ;
149
170
150
171
impl #name {
151
- /// Creates a new default initialized bitfield.
152
- #vis const fn new( ) -> Self {
153
- let mut this = Self ( #from( 0 ) ) ;
154
- #( #defaults ) *
155
- this
156
- }
172
+ #impl_new
173
+
157
174
#conversion
158
175
159
176
#( #members ) *
@@ -814,6 +831,7 @@ struct Params {
814
831
into : Option < syn:: Path > ,
815
832
from : Option < syn:: Path > ,
816
833
bits : usize ,
834
+ new : Enable ,
817
835
debug : Enable ,
818
836
defmt : Enable ,
819
837
default : Enable ,
@@ -837,6 +855,7 @@ impl Parse for Params {
837
855
into : None ,
838
856
from : None ,
839
857
bits,
858
+ new : Enable :: Yes ,
840
859
debug : Enable :: Yes ,
841
860
defmt : Enable :: No ,
842
861
default : Enable :: Yes ,
@@ -864,6 +883,9 @@ impl Parse for Params {
864
883
"defmt" => {
865
884
ret. defmt = input. parse ( ) ?;
866
885
}
886
+ "new" => {
887
+ ret. new = input. parse ( ) ?;
888
+ }
867
889
"default" => {
868
890
ret. default = input. parse ( ) ?;
869
891
}
0 commit comments