Skip to content

Commit 103fd04

Browse files
committed
Introduce ZERO constant
This is a shortcut for `A::new_with_raw_value(0)`.
1 parent cdf15f9 commit 103fd04

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

bitbybit-tests/src/bitfield_tests.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ fn test_construction() {
1414
assert_eq!(45, t.raw_value);
1515
}
1616

17+
#[test]
18+
fn test_zero() {
19+
#[bitfield(u32)]
20+
struct TestA {}
21+
22+
#[bitfield(u32, default = 0x123)]
23+
struct TestB {}
24+
25+
assert_eq!(0, TestA::ZERO.raw_value());
26+
assert_eq!(0, TestB::ZERO.raw_value());
27+
}
28+
1729
#[test]
1830
fn test_getter_and_with() {
1931
#[bitfield(u128, default = 0)]
@@ -1549,7 +1561,6 @@ fn test_noncontiguous_ranges_array_with_interleaving_and_builder() {
15491561
);
15501562
}
15511563

1552-
15531564
#[test]
15541565
fn test_getter_and_setter() {
15551566
#[bitfield(u128, default = 0)]

bitbybit/src/bitfield/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ pub fn bitfield(args: TokenStream, input: TokenStream) -> TokenStream {
251251
quote! { #base_data_type::#extract(self.raw_value, 0) }
252252
};
253253

254+
let zero = if base_data_size.exposed == base_data_size.internal {
255+
quote! { 0 }
256+
} else {
257+
quote! { #base_data_type::new(0) }
258+
};
259+
254260
let expanded = quote! {
255261
#[derive(Copy, Clone)]
256262
#[repr(C)]
@@ -260,6 +266,8 @@ pub fn bitfield(args: TokenStream, input: TokenStream) -> TokenStream {
260266
}
261267

262268
impl #struct_name {
269+
pub const ZERO: Self = Self::new_with_raw_value(#zero);
270+
263271
#default_constructor
264272
/// Returns the underlying raw value of this bitfield
265273
#[inline]

0 commit comments

Comments
 (0)