Skip to content

Commit c630651

Browse files
committed
add of_defaults
1 parent c7eefa5 commit c630651

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bit-struct"
3-
version = "0.1.12"
3+
version = "0.1.13"
44
edition = "2021"
55
description = "Define structs which have fields which are assigned to individual bits, not bytes"
66
repository = "https://github.com/andrewgazelka/bit-struct"

src/lib.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,7 @@ macro_rules! bit_struct_impl {
340340

341341
impl Default for $name {
342342
fn default() -> Self {
343-
let mut res = unsafe { Self::from_unchecked(0) };
344-
$(
345-
res.$field().set(Default::default());
346-
)*
347-
res
343+
Self::of_defaults()
348344
}
349345
}
350346

@@ -360,6 +356,21 @@ macro_rules! bit_struct_impl {
360356
}
361357
) => {
362358

359+
impl $name {
360+
361+
/// Returns a valid representation for the bit_struct, where all values are
362+
/// the defaults. This is different than Self::default(), because the actual
363+
/// default implementation might not be composed of only the defaults of the
364+
/// given fields
365+
pub fn of_defaults() -> Self {
366+
let mut res = unsafe { Self::from_unchecked(0) };
367+
$(
368+
res.$field().set(Default::default());
369+
)*
370+
res
371+
}
372+
}
373+
363374
impl ::core::fmt::Debug for $name {
364375
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> core::fmt::Result {
365376
let mut copied = *self;

tests/integration.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ bit_struct!(
2525
);
2626

2727
#[test]
28-
fn full_count(){
28+
fn test_full_count() {
2929
let full_count = FullCount::new(124);
3030
assert_eq!(full_count.raw(), 124);
3131
}
3232

33+
#[test]
34+
fn test_of_defaults() {
35+
let full_count = FullCount::of_defaults();
36+
assert_eq!(full_count.raw(), 0);
37+
}
38+
3339
#[test]
3440
fn test_enum_froms() {
3541
macro_rules! froms {
@@ -52,7 +58,6 @@ fn test_enum_froms() {
5258

5359
froms!(ModeA, u8, u16, u32, u64, u128);
5460
froms!(ModeD, u8, u16, u32, u64, u128);
55-
5661
}
5762

5863
#[test]
@@ -69,7 +74,6 @@ fn test_enum_intos() {
6974

7075
intos!(ModeA, u8, u16, u32, u64, u128);
7176
intos!(ModeD, u8, u16, u32, u64, u128);
72-
7377
}
7478

7579
#[test]
@@ -94,7 +98,10 @@ fn test_bit_struct_defaults() {
9498
#[test]
9599
fn test_bit_struct_debug() {
96100
let abc = Abc::default();
97-
assert_eq!(format!("{:?}", abc), "Abc { mode: Two, _padding: 0, count: 0 }");
101+
assert_eq!(
102+
format!("{:?}", abc),
103+
"Abc { mode: Two, _padding: 0, count: 0 }"
104+
);
98105
}
99106

100107
#[test]
@@ -108,14 +115,14 @@ fn test_bit_struct_raw_values() {
108115
// 0x4200
109116
assert_eq!(abc.raw(), 0x4200);
110117

111-
let eq_abc = unsafe { Abc::from_unchecked(0x4200)};
118+
let eq_abc = unsafe { Abc::from_unchecked(0x4200) };
112119

113120
assert_eq!(eq_abc.raw(), 0x4200);
114121
assert_eq!(eq_abc, abc);
115122
}
116123

117124
#[test]
118-
fn test_new_types(){
125+
fn test_new_types() {
119126
assert_eq!(u1!(0).inner(), 0b0);
120127
assert_eq!(u1!(1).inner(), 0b1);
121128

@@ -142,19 +149,19 @@ fn test_new_types(){
142149
}
143150

144151
#[test]
145-
fn test_valid_struct(){
152+
fn test_valid_struct() {
146153
// 0b[AA]** **** **** ****
147154
// makes Abc valid where AA is 0b00, 0b01, 0b10
148155
// makes Abc invalid where AA is 0b11
149156

150-
for first_bits in 0x0 .. 0xF {
157+
for first_bits in 0x0..0xF {
151158
let raw = first_bits << 12;
152159
let mode_a_bits = first_bits >> 2;
153160
let conversion = Abc::try_from(raw);
154161
let valid = match mode_a_bits {
155162
0b00 | 0b01 | 0b10 => conversion.is_ok(),
156163
0b11 => conversion.is_err(),
157-
_ => panic!("impossible")
164+
_ => panic!("impossible"),
158165
};
159166

160167
assert!(valid);
@@ -196,7 +203,7 @@ fn test_bit_struct_creation() {
196203
}
197204

198205
#[test]
199-
fn fails(){
206+
fn fails() {
200207
let t = trybuild::TestCases::new();
201208
t.compile_fail("tests/compile/*.rs");
202209
}

0 commit comments

Comments
 (0)