Skip to content

Commit 3be527e

Browse files
committed
📝 Update docs
1 parent 0c3e35e commit 3be527e

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,14 @@ impl Debug for MyBitfield { /* ... */ }
188188

189189
> Hint: You can use the rust-analyzer "Expand macro recursively" action to view the generated code.
190190
191-
## `fmt::Debug`
192191

193-
This macro automatically creates a suitable `fmt::Debug` implementation
194-
similar to the ones created for normal structs by `#[derive(Debug)]`.
195-
You can disable it with the extra debug argument.
192+
## `fmt::Debug` and `Default`
196193

197-
```rust
198-
#[bitfield(u64, debug = false)]
194+
This macro automatically creates a suitable `fmt::Debug` and `Default` implementations similar to the ones created for normal structs by `#[derive(Debug, Default)]`.
195+
You can disable this with the extra `debug` and `default` arguments.
196+
197+
```rs
198+
#[bitfield(u64, debug = false, default = false)]
199199
struct CustomDebug {
200200
data: u64
201201
}
@@ -204,7 +204,12 @@ impl fmt::Debug for CustomDebug {
204204
write!(f, "0x{:x}", self.data())
205205
}
206206
}
207+
impl Default for CustomDebug {
208+
fn default() -> Self {
209+
Self(123) // note: you can also use `#[bits(64, default = 123)]`
210+
}
211+
}
207212

208-
let val = CustomDebug::new().with_data(123);
213+
let val = CustomDebug::default();
209214
println!("{val:?}")
210215
```

src/lib.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,16 @@
178178
//!
179179
//! > Hint: You can use the rust-analyzer "Expand macro recursively" action to view the generated code.
180180
//!
181-
//! ## `fmt::Debug`
181+
//! ## `fmt::Debug` and `Default`
182182
//!
183-
//! This macro automatically creates a suitable `fmt::Debug` implementation
184-
//! similar to the ones created for normal structs by `#[derive(Debug)]`.
185-
//! You can disable it with the extra debug argument.
183+
//! This macro automatically creates a suitable `fmt::Debug` and `Default` implementations
184+
//! similar to the ones created for normal structs by `#[derive(Debug, Default)]`.
185+
//! You can disable this with the extra `debug` and `default` arguments.
186186
//!
187187
//! ```
188188
//! # use std::fmt;
189189
//! # use bitfield_struct::bitfield;
190-
//!
191-
//! #[bitfield(u64, debug = false)]
190+
//! #[bitfield(u64, debug = false, default = false)]
192191
//! struct CustomDebug {
193192
//! data: u64
194193
//! }
@@ -199,7 +198,13 @@
199198
//! }
200199
//! }
201200
//!
202-
//! let val = CustomDebug::new().with_data(123);
201+
//! impl Default for CustomDebug {
202+
//! fn default() -> Self {
203+
//! Self(123) // note: you can also use `#[bits(64, default = 123)]`
204+
//! }
205+
//! }
206+
//!
207+
//! let val = CustomDebug::default();
203208
//! println!("{val:?}")
204209
//! ```
205210
//!
@@ -233,7 +238,7 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
233238
ty,
234239
bits,
235240
debug,
236-
impl_default,
241+
default,
237242
} = syn::parse2::<Params>(args)?;
238243

239244
let span = input.fields.span();
@@ -290,7 +295,7 @@ fn bitfield_inner(args: TokenStream, input: TokenStream) -> syn::Result<TokenStr
290295

291296
let defaults = members.iter().map(|m| m.default());
292297

293-
let default_impl = if impl_default {
298+
let default_impl = if default {
294299
quote! {
295300
impl Default for #name {
296301
fn default() -> Self {
@@ -706,7 +711,7 @@ struct Params {
706711
ty: syn::Type,
707712
bits: usize,
708713
debug: bool,
709-
impl_default: bool,
714+
default: bool,
710715
}
711716

712717
impl Parse for Params {
@@ -720,7 +725,7 @@ impl Parse for Params {
720725
}
721726

722727
let mut debug = true;
723-
let mut impl_default = true;
728+
let mut default = true;
724729

725730
// try parse additional args
726731
while <Token![,]>::parse(input).is_ok() {
@@ -729,8 +734,8 @@ impl Parse for Params {
729734
let value = syn::LitBool::parse(input)?.value;
730735
if ident == "debug" {
731736
debug = value;
732-
} else if ident == "impl_default" {
733-
impl_default = value;
737+
} else if ident == "default" {
738+
default = value;
734739
} else {
735740
return Err(syn::Error::new(ident.span(), "unknown argument"));
736741
}
@@ -740,7 +745,7 @@ impl Parse for Params {
740745
bits,
741746
ty,
742747
debug,
743-
impl_default,
748+
default,
744749
})
745750
}
746751
}

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn members() {
8888
#[test]
8989
fn attrs() {
9090
/// We have a custom default
91-
#[bitfield(u64, impl_default = false)]
91+
#[bitfield(u64, default = false)]
9292
#[derive(PartialEq, Eq)]
9393
struct Full {
9494
data: u64,

0 commit comments

Comments
 (0)