| 1 | use proc_macro2rust-analyzer cargo proc-macro2 1.0.95 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#; |
| 2 | use quoterust-analyzer cargo quote 1.0.40 crate/::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/::generate_setting_fields_from_bits_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/common/generate_setting_fields_from_bits_tokens().; |
| 6 | use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::parsingrust-analyzer cargo bitfields-impl 1.0.0 parsing/::bitfield_attributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/::{BitfieldAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#, Endianrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/Endian#}; |
| 7 | 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#; |
| 8 | |
| 9 | /// Generates the `from_bits` function for the bitfield. |
| 10 | pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) fn generate_from_bits_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/from_into_bits_conversions/generate_from_bits_function_tokens().( |
| 11 | vis: Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#, |
| 12 | fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#], |
| 13 | ignored_fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#], |
| 14 | bitfield_type: &synrust-analyzer cargo syn 2.0.104 crate/::Typerust-analyzer cargo syn 2.0.104 ty/Type#, |
| 15 | bitfield_attribute: &BitfieldAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#, |
| 16 | ) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# { |
| 17 | let setting_fields_from_bits_tokens = generate_setting_fields_from_bits_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/common/generate_setting_fields_from_bits_tokens().( |
| 18 | bitfield_type, |
| 19 | fields, |
| 20 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(quoterust-analyzer cargo quote 1.0.40 quote!! { Self }), |
| 21 | false, |
| 22 | !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().(), |
| 23 | ); |
| 24 | |
| 25 | let swap_bits_endian_tokens = (bitfield_attribute.from_endianrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#from_endian. rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/impl#[Endian][`PartialEq<Self>`]eq().==rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/impl#[Endian][`PartialEq<Self>`]eq(). rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/impl#[Endian][`PartialEq<Self>`]eq().Endianrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/Endian#::Littlerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/Endian#Little#).thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| { |
| 26 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 27 | let bits = bits.swap_bytes(); |
| 28 | } |
| 29 | }); |
| 30 | |
| 31 | 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| { |
| 32 | let field_name = &field.namerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#name.; |
| 33 | let field_ty = &field.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#ty.; |
| 34 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 35 | #field_name: <#field_ty>::default(), |
| 36 | } |
| 37 | }); |
| 38 | |
| 39 | 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().() { |
| 40 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 41 | Self { |
| 42 | val: 0, |
| 43 | #( #ignored_fields_defaults )* |
| 44 | } |
| 45 | } |
| 46 | } else { |
| 47 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 48 | Self(0) |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | let const_ident_tokens = ignored_fields.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().().thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| quoterust-analyzer cargo quote 1.0.40 quote!! { const }); |
| 53 | |
| 54 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 55 | #[doc = "Creates a new bitfield instance from the given bits."] |
| 56 | #vis #const_ident_tokens fn from_bits(bits: #bitfield_type) -> Self { |
| 57 | #swap_bits_endian_tokens |
| 58 | let mut this = #initialize_struct_tokens; |
| 59 | #setting_fields_from_bits_tokens |
| 60 | this |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /// Generates the `from_bits_with_defaults` function for the bitfield. |
| 66 | pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) fn generate_from_bits_with_defaults_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/from_into_bits_conversions/generate_from_bits_with_defaults_function_tokens().( |
| 67 | vis: Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#, |
| 68 | fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#], |
| 69 | bitfield_type: &synrust-analyzer cargo syn 2.0.104 crate/::Typerust-analyzer cargo syn 2.0.104 ty/Type#, |
| 70 | bitfield_attribute: &BitfieldAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#, |
| 71 | ignored_fields_struct: bool, |
| 72 | ) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# { |
| 73 | let setting_fields_from_bits_tokens = generate_setting_fields_from_bits_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/common/generate_setting_fields_from_bits_tokens().( |
| 74 | bitfield_type, |
| 75 | fields, |
| 76 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(quoterust-analyzer cargo quote 1.0.40 quote!! { Self }), |
| 77 | true, |
| 78 | ignored_fields_struct, |
| 79 | ); |
| 80 | |
| 81 | let swap_bits_endian_tokens = (bitfield_attribute.from_endianrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#from_endian. rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/impl#[Endian][`PartialEq<Self>`]eq().==rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/impl#[Endian][`PartialEq<Self>`]eq(). rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/impl#[Endian][`PartialEq<Self>`]eq().Endianrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/Endian#::Littlerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/Endian#Little#).thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| { |
| 82 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 83 | let bits = bits.swap_bytes(); |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | let const_ident_tokens = (!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().ignored_fields_struct).thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| quoterust-analyzer cargo quote 1.0.40 quote!! { const }); |
| 88 | |
| 89 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 90 | #[doc = "Creates a new bitfield instance from the given bits while respecting field default values."] |
| 91 | #vis #const_ident_tokens fn from_bits_with_defaults(bits: #bitfield_type) -> Self { |
| 92 | #swap_bits_endian_tokens |
| 93 | let mut this = Self::from_bits(bits); |
| 94 | #setting_fields_from_bits_tokens |
| 95 | this |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) fn generate_into_bits_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/from_into_bits_conversions/generate_into_bits_function_tokens().( |
| 101 | vis: Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#, |
| 102 | bitfield_attribute: &BitfieldAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#, |
| 103 | ignored_fields_struct: bool, |
| 104 | ) -> TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# { |
| 105 | let struct_val_ident = if ignored_fields_struct { |
| 106 | quoterust-analyzer cargo quote 1.0.40 quote!! { self.val } |
| 107 | } else { |
| 108 | quoterust-analyzer cargo quote 1.0.40 quote!! { self.0 } |
| 109 | }; |
| 110 | |
| 111 | let into_bits_impl = match bitfield_attribute.into_endianrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#into_endian. { |
| 112 | Endianrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/Endian#::Bigrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/Endian#Big# => { |
| 113 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 114 | #struct_val_ident |
| 115 | } |
| 116 | } |
| 117 | Endianrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/Endian#::Littlerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/Endian#Little# => { |
| 118 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 119 | #struct_val_ident.swap_bytes() |
| 120 | } |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | let bitfield_type = &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.; |
| 125 | |
| 126 | quoterust-analyzer cargo quote 1.0.40 quote!! { |
| 127 | #[doc = "Returns the bits of the bitfield."] |
| 128 | #vis const fn into_bits(self) -> #bitfield_type { |
| 129 | #into_bits_impl |
| 130 | } |
| 131 | } |
| 132 | } |