| 1 | use proc_macro2rust-analyzer cargo proc-macro2 1.0.95 crate/::{Identrust-analyzer cargo proc-macro2 1.0.95 Ident#, TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#}; |
| 2 | use quoterust-analyzer cargo quote 1.0.40 crate/::{format_identrust-analyzer cargo quote 1.0.40 format/format_ident!, quoterust-analyzer cargo quote 1.0.40 quote!}; |
| 3 | use synrust-analyzer cargo syn 2.0.104 crate/::Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#; |
| 4 | |
| 5 | use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::commonrust-analyzer cargo bitfields-impl 1.0.0 generation/common/::{ |
| 6 | does_field_have_setterrust-analyzer cargo bitfields-impl 1.0.0 generation/common/does_field_have_setter()., generate_setting_fields_default_values_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/common/generate_setting_fields_default_values_tokens()., |
| 7 | generate_setting_fields_to_zero_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/common/generate_setting_fields_to_zero_tokens()., |
| 8 | }; |
| 9 | use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::field_const_getter_setterrust-analyzer cargo bitfields-impl 1.0.0 generation/field_const_getter_setter/::generate_setter_impl_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/field_const_getter_setter/generate_setter_impl_tokens().; |
| 10 | use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::parsingrust-analyzer cargo bitfields-impl 1.0.0 parsing/::bitfield_fieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/::{BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#, FieldAccessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldAccess#}; |
| 11 | |
| 12 | /// Generates the builder implementation. |
| 13 | pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) fn generate_builder_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/builder_struct/generate_builder_tokens().( |
| 14 | vis: Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#, |
| 15 | bitfield_type: &synrust-analyzer cargo syn 2.0.104 crate/::Typerust-analyzer cargo syn 2.0.104 ty/Type#, |
| 16 | bitfield_struct_name: Identrust-analyzer cargo proc-macro2 1.0.95 Ident#, |
| 17 | fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#], |
| 18 | ignored_fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#], |
| 19 | ) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# { |
| 20 | let builder_name = format_identrust-analyzer cargo quote 1.0.40 format/format_ident!!("{}Builder", bitfield_struct_name); |
| 21 | |
| 22 | let builder_setter_tokens = fields |
| 23 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 24 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|field| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().field.paddingrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#padding.) |
| 25 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|field| does_field_have_setterrust-analyzer cargo bitfields-impl 1.0.0 generation/common/does_field_have_setter().(field) || field.accessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#access. rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[FieldAccess][`PartialEq<Self>`]eq().==rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[FieldAccess][`PartialEq<Self>`]eq(). rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[FieldAccess][`PartialEq<Self>`]eq().FieldAccessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldAccess#::ReadOnlyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldAccess#ReadOnly#) |
| 26 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|field| { |
| 27 | let field_name = field.namerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#name..clonerust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][Clone]clone().(); |
| 28 | let field_name_with_builder_setter_ident = format_identrust-analyzer cargo quote 1.0.40 format/format_ident!!("with_{}", field_name); |
| 29 | let checked_field_name_with_builder_setter_ident = format_identrust-analyzer cargo quote 1.0.40 format/format_ident!!("checked_with_{}", field_name); |
| 30 | let field_offset_setter_ident = format_identrust-analyzer cargo quote 1.0.40 format/format_ident!!("set_{}", field_name); |
| 31 | let checked_field_offset_setter_ident = format_identrust-analyzer cargo quote 1.0.40 format/format_ident!!("checked_set_{}", field_name); |
| 32 | let field_type = field.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#ty..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Type`][Clone]clone().(); |
| 33 | let field_bits = field.bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#bits.; |
| 34 | let field_offset = field.offsetrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#offset.; |
| 35 | let field_bits_end = field_offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Add<Self>`]add().field_bits; |
| 36 | let with_field_documentation = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Sets builder bits [{}..={}].", field_offset, field_bits_end); |
| 37 | let checked_with_field_documentation = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Sets builder bits [{}..={}]. Returns an error if the value is too big to fit within the field bits.", field_offset, field_bits_end); |
| 38 | |
| 39 | if does_field_have_setterrust-analyzer cargo bitfields-impl 1.0.0 generation/common/does_field_have_setter().(field) { |
| 40 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 41 | #[doc = #with_field_documentation] |
| 42 | #vis fn #field_name_with_builder_setter_ident(mut self, bits: #field_type) -> Self { |
| 43 | self.this.#field_offset_setter_ident(bits); |
| 44 | self |
| 45 | } |
| 46 | |
| 47 | #[doc = #checked_with_field_documentation] |
| 48 | #vis fn #checked_field_name_with_builder_setter_ident(mut self, bits: #field_type) -> Result<Self, &'static str> { |
| 49 | self.this.#checked_field_offset_setter_ident(bits)?; |
| 50 | Ok(self) |
| 51 | } |
| 52 | } |
| 53 | } else { |
| 54 | let setter_impl_tokens = generate_setter_impl_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/field_const_getter_setter/generate_setter_impl_tokens().( |
| 55 | bitfield_type, |
| 56 | field.clonerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[BitfieldField][Clone]clone().(), |
| 57 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(quoterust-analyzer cargo quote 1.0.40 quote!! { #bitfield_struct_name }), |
| 58 | quoterust-analyzer cargo quote 1.0.40 quote!! { bits }, |
| 59 | /* check_value_bit_size= */ false, |
| 60 | !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().ignored_fields.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().(), |
| 61 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#( quoterust-analyzer cargo quote 1.0.40 quote!! { self.this } ), |
| 62 | ); |
| 63 | let checked_setter_impl_tokens = generate_setter_impl_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/field_const_getter_setter/generate_setter_impl_tokens().( |
| 64 | bitfield_type, |
| 65 | field.clonerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[BitfieldField][Clone]clone().(), |
| 66 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(quoterust-analyzer cargo quote 1.0.40 quote!! { #bitfield_struct_name }), |
| 67 | quoterust-analyzer cargo quote 1.0.40 quote!! { bits }, |
| 68 | /* check_value_bit_size= */ false, |
| 69 | !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().ignored_fields.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().(), |
| 70 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#( quoterust-analyzer cargo quote 1.0.40 quote!! { self.this } ), |
| 71 | ); |
| 72 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 73 | #[doc = #with_field_documentation] |
| 74 | #vis fn #field_name_with_builder_setter_ident(mut self, bits: #field_type) -> Self { |
| 75 | #setter_impl_tokens |
| 76 | self |
| 77 | } |
| 78 | |
| 79 | #[doc = #checked_with_field_documentation] |
| 80 | #vis fn #checked_field_name_with_builder_setter_ident(mut self, bits: #field_type) -> Result<Self, &'static str> { |
| 81 | #checked_setter_impl_tokens |
| 82 | Ok(self) |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | }); |
| 87 | |
| 88 | let setting_fields_default_values_tokens = generate_setting_fields_default_values_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/common/generate_setting_fields_default_values_tokens().( |
| 89 | bitfield_type, |
| 90 | fields, |
| 91 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(quoterust-analyzer cargo quote 1.0.40 quote!! { #bitfield_struct_name }), |
| 92 | !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().ignored_fields.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().(), |
| 93 | ); |
| 94 | let setting_fields_to_zero_tokens = generate_setting_fields_to_zero_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/common/generate_setting_fields_to_zero_tokens().( |
| 95 | bitfield_type, |
| 96 | fields, |
| 97 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(quoterust-analyzer cargo quote 1.0.40 quote!! { #bitfield_struct_name }), |
| 98 | !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().ignored_fields.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().(), |
| 99 | ); |
| 100 | |
| 101 | let ignored_fields_defaults = ignored_fields.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|field| { |
| 102 | let field_name = &field.namerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#name.; |
| 103 | let field_ty = &field.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#ty.; |
| 104 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 105 | #field_name: <#field_ty>::default(), |
| 106 | } |
| 107 | }); |
| 108 | |
| 109 | let initialize_struct_tokens = if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().ignored_fields.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() { |
| 110 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 111 | #bitfield_struct_name { |
| 112 | val: 0, |
| 113 | #( #ignored_fields_defaults )* |
| 114 | } |
| 115 | } |
| 116 | } else { |
| 117 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 118 | #bitfield_struct_name(0) |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 123 | #[doc = "A builder for the bitfield."] |
| 124 | #vis struct #builder_name { |
| 125 | this: #bitfield_struct_name, |
| 126 | } |
| 127 | |
| 128 | impl Default for #builder_name { |
| 129 | fn default() -> Self { |
| 130 | let mut this = #initialize_struct_tokens; |
| 131 | #setting_fields_default_values_tokens |
| 132 | Self { |
| 133 | this, |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | impl #builder_name { |
| 139 | #[doc = "Creates a new bitfield builder instance."] |
| 140 | #vis fn new() -> Self { |
| 141 | let mut this = #initialize_struct_tokens; |
| 142 | #setting_fields_default_values_tokens |
| 143 | Self { |
| 144 | this, |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | #[doc = "Creates a new bitfield builder instance without setting any default values."] |
| 149 | #vis fn new_without_defaults() -> Self { |
| 150 | let mut this = #initialize_struct_tokens; |
| 151 | #setting_fields_to_zero_tokens |
| 152 | Self { |
| 153 | this, |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | #( #builder_setter_tokens )* |
| 158 | |
| 159 | #[doc = "Builds a bitfield instance from the builder."] |
| 160 | #vis fn build(self) -> #bitfield_struct_name { |
| 161 | self.this |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /// Generates the to builder implementation. |
| 168 | pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) fn generate_to_builder_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/builder_struct/generate_to_builder_tokens().( |
| 169 | vis: Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#, |
| 170 | bitfield_struct_name: Identrust-analyzer cargo proc-macro2 1.0.95 Ident#, |
| 171 | ) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# { |
| 172 | let builder_name = format_identrust-analyzer cargo quote 1.0.40 format/format_ident!!("{}Builder", bitfield_struct_name); |
| 173 | |
| 174 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 175 | #vis fn to_builder(&self) -> #builder_name { |
| 176 | #builder_name { |
| 177 | this: self.clone(), |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |