Skip to content

Commit 296a6a3

Browse files
committed
✨ Option for disabling new and docs
1 parent b636181 commit 296a6a3

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,6 @@ Each accessor also inherits the documentation of its field.
177177
The signatures for `int` are:
178178

179179
```rust
180-
use std::fmt::{Debug, Formatter, Result};
181-
182180
// generated struct
183181
struct MyBitfield(u64);
184182
impl MyBitfield {
@@ -187,9 +185,13 @@ impl MyBitfield {
187185
const INT_BITS: usize = 16;
188186
const INT_OFFSET: usize = 0;
189187

190-
const fn with_int(self, value: u16) -> Self { todo!() }
191188
const fn int(&self) -> u16 { todo!() }
189+
190+
const fn with_int(self, value: u16) -> Self { todo!() }
191+
const fn with_int_checked(self, value: u16) -> Result<Self, ()> { todo!() }
192+
192193
fn set_int(&mut self, value: u16) { todo!() }
194+
fn set_int_checked(&mut self, value: u16) -> Result<(), ()> { todo!() }
193195

194196
// other field ...
195197
}
@@ -409,9 +411,9 @@ struct DefmtExample {
409411
}
410412
```
411413

412-
### Conditionally Enable `Debug`/`Default`/`defmt::Format`
414+
### Conditionally Enable `new`/`Debug`/`Default`/`defmt::Format`
413415

414-
Instead of booleans, you can specify `cfg(...)` attributes for `debug`, `default` and `defmt`:
416+
Instead of booleans, you can specify `cfg(...)` attributes for `new`, `debug`, `default` and `defmt`:
415417

416418
```rust
417419
use bitfield_struct::bitfield;

src/lib.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,17 @@ fn s_err(span: proc_macro2::Span, msg: impl fmt::Display) -> syn::Error {
2727
/// - `repr` specifies the bitfield's representation in memory
2828
/// - `from` to specify a conversion function from repr to the bitfield's integer type
2929
/// - `into` to specify a conversion function from the bitfield's integer type to repr
30+
/// - `new` to disable the `new` function generation
3031
/// - `debug` to disable the `Debug` trait generation
3132
/// - `defmt` to enable the `defmt::Format` trait generation.
3233
/// - `default` to disable the `Default` trait generation
3334
/// - `order` to specify the bit order (Lsb, Msb)
3435
/// - `conversion` to disable the generation of `into_bits` and `from_bits`
3536
///
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+
///
3641
/// Parameters of the `bits` attribute (for fields):
3742
/// - the number of bits
3843
/// - `access` to specify the access mode (RW, RO, WO, None)
@@ -55,6 +60,7 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
5560
into,
5661
from,
5762
bits,
63+
new,
5864
debug,
5965
defmt,
6066
default,
@@ -114,15 +120,30 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
114120
impl_debug.extend(implement_defmt(&name, &members, cfg));
115121
}
116122

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+
});
118137

119138
let impl_default = default.cfg().map(|cfg| {
120139
let attr = cfg.map(|cfg| quote!(#[cfg(#cfg)]));
121140
quote! {
122141
#attr
123142
impl Default for #name {
124143
fn default() -> Self {
125-
Self::new()
144+
let mut this = Self(#from(0));
145+
#( #defaults )*
146+
this
126147
}
127148
}
128149
}
@@ -148,12 +169,8 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
148169
#vis struct #name(#repr);
149170

150171
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+
157174
#conversion
158175

159176
#( #members )*
@@ -814,6 +831,7 @@ struct Params {
814831
into: Option<syn::Path>,
815832
from: Option<syn::Path>,
816833
bits: usize,
834+
new: Enable,
817835
debug: Enable,
818836
defmt: Enable,
819837
default: Enable,
@@ -837,6 +855,7 @@ impl Parse for Params {
837855
into: None,
838856
from: None,
839857
bits,
858+
new: Enable::Yes,
840859
debug: Enable::Yes,
841860
defmt: Enable::No,
842861
default: Enable::Yes,
@@ -864,6 +883,9 @@ impl Parse for Params {
864883
"defmt" => {
865884
ret.defmt = input.parse()?;
866885
}
886+
"new" => {
887+
ret.new = input.parse()?;
888+
}
867889
"default" => {
868890
ret.default = input.parse()?;
869891
}

0 commit comments

Comments
 (0)