Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions bitbybit/src/bitfield/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ pub fn make_builder(
#[doc = #struct_name_str]
/// `].
#struct_vis struct #builder_struct_name<#( #params, )*> {
value: #struct_name,
// Mangled name to avoid clashing with user field names.
__value: #struct_name,
}
});

Expand Down Expand Up @@ -515,15 +516,15 @@ pub fn make_builder(
}
let mut array_setters = Vec::with_capacity(array_count);
for i in 0..array_count {
array_setters.push(quote! { .#with_name(#i, value[#i]) });
array_setters.push(quote! { .#with_name(#i, _value[#i]) });
}
let value_transform = quote!(self.value #( #array_setters )*);
let value_transform = quote!(self.__value #( #array_setters )*);
let array_type = quote! { [#setter_type; #array_count] };

(value_transform, array_type)
} else {
(
quote! { self.value.#with_name(value)},
quote! { self.__value.#with_name(_value)},
quote! { #setter_type },
)
};
Expand Down Expand Up @@ -560,9 +561,9 @@ pub fn make_builder(
#[allow(non_camel_case_types)]
impl<#( #params, )*> #builder_struct_name<#( #names, )*> {
#(#doc_comment)*
pub const fn #with_name(&self, value: #argument_type) -> #builder_struct_name<#( #result, )*> {
pub const fn #with_name(&self, _value: #argument_type) -> #builder_struct_name<#( #result, )*> {
#builder_struct_name {
value: #value_transform,
__value: #value_transform,
}
}
}
Expand Down Expand Up @@ -618,7 +619,7 @@ pub fn make_builder(
#[doc = #struct_name_str]
/// `].
pub const fn build(&self) -> #struct_name {
self.value
self.__value
}
}
});
Expand All @@ -630,17 +631,17 @@ pub fn make_builder(

let default = if has_default {
quote! { #builder_struct_name {
value: #struct_name::DEFAULT,
__value: #struct_name::DEFAULT,
} }
} else if base_data_size.exposed == base_data_size.internal {
quote! { #builder_struct_name {
value: #struct_name::new_with_raw_value(0),
__value: #struct_name::new_with_raw_value(0),
} }
} else {
quote! {
const ZERO: #base_data_type = #base_data_type::new(0);
#builder_struct_name {
value: #struct_name::new_with_raw_value(ZERO),
__value: #struct_name::new_with_raw_value(ZERO),
}
}
};
Expand Down
12 changes: 12 additions & 0 deletions bitbybit/tests/builder_value_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use bitbybit::bitfield;

// No issues with the private internal field value when a field is named value.
#[bitfield(u32, default = 0x0, debug)]
pub struct Data {
#[bit(15, rw)]
dparity: bool,
#[bits(0..=7, rw)]
value: u8,
}

pub fn main() {}
4 changes: 2 additions & 2 deletions bitbybit/tests/no_compile/invalid_field_range.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ error[E0599]: no method named `with_c` found for struct `PartialTest<true, true,
- `PartialTest<false, b, false>`
help: one of the expressions' fields has a method of the same name
|
30 | Test::builder().with_a(u4::new(1)).with_b(u4::new(1)).value.with_c(true).build();
| ++++++
30 | Test::builder().with_a(u4::new(1)).with_b(u4::new(1)).__value.with_c(true).build();
| ++++++++

error[E0599]: no method named `build` found for struct `PartialTest<false, false, true>` in the current scope
--> tests/no_compile/invalid_field_range.rs:31:34
Expand Down
4 changes: 2 additions & 2 deletions bitbybit/tests/no_compile/missing_fields_in_builder.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ error[E0599]: no method named `with_bar` found for struct `PartialTest<true, tru
- `PartialTest<foo, false>`
help: one of the expressions' fields has a method of the same name
|
15 | Test::builder().with_bar(1).with_foo(1).value.with_bar(2).build();
| ++++++
15 | Test::builder().with_bar(1).with_foo(1).__value.with_bar(2).build();
| ++++++++
8 changes: 4 additions & 4 deletions bitbybit/tests/no_compile/overlapping_bitfield_bits.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ error[E0599]: no method named `with_b` found for struct `PartialTestNonExhaustiv
|
help: one of the expressions' fields has a method of the same name
|
44 | .value.with_b(0)
| ++++++
44 | .__value.with_b(0)
| ++++++++
help: there is a method `with_c` with a similar name
|
44 - .with_b(0)
Expand All @@ -88,8 +88,8 @@ error[E0599]: no method named `with_b` found for struct `PartialTestExhaustiveOv
- `PartialTestExhaustiveOverlap<false, false>`
help: one of the expressions' fields has a method of the same name
|
48 | .value.with_b(0)
| ++++++
48 | .__value.with_b(0)
| ++++++++

error[E0599]: no method named `build` found for struct `PartialTestExhaustiveOverlap<false, false>` in the current scope
--> tests/no_compile/overlapping_bitfield_bits.rs:52:10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ error[E0599]: no method named `with_a` found for struct `PartialTestAllowed<true
- `PartialTestAllowed<upper, false, c, b, false>`
help: one of the expressions' fields has a method of the same name
|
48 | TestAllowed::builder().with_upper(u4::new(0)).with_lower(u12::new(0)).value.with_a(u2::new(0)).build();
| ++++++
48 | TestAllowed::builder().with_upper(u4::new(0)).with_lower(u12::new(0)).__value.with_a(u2::new(0)).build();
| ++++++++

error[E0599]: no function or associated item named `builder` found for struct `TestSometimesUnconstructable` in the current scope
--> tests/no_compile/overlapping_bitfield_u8_fields.rs:49:35
Expand Down
1 change: 1 addition & 0 deletions bitbybit/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fn all_tests() {
t.pass("tests/basic.rs");
t.pass("tests/with_fields.rs");
t.pass("tests/correct.rs");
t.pass("tests/builder_value_field.rs");

t.compile_fail("tests/no_compile/exhaustive_bitenum.rs");
t.compile_fail("tests/no_compile/non_exhaustive_bitenum.rs");
Expand Down
Loading