Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 mod generationrust-analyzer cargo bitfields-impl 1.0.0 generation/;
2 mod parsingrust-analyzer cargo bitfields-impl 1.0.0 parsing/;
3
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#;
5 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/;
6
7 use proc_macro2rust-analyzer cargo proc-macro2 1.0.95 crate/::TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#;
8 use quoterust-analyzer cargo quote 1.0.40 crate/::{ToTokensrust-analyzer cargo quote 1.0.40 to_tokens/ToTokens#, quoterust-analyzer cargo quote 1.0.40 quote!};
9 use synrust-analyzer cargo syn 2.0.104 crate/::spannedrust-analyzer cargo syn 2.0.104 spanned/::Spannedrust-analyzer cargo syn 2.0.104 spanned/Spanned#;
10 use synrust-analyzer cargo syn 2.0.104 crate/::{Exprrust-analyzer cargo syn 2.0.104 expr/Expr#, ExprLitrust-analyzer cargo syn 2.0.104 expr/ExprLit#, ExprUnaryrust-analyzer cargo syn 2.0.104 expr/ExprUnary#, Fieldsrust-analyzer cargo syn 2.0.104 data/Fields#, Litrust-analyzer cargo syn 2.0.104 lit/Lit#, LitIntrust-analyzer cargo syn 2.0.104 lit/LitInt#, Metarust-analyzer cargo syn 2.0.104 attr/Meta#, Typerust-analyzer cargo syn 2.0.104 ty/Type#, Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#};
11
12 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::bit_operationsrust-analyzer cargo bitfields-impl 1.0.0 generation/bit_operations/::{generate_get_bit_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/bit_operations/generate_get_bit_tokens()., generate_set_bit_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/bit_operations/generate_set_bit_tokens().};
13 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::builder_structrust-analyzer cargo bitfields-impl 1.0.0 generation/builder_struct/::{generate_builder_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/builder_struct/generate_builder_tokens()., generate_to_builder_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/builder_struct/generate_to_builder_tokens().};
14 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/::PANIC_ERROR_MESSAGErust-analyzer cargo bitfields-impl 1.0.0 generation/common/PANIC_ERROR_MESSAGE.;
15 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::debug_implrust-analyzer cargo bitfields-impl 1.0.0 generation/debug_impl/::generate_debug_implementationrust-analyzer cargo bitfields-impl 1.0.0 generation/debug_impl/generate_debug_implementation().;
16 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::default_implrust-analyzer cargo bitfields-impl 1.0.0 generation/default_impl/::generate_default_implementation_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/default_impl/generate_default_implementation_tokens().;
17 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/::{
18 generate_field_constants_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/field_const_getter_setter/generate_field_constants_tokens()., generate_field_getters_functions_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/field_const_getter_setter/generate_field_getters_functions_tokens().,
19 generate_field_setters_functions_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/field_const_getter_setter/generate_field_setters_functions_tokens().,
20 };
21 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::from_into_bits_conversionsrust-analyzer cargo bitfields-impl 1.0.0 generation/from_into_bits_conversions/::{
22 generate_from_bits_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/from_into_bits_conversions/generate_from_bits_function_tokens()., 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().,
23 generate_into_bits_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/from_into_bits_conversions/generate_into_bits_function_tokens().,
24 };
25 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::from_types_implrust-analyzer cargo bitfields-impl 1.0.0 generation/from_types_impl/::{
26 generate_from_bitfield_for_bitfield_type_implementation_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/from_types_impl/generate_from_bitfield_for_bitfield_type_implementation_tokens().,
27 generate_from_bitfield_type_for_bitfield_implementation_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/from_types_impl/generate_from_bitfield_type_for_bitfield_implementation_tokens().,
28 };
29 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::new_implrust-analyzer cargo bitfields-impl 1.0.0 generation/new_impl/::{
30 generate_new_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/new_impl/generate_new_function_tokens()., generate_new_without_defaults_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/new_impl/generate_new_without_defaults_function_tokens().,
31 };
32 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::set_clear_bits_implrust-analyzer cargo bitfields-impl 1.0.0 generation/set_clear_bits_impl/::generate_set_bits_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/set_clear_bits_impl/generate_set_bits_function_tokens().;
33 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::set_clear_bits_implrust-analyzer cargo bitfields-impl 1.0.0 generation/set_clear_bits_impl/::{
34 generate_clear_bits_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/set_clear_bits_impl/generate_clear_bits_function_tokens()., generate_clear_bits_preserve_defaults_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/set_clear_bits_impl/generate_clear_bits_preserve_defaults_function_tokens().,
35 generate_set_bits_with_defaults_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/set_clear_bits_impl/generate_set_bits_with_defaults_function_tokens().,
36 };
37 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::generationrust-analyzer cargo bitfields-impl 1.0.0 generation/::tuple_structrust-analyzer cargo bitfields-impl 1.0.0 generation/tuple_struct/::{
38 generate_struct_with_fields_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/tuple_struct/generate_struct_with_fields_tokens()., generate_tuple_struct_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/tuple_struct/generate_tuple_struct_tokens().,
39 };
40 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/::{BitOrderrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitOrder#, BitfieldAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#};
41 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#, BitsAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitsAttribute#, FieldAccessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldAccess#, FieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#};
42 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::parsingrust-analyzer cargo bitfields-impl 1.0.0 parsing/::number_parserrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/::{NumberParseErrorrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#, ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#, parse_number_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/parse_number_string().};
43 use craterust-analyzer cargo bitfields-impl 1.0.0 crate/::parsingrust-analyzer cargo bitfields-impl 1.0.0 parsing/::typesrust-analyzer cargo bitfields-impl 1.0.0 parsing/types/::{
44 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#, get_bits_from_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_bits_from_type()., get_integer_suffix_from_integer_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_integer_suffix_from_integer_type().,
45 get_integer_type_from_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_integer_type_from_type()., get_type_identrust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_type_ident()., is_custom_field_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/is_custom_field_type()., is_size_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/is_size_type().,
46 is_supported_field_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/is_supported_field_type()., is_unsigned_integer_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/is_unsigned_integer_type().,
47 };
48
49 /// The `#[bit]` attribute name.
50 pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) const BIT_ATTRIBUTE_NAMErust-analyzer cargo bitfields-impl 1.0.0 BIT_ATTRIBUTE_NAME.: &str = "bits";
51
52 /// The ident prefix for padding fields.
53 pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) const PADDING_FIELD_NAME_PREFIXrust-analyzer cargo bitfields-impl 1.0.0 PADDING_FIELD_NAME_PREFIX.: &str = "_";
54
55 #[rustfmt::skip]
56 /// Creates a bitfield for the attributed struct.
57 ///
58 /// ## Example
59 ///
60 /// ```ignore
61 /// use bitfields::bitfield;
62 ///
63 /// /// All fields in the bitfield must sum up to the number of bits of the bitfield type.
64 /// #[bitfield(u64)]
65 /// #[derive(Clone)] // Attributes are passed to the struct.
66 /// pub struct Bitfield {
67 /// /// Fields without bits specified default to the size of the field type.
68 /// /// 8 bits.
69 /// u8int: u8,
70 /// /// A field can have specified bits, but the bits must be greater than zero
71 /// /// and fit in the field type.
72 /// #[bitfield(4)] // u8 is 8 bits, so 4 bits is valid.
73 /// small_u8int: u8,
74 /// /// A field that is signed, will be sign-extended by the most significant
75 /// /// bit of its type.
76 /// signed_int: i8,
77 /// /// If you specify bits, the field will be sign-extended by the most significant
78 /// /// bit of the specified bits. In this case, the most significant bit of 4 bits.
79 /// #[bits(4)]
80 /// small_signed_int: i8,
81 /// /// A field can be a bool type.
82 /// bool_field: bool,
83 /// /// A field can have a default value, which must fit in the field type.
84 /// #[bits(default = 0x1F)]
85 /// field_with_default: u8,
86 /// /// A field can have a default value and specified bits. The default value
87 /// /// must fit in the specified bits or a compile error will occur.
88 /// #[bits(4, default = 0xF)] // Default fits in 4 bits.
89 /// field_with_bits_default: u8,
90 /// /// By default, all functions share the same visibility as the bitfield struct.
91 /// /// Fields can have their getters and setters visibility overridden by specifying
92 /// /// the visibility of the field.
93 /// pub pub_field: u8, // Getter and setter are public.
94 /// /// Nested bitfields are supported, but must have their bits specified.
95 /// #[bits(3)]
96 /// nested_field: NestedBitfield,
97 /// /// Custom types are supported, but must have their bits specified and
98 /// /// implement the `from_bits` and `into_bits` functions.
99 /// #[bits(3)]
100 /// custom_type: CustomType,
101 /// /// Fields can have their access restricted. `ro` means read-only, meaning
102 /// /// the field can be read but not written.
103 /// #[bits(5, access = ro)] // Read-only field, no setter.
104 /// read_only: u8,
105 /// /// Fields prefixed with "_" are padding fields, which are inaccessible.
106 /// #[bits(4, default = 0x3)]
107 /// _padding: u8,
108 /// /// Fields with the ignore attribute are ignored.
109 /// #[bits(99, ignore = true)]
110 /// ignore_me: u128,
111 /// }
112 ///
113 /// #[bitfield(u8)]
114 /// struct NestedBitfield {
115 /// field: u8
116 /// }
117 ///
118 /// /// Custom types must have 2 const functions, `from_bits` and `into_bits` to convert
119 /// /// the type to and from bits functions.
120 /// #[derive(Default)]
121 /// struct CustomType {
122 /// a: u8,
123 /// }
124 ///
125 /// impl CustomType {
126 /// /// Make sure the parameter type can fit the specified number of bits. Also,
127 /// /// must be const, we need that extra compile time safety.
128 /// const fn from_bits(bits: u8) -> Self {
129 /// Self {
130 /// a: bits,
131 /// }
132 /// }
133 ///
134 /// /// Make sure the return type can fit the specified number of bits. Also,
135 /// /// must be const, we need that extra compile time safety.
136 /// const fn into_bits(self) -> u8 {
137 /// self.a
138 /// }
139 /// }
140 ///
141 /// // Usage:
142 /// // Creates a new bitfield using a builder pattern, unset fields default to 0
143 /// // or their provided default value.
144 /// let mut bitfield = BitfieldBuilder::new()
145 /// .with_u8int(5)
146 /// .with_small_u8int(0xF)
147 /// .with_custom_type(CustomType::from_bits(0x3))
148 /// // .with_custom_type(CustomType::default()) // Can pass a [`CustomType`] instance.
149 /// .with_read_only(0x3) // Read-only field can only be set during construction.
150 /// // .with__padding(0x3) // Compile error, padding fields are inaccessible.
151 /// .with_signed_int(-5)
152 /// .with_small_signed_int(0xF)
153 /// .build();
154 ///
155 /// // let bitfield = Bitfield::new(); // Bitfield with default values.
156 /// // let bitfield = Bitfield::new_without_defaults(); // Bitfield without default values.
157 /// // let bitfield = BitfieldBuilder::new_without_defaults(); // Builder without defaults.
158 ///
159 /// // let builder = bitfield.to_builder(); // Convert a bitfield back to builder, requires
160 /// // `#[bitfield(to_builder = true)]` and `#[derive(Clone)]` on the bitfield.
161 ///
162 /// // Accessing fields:
163 /// let u8int = bitfield.u8int(); // Getters
164 /// let small_u8int = bitfield.small_u8int(); // Signed-types are sign-extended.
165 /// bitfield.ignore_me; // Ignored fields can be accessed directly.
166 /// // bitfield.set_read_only(0x3); // Compile error, read-only fields can't be set.
167 ///
168 /// // Setting fields:
169 /// bitfield.set_u8int(0x3); // Setters
170 /// bitfield.checked_set_small_u8int(0xF); // Checked setter, error if value overflow bits.
171 ///
172 /// // Converting to bits:
173 /// let bits = bitfield.into_bits();
174 ///
175 /// // Converting from bits:
176 /// let mut bitfield = Bitfield::from_bits(0x3); // Converts from bits
177 /// // let bitfield = Bitfield::from_bits_with_defaults(0x3); // Converts, respects defaults.
178 ///
179 /// // Set and clear bitfield:
180 /// bitfield.set_bits(0x12345678); // Sets the bitfield.
181 /// bitfield.set_bits_with_defaults(0x12345678); // Sets the bitfield, respects
182 /// defaults.
183 ///
184 /// bitfield.clear_bits(); // Clears the bitfield.
185 /// bitfield.clear_bits_with_defaults(); // Clears the bitfield, respects
186 /// defaults.
187 ///
188 /// // Constants:
189 /// assert_eq!(Bitfield::U8INT_BITS, 8); // Number of bits of the field.
190 /// assert_eq!(Bitfield::U8INT_OFFSET, 0); // The offset of the field in the
191 /// bitfield.
192 /// ```
193 ///
194 /// ## Features
195 ///
196 /// ### Bitfield Types
197 ///
198 /// A bitfield can represent unsigned types (`u8`, `u16`, `u32`, `u64`, `u128`)
199 /// up to 128-bits, because Rust was weak and stopped at `u128`. The field bits
200 /// of a bitfield must add up to the number of bits of the bitfield type.
201 ///
202 /// ```ignore
203 /// use bitfields::bitfield;
204 ///
205 /// #[bitfield(u8)]
206 /// struct BitFieldU8 {
207 /// a: u8,
208 /// }
209 ///
210 /// #[bitfield(u32)]
211 /// struct BitFieldU32 {
212 /// a: u32,
213 /// }
214 ///
215 /// #[bitfield(u128)]
216 /// struct BitFieldU128 {
217 /// a: u128,
218 /// }
219 /// ```
220 ///
221 /// ### Bitfield Field Types
222 ///
223 /// A bitfield field can be any unsigned (`u8`, `u16`, `u32`, `u64`, `u128`),
224 /// signed type (`i8`, `i16`, `i32`, `i64`, `i128`), or a custom type that
225 /// implements the const functions `from_bits` and `into_bits`. A default value
226 /// can also be a const variable or a const function. Just be aware that const
227 /// function and variables defaults lose their compile-time field bits checking.
228 ///
229 /// Signed types are treated as 2's complement data types, meaning the most
230 /// significant represents the sign bit. For example, if you had a field with 5
231 /// bits, the value range would be `-16` to `15`. The more bits you include, the
232 /// larger the value range.
233 ///
234 /// ```ignore
235 /// use bitfields::bitfield;
236 ///
237 /// const CONST_VAR: u8 = 0x2;
238 ///
239 /// const fn provide_val() -> u8 {
240 /// 0x1
241 /// }
242 ///
243 /// #[bitfield(u32)]
244 /// struct Bitfield {
245 /// #[bits(default = 0xFF)]
246 /// a: u8,
247 /// #[bits(default = -127)]
248 /// b: i8,
249 /// /// Sign-extended by the most significant bit of 4 bits. Also treated as 2's
250 /// /// complement, meaning this field with 4 bits has the value range of `-8` to `7`.
251 /// /// You can add more bits to increase this range!
252 /// #[bits(4, default = 9)]
253 /// c_sign_extended: i8,
254 /// #[bits(2, default = CONST_VAR)] // No compile time checks for const variables.
255 /// const_var_default: u8,
256 /// #[bits(2, default = provide_val())] // No compile time checks for const functions.
257 /// const_fn_default: u8, // No compile time checks for const functions.
258 /// #[bits(8, default = CustomType::C)]
259 /// custom_type: CustomType
260 /// }
261 ///
262 /// #[derive(Debug, PartialEq)]
263 /// enum CustomType {
264 /// A = 0,
265 /// B = 1,
266 /// C = 2,
267 /// }
268 ///
269 /// impl CustomType {
270 /// const fn from_bits(bits: u8) -> Self {
271 /// match bits {
272 /// 0 => Self::A,
273 /// 1 => Self::B,
274 /// 2 => Self::C,
275 /// _ => unreachable!(),
276 /// }
277 /// }
278 ///
279 /// const fn into_bits(self) -> u8 {
280 /// self as u8
281 /// }
282 /// }
283 ///
284 /// let bitfield = Bitfield::new();
285 /// assert_eq!(bitfield.a(), 0xFF);
286 /// assert_eq!(bitfield.b(), -127);
287 /// assert_eq!(bitfield.c_sign_extended(), -7);
288 /// assert_eq!(bitfield.const_var_default(), 0x2);
289 /// assert_eq!(bitfield.const_fn_default(), 0x1);
290 /// assert_eq!(bitfield.custom_type(), CustomType::C);
291 /// ```
292 ///
293 /// ### Constructing a Bitfield
294 ///
295 /// A bitfield can be constructed using the `new` and `new_without_defaults`
296 /// constructors. The former initializes the bitfield with default values, while
297 /// the latter initializes the bitfield without default values, except for
298 /// padding fields which always keep their default value or 0.
299 ///
300 /// A bitfield can also be constructed using a fluent builder pattern using the
301 /// `<Bitfield>Builder::new` and `<Bitfield>Builder::new_without_defaults`
302 /// constructors. They operate the same as the `new` and `new_without_defaults`
303 /// constructors.
304 ///
305 /// ```ignore
306 /// use bitfields::bitfield;
307 ///
308 /// #[bitfield(u32)]
309 /// struct Bitfield {
310 /// #[bits(default = 0x12)]
311 /// a: u8,
312 /// #[bits(default = 0x34)]
313 /// b: u8,
314 /// #[bits(default = 0x56)]
315 /// c: u8,
316 /// #[bits(default = 0x78)]
317 /// _d: u8,
318 /// }
319 ///
320 /// let bitfield = Bitfield::new();
321 /// assert_eq!(bitfield.a(), 0x12);
322 /// assert_eq!(bitfield.b(), 0x34);
323 /// assert_eq!(bitfield.c(), 0x56);
324 /// assert_eq!(bitfield.into_bits(), 0x78563412);
325 ///
326 /// let bitfield_without_defaults = Bitfield::new_without_defaults();
327 /// assert_eq!(bitfield_without_defaults.a(), 0);
328 /// assert_eq!(bitfield_without_defaults.b(), 0);
329 /// assert_eq!(bitfield_without_defaults.c(), 0);
330 /// assert_eq!(bitfield_without_defaults.into_bits(), 0x78000000);
331 ///
332 /// let bitfield = BitfieldBuilder::new()
333 /// .with_a(0x12)
334 /// .with_b(0x34)
335 /// .with_c(0x56)
336 /// .build();
337 /// assert_eq!(bitfield.a(), 0x12);
338 /// assert_eq!(bitfield.b(), 0x34);
339 /// assert_eq!(bitfield.c(), 0x56);
340 /// assert_eq!(bitfield.into_bits(), 0x78563412);
341 /// ```
342 ///
343 /// ### To Builder
344 ///
345 /// A constructed bitfield can be converted back to a builder using the
346 /// `to_builder` function which is enabled using the `#[bitfield(to_builder =
347 /// true)]` attribute arg. The bitfield must also derive `Clone` to support this
348 /// feature.
349 ///
350 /// ```ignore
351 /// use bitfields::bitfield;
352 ///
353 /// #[bitfield(u32, to_builder = true)]
354 /// #[derive(Clone)]
355 /// struct Bitfield {
356 /// #[bits(default = 0x12)]
357 /// a: u8,
358 /// #[bits(default = 0x34)]
359 /// b: u8,
360 /// #[bits(default = 0x56)]
361 /// c: u8,
362 /// #[bits(default = 0x78)]
363 /// _d: u8,
364 /// }
365 ///
366 /// let bitfield = Bitfield::new();
367 ///
368 /// let bitfield_builder = bitfield.to_builder();
369 /// ```
370 ///
371 /// ### Setting and Clearing a Bitfield
372 ///
373 /// You are able to set and clear a bitfield using the `set_bits` and
374 /// `clear_bits` functions.
375 ///
376 /// ```ignore
377 /// use bitfields::bitfield;
378 ///
379 /// #[bitfield(u32)]
380 /// struct Bitfield {
381 /// #[bits(default = 0x12)]
382 /// a: u8,
383 /// #[bits(default = 0x34)]
384 /// b: u8,
385 /// c: u8,
386 /// #[bits(default = 0x78)]
387 /// _d: u8, // Padding fields are respected.
388 /// }
389 ///
390 /// let mut bitfield = Bitfield::new();
391 /// bitfield.set_bits(0x11223344);
392 /// assert_eq!(bitfield.into_bits(), 0x78223344);
393 ///
394 /// let mut bitfield = Bitfield::new();
395 /// bitfield.set_bits_with_defaults(0x11223344);
396 /// assert_eq!(bitfield.into_bits(), 0x78223412);
397 ///
398 /// let mut bitfield = Bitfield::new();
399 /// bitfield.clear_bits();
400 /// assert_eq!(bitfield.into_bits(), 0x78000000);
401 ///
402 /// let mut bitfield = Bitfield::new();
403 /// bitfield.clear_bits_with_defaults();
404 /// assert_eq!(bitfield.into_bits(), 0x78003412);
405 /// ```
406 ///
407 /// ### Bitfield Conversions
408 ///
409 /// A bitfield can be converted from bits using the `from_bits` or
410 /// `from_bits_with_defaults` functions. The former ignores default values,
411 /// while the latter respects them. Padding fields are always 0 or their default
412 /// value. The bitfield can also be converted to bits using the `into_bits`
413 /// function. The `From` trait is also implemented between the bitfield and the
414 /// bitfield type and operates the same as `from_bits`.
415 ///
416 /// ```ignore
417 /// use bitfields::bitfield;
418 ///
419 /// #[bitfield(u32)]
420 /// #[derive(Copy, Clone)]
421 /// struct Bitfield {
422 /// #[bits(default = 0x12)]
423 /// a: u8,
424 /// #[bits(8)]
425 /// b: CustomType,
426 /// c: u8,
427 /// #[bits(default = 0x78)]
428 /// _d: u8,
429 /// }
430 ///
431 /// #[derive(Debug, PartialEq)]
432 /// enum CustomType {
433 /// A = 0,
434 /// B = 1,
435 /// C = 2,
436 /// }
437 ///
438 /// impl CustomType {
439 /// const fn from_bits(bits: u8) -> Self {
440 /// match bits {
441 /// 1 => Self::A,
442 /// 2 => Self::B,
443 /// 3 => Self::C,
444 /// _ => Self::A,
445 /// }
446 /// }
447 ///
448 /// const fn into_bits(self) -> u8 {
449 /// self as u8
450 /// }
451 /// }
452 ///
453 /// let bitfield = Bitfield::from_bits(0x11223344);
454 /// assert_eq!(bitfield.a(), 0x44);
455 /// assert_eq!(bitfield.b(), CustomType::A);
456 /// assert_eq!(bitfield.c(), 0x22);
457 /// let val = bitfield.into_bits();
458 /// assert_eq!(val, 0x78220044);
459 ///
460 /// let bitfield_respect_defaults =
461 /// Bitfield::from_bits_with_defaults(0x11223344);
462 /// assert_eq!(bitfield_respect_defaults.a(), 0x12); // Default value respected
463 /// assert_eq!(bitfield_respect_defaults.b(), CustomType::A);
464 /// assert_eq!(bitfield_respect_defaults.c(), 0x22);
465 /// let val = bitfield_respect_defaults.into_bits();
466 /// assert_eq!(val, 0x78220012);
467 ///
468 /// // From trait
469 /// let val: u32 = bitfield.into();
470 /// assert_eq!(val, 0x78220044);
471 /// let bitfield: Bitfield = val.into();
472 /// assert_eq!(bitfield.into_bits(), 0x78220044);
473 /// ```
474 ///
475 /// ### Conversion Endianess
476 ///
477 /// Sometimes the outside world is outside our control, like how systems store
478 /// or expect data endian. Luckily, the endian of the bitfield conversions can
479 /// be controlled by specifying the `#[bitfield(from_endian = x, into_endian =
480 /// x)]` args. The possible endians are `little` or `big`. By default, the
481 /// endian of both is `big`.
482 ///
483 /// ````ignore
484 /// use bitfields::bitfield;
485 ///
486 /// // We are working with a system that stores data in little-endian, we
487 /// // set the from_endian to little for the proper representation.
488 /// //
489 /// // The system expects the data it stores in big-endian, we set the
490 /// // into_endian to big-endian for converting into the proper representation.
491 /// #[bitfield(u32, from_endian = little, into_endian = big)]
492 /// pub struct Bitfield {
493 /// a: u8,
494 /// b: u8,
495 /// c: u8,
496 /// d: u8,
497 /// }
498 ///
499 /// // The host device stored the data 0x12345678 in little-endian memory
500 /// // as [0x78, 0x56, 0x34, 0x12].
501 /// let bitfield = Bitfield::from_bits(0x78563412);
502 ///
503 /// assert_eq!(bitfield.a(), 0x78);
504 /// assert_eq!(bitfield.b(), 0x56);
505 /// assert_eq!(bitfield.c(), 0x34);
506 /// assert_eq!(bitfield.d(), 0x12);
507 /// assert_eq!(bitfield.into_bits(), 0x12345678);
508 /// ````
509 ///
510 /// ### Field Order
511 ///
512 /// By default, fields are ordered from the least significant bit (lsb) to the
513 /// most significant bit (msb). The order can be changed by specifying the
514 /// `#[bitfield(order = x)]` arg on the bitfield struct. There are two field
515 /// orderings, `lsb` and `msb`.
516 ///
517 /// ```ignore
518 /// use bitfields::bitfield;
519 ///
520 /// #[bitfield(u32, order = msb)]
521 /// struct Bitfield {
522 /// #[bits(default = 0x12)]
523 /// a: u8,
524 /// #[bits(default = 0x34)]
525 /// b: u8,
526 /// #[bits(default = 0x56)]
527 /// c: u8,
528 /// #[bits(default = 0x78)]
529 /// d: u8,
530 /// }
531 ///
532 /// let bitfield = Bitfield::new();
533 /// assert_eq!(bitfield.a(), 0x12);
534 /// assert_eq!(bitfield.b(), 0x34);
535 /// assert_eq!(bitfield.c(), 0x56);
536 /// assert_eq!(bitfield.d(), 0x78);
537 /// let val = bitfield.into_bits();
538 ///
539 /// // .- a
540 /// // | .- b
541 /// // | | .- c
542 /// // | | | .- d
543 /// assert_eq!(val, 0x12_34_56_78);
544 /// assert_eq!(Bitfield::A_OFFSET, 24); // Offset of the a field in the
545 /// bitfield.
546 /// ```
547 ///
548 /// ### Field Access
549 ///
550 /// Field access can be controlled by specifying the `#[bits(access = x)]` arg
551 /// on a field. There are four accesses:
552 /// - `rw` - Read and write access (default)
553 /// - `ro` - Read-only access, only set during construction or from bits.
554 /// - `wo` - Write-only access.
555 /// - `none` - No access.
556 ///
557 /// ```ignore
558 /// use bitfields::bitfield;
559 ///
560 /// #[bitfield(u32)]
561 /// struct Bitfield {
562 /// read_write: u8,
563 /// #[bits(access = ro)]
564 /// read_only: u8,
565 /// #[bits(access = wo)]
566 /// write_only: u8,
567 /// #[bits(default = 0xFF, access = none)]
568 /// none: u8,
569 /// }
570 ///
571 /// let mut bitfield = BitfieldBuilder::new()
572 /// .with_read_write(0x12)
573 /// .with_read_only(0x34) // Read-only fields only set during construction or from bits.
574 /// .with_write_only(0x56)
575 /// // .with_none(0x78) // Compile error, none field can't be set.
576 /// .build();
577 /// bitfield.set_read_write(0x12);
578 /// // bitfield.set_read_only(1); // Compile error, read-only field can't be set, after construction.
579 /// bitfield.set_write_only(0x56);
580 /// // bitfield.set_none(0x78); // Compile error, none field can't be set.
581 ///
582 /// assert_eq!(bitfield.read_write(), 0x12);
583 /// assert_eq!(bitfield.read_only(), 0x34);
584 /// // assert_eq!(bitfield.write_only(), 0x56); // Compile error, write-only can't be read.
585 /// // assert_eq!(bitfield.none(), 0xFF); // Compile error, none field can't be accessed.
586 /// assert_eq!(bitfield.into_bits(), 0xFF563412); // All fields exposed when converted to bits.
587 /// ```
588 ///
589 /// ### Checked Setters
590 ///
591 /// Normally, when a field is set, the value is truncated to the number of bits
592 /// of the field. Fields also have checked setters that returns an error if the
593 /// value overflows the number of bits of the field.
594 ///
595 /// ```ignore
596 /// use bitfields::bitfield;
597 ///
598 /// #[bitfield(u16)]
599 /// struct Bitfield {
600 /// a: u8,
601 /// #[bits(4)]
602 /// b: u8,
603 /// #[bits(4)]
604 /// _padding: u8,
605 /// }
606 ///
607 /// let mut bitfield = Bitfield::new();
608 /// bitfield.set_a(0xFF);
609 /// bitfield.set_b(0x12); // Truncated to 4 bits.
610 /// assert_eq!(bitfield.a(), 0xFF);
611 /// assert_eq!(bitfield.b(), 0x2);
612 ///
613 /// let res = bitfield.checked_set_b(0x12); // Error, value overflows bits.
614 /// assert!(res.is_err());
615 /// ```
616 ///
617 /// ### Bit Operations
618 ///
619 /// Individual bits can be get or set using the `get_bit` and `set_bit`
620 /// functions. They can be enabled using the bitfield attribute arg
621 /// `#[bitfield(bit_ops = true)]` When calling `get_bit`, if the bit is
622 /// out-of-bounds or the field doesn't have write access, `false` is returned.
623 /// There is a checked version `checked_get_bit` that return an error instead.
624 /// Similarly, for `set_bit`, if the bit is out-of-bounds or the field doesn't
625 /// have write access, the operation is no-op. There is a checked version
626 /// `checked_set_bit` that returns an error instead.
627 ///
628 /// ```ignore
629 /// use bitfields::bitfield;
630 ///
631 /// #[bitfield(u8, bit_ops = true)]
632 /// #[derive(Copy, Clone)]
633 /// pub struct Bitfield {
634 /// #[bits(2, default = 0b11)]
635 /// a: u8,
636 /// #[bits(2, default = 0b00)]
637 /// b: u8,
638 /// #[bits(2, default = 0b10, access = wo)]
639 /// c: u8,
640 /// #[bits(2, default = 0b01)]
641 /// _d: u8,
642 /// }
643 ///
644 /// let bitfield = Bitfield::new();
645 ///
646 /// assert!(bitfield.get_bit(0));
647 /// assert!(bitfield.get_bit(1));
648 /// assert!(!bitfield.get_bit(2));
649 /// assert!(!bitfield.get_bit(3));
650 /// assert!(bitfield.get_bit(4)); // No write access, false is returned.
651 /// assert!(bitfield.get_bit(5)); // No write access, false is returned.
652 /// assert!(bitfield.checked_get_bit(4).is_err()); // No write access, err.
653 /// assert!(bitfield.checked_get_bit(5).is_err()); // No write access, err.
654 /// assert!(bitfield.get_bit(6));
655 /// assert!(!bitfield.get_bit(7));
656 /// assert!(bitfield.get_bit(50)); // Out-of-bounds, false is returned.
657 /// assert!(bitfield.checked_get_bit(50).is_err()); // Out-of-bounds, err.
658 /// ```
659 ///
660 /// ```ignore
661 /// #[bitfield(u8, bit_ops = true)]
662 /// #[derive(Copy, Clone)]
663 /// pub struct Bitfield {
664 /// #[bits(2)]
665 /// a: u8,
666 /// #[bits(2, default = 0b11)]
667 /// b: u8,
668 /// #[bits(2, default = 0b11, access = ro)]
669 /// c: u8,
670 /// #[bits(2, default = 0b00)]
671 /// _d: u8,
672 /// }
673 ///
674 /// let mut bitfield = Bitfield::new();
675 ///
676 /// bitfield.set_bit(0, true);
677 /// bitfield.set_bit(1, true);
678 /// bitfield.set_bit(2, false);
679 /// bitfield.set_bit(3, false);
680 /// bitfield.set_bit(4, false); // No-op, no write access.
681 /// bitfield.set_bit(5, false); // No-op, no write access.
682 /// assert!(bitfield.checked_set_bit(4, false).is_err()); // Error, no write access.
683 /// assert!(bitfield.checked_set_bit(5, false).is_err()); // Error, no write access.
684 /// bitfield.set_bit(6, true); // No-op, padding.
685 /// bitfield.set_bit(7, true); // No-op, padding.
686 /// assert!(bitfield.checked_set_bit(4, false).is_err()); // Error, padding.
687 /// assert!(bitfield.checked_set_bit(5, false).is_err()); // Error, padding.
688 /// assert_eq!(bitfield.into_bits(), 0b110011);
689 /// ```
690 ///
691 /// ### Padding Fields
692 ///
693 /// Fields prefixed with an underscore `_` are padding fields, which are
694 /// inaccessible. Meaning the field is always 0/false or a default value. They
695 /// are useful for padding the bits of the bitfield.
696 ///
697 /// ```ignore
698 /// use bitfields::bitfield;
699 ///
700 /// #[bitfield(u16)]
701 /// struct Bitfield {
702 /// a: u8,
703 /// #[bits(default = 0xFF)]
704 /// _padding: u8, // Fills the remaining bits of the u16.
705 /// }
706 ///
707 /// let bitfield = Bitfield::new();
708 /// assert_eq!(bitfield.a(), 0);
709 /// // assert_eq!(bitfield._padding(), 0xFF00); // Compile error, padding inaccessible.
710 /// // bitfield.set__padding(0xFF); // Compile error, padding fields are inaccessible.
711 /// assert_eq!(bitfield.into_bits(), 0xFF00); // All fields exposed when converted to bits.
712 /// ```
713 ///
714 /// ### Inverting Bits
715 ///
716 /// Bitfields with the `#[bitfield(neg = true)` attribute, generates a `neg_<field>` getter function for each field
717 /// that inverts the field's bits.
718 ///
719 /// ```ignore
720 /// #[bitfield(u8, neg = true)]
721 /// struct Bitfield {
722 /// #[bits(5, default = 0xC)]
723 /// a: u8,
724 /// #[bits(1, default = true)]
725 /// b: bool,
726 /// #[bits(2)]
727 /// _padding: u8,
728 /// }
729 ///
730 /// let builder = Bitfield::new();
731 ///
732 /// assert_eq!(builder.a(), 0xC);
733 /// assert!(builder.b());
734 ///
735 /// // Inverted bits
736 /// assert_eq!(builder.neg_a(), 0x13);
737 /// assert!(!builder.neg_b());
738 /// ```
739 ///
740 /// ### Ignored Fields
741 ///
742 /// Fields with the `#[bits(ignore = true)` attribute are ignored and not
743 /// included in the bitfield. This is useful for when you are building a custom
744 /// bitfield, but want to include certain fields that aren't a part of the
745 /// bitfield without wrapping having to wrap bitfield is a parent struct. All
746 /// ignored fields must implement the `Default` trait. Ignored fields
747 /// are accessible directly like normal struct fields.
748 ///
749 /// ```ignore
750 /// use bitfields::bitfield;
751 ///
752 /// #[bitfield(u16)]
753 /// struct Bitfield {
754 /// a: u8,
755 /// b: u8,
756 /// #[bits(ignore = true)] // Ignored field.
757 /// field_id: u8,
758 /// #[bits(ignore = true)] // Ignored field.
759 /// field_custom: CustomType,
760 /// }
761 ///
762 /// #[derive(Debug, Default, PartialEq)]
763 /// enum CustomType {
764 /// #[default]
765 /// A,
766 /// B,
767 /// }
768 ///
769 /// let bitfield = Bitfield::new();
770 ///
771 /// assert_eq!(bitfield.field_id, 0); // Ignored fields can be accessed
772 /// directly. assert_eq!(bitfield.field_custom, CustomType::A); // Ignored
773 /// fields can be accessed directly.
774 /// ```
775 ///
776 /// ### Field Constants
777 ///
778 /// Fields with read or write access have constants generated for their number
779 /// of bits and offset in the bitfield.
780 ///
781 /// ```ignore
782 /// use bitfields::bitfield;
783 ///
784 /// #[bitfield(u32)]
785 /// struct Bitfield {
786 /// #[bits(default = 0x12)]
787 /// a: u8,
788 /// #[bits(default = 0x34)]
789 /// b: u8,
790 /// #[bits(default = 0x56)]
791 /// c: u8,
792 /// #[bits(default = 0x78)]
793 /// d: u8,
794 /// }
795 ///
796 /// assert_eq!(Bitfield::A_BITS, 8); // Number of bits of the afield.
797 /// assert_eq!(Bitfield::A_OFFSET, 0); // The offset of the a field in the
798 /// bitfield. assert_eq!(Bitfield::B_BITS, 8); // Number of bits of the b field.
799 /// assert_eq!(Bitfield::B_OFFSET, 8); // The offset of the b field in the
800 /// bitfield. assert_eq!(Bitfield::C_BITS, 8); // Number of bits of c the field.
801 /// assert_eq!(Bitfield::C_OFFSET, 16); // The offset of the c field in the
802 /// bitfield. assert_eq!(Bitfield::D_BITS, 8); // Number of bits of the d field.
803 /// assert_eq!(Bitfield::D_OFFSET, 24); // The offset of the d field in the
804 /// bitfield.
805 /// ```
806 ///
807 /// ### Debug Implementation
808 ///
809 /// A debug implementation is generated for the bitfield, which prints the
810 /// fields and their values.
811 ///
812 /// ```ignore
813 /// use bitfields::bitfield;
814 ///
815 /// #[bitfield(u32)]
816 /// struct Bitfield {
817 /// #[bits(default = 0x12)]
818 /// a: u8,
819 /// #[bits(default = 0x34)]
820 /// b: u8,
821 /// #[bits(default = 0x56)]
822 /// c: u8,
823 /// #[bits(default = 0x78)]
824 /// d: u8,
825 /// }
826 ///
827 /// let bitfield = Bitfield::new();
828 ///
829 /// assert_eq!(format!("{:?}", bitfield), "Bitfield { d: 120, c: 86, b: 52, a: 18 }"); ```
830 ///
831 /// ### Passing Attributes
832 ///
833 /// Attributes below the `#[bitfield]` attribute are passed to the generated
834 /// struct.
835 /// ```ignore
836 /// use bitfields::bitfield;
837 ///
838 /// #[bitfield(u32)]
839 /// #[derive(Copy, Clone)]
840 /// struct Bitfield {
841 /// a: u32,
842 /// }
843 /// ```
844 ///
845 /// ### Complete Generation Control
846 ///
847 /// You have complete control over what gets generated by the bitfield macro.
848 /// When your deploying to a resource-constrained environment, you can generate
849 /// only the necessary functions or implementations. You can disable generation
850 /// by passing `false` to its attribute arg.
851 ///
852 /// The `#[bitfield]` args that control generation are:
853 ///
854 /// - `#[bitfield(new = true)]` - Generates the `new` and `new_without_defaults`
855 /// constructor.
856 /// - `#[bitfield(from_bits = true)]` - Generates the `from_bits` and
857 /// `from_bits_with_defaults` functions.
858 /// - `#[bitfield(into_bits = true)]` - Generates the `into_bits` function.
859 /// - `#[bitfield(from = true)]` - Generates the `From` trait implementation.
860 /// - `#[bitfield(debug = true)]` - Generates the `Debug` trait implementation.
861 /// - `#[bitfield(default = true)]` - Generates the `Default` trait
862 /// implementation
863 /// - `#[bitfield(builder = true)]` - Generates the builder implementation.
864 /// - `#[bitfield(set_bits = true)]` - Generates the `set_bits` function.
865 /// - `#[bitfield(clear_bits = true)]` - Generates the `clear_bits` function.
866 /// - `#[bitfield(bit_ops = true)]` - Generates the bit operations
867 /// implementation.
868 /// - `#[bitfield(to_builder = true)]` - Generates the `to_builder` function.
869 /// - `#[bitfield(neg = true)]` - Generates `neg_<field>` getter functions for each field.
870 #[proc_macro_attribute]
871 pub fn bitfieldrust-analyzer cargo bitfields-impl 1.0.0 bitfield:(
872 args: proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#,
873 input: proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream#,
874 ) -> proc_macrorust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro crate/::TokenStreamrust-analyzer cargo proc_macro https://github.com/rust-lang/rust/library/proc_macro TokenStream# {
875 match parse_bitfieldrust-analyzer cargo bitfields-impl 1.0.0 parse_bitfield().(args.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), input.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) {
876 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(res) => res.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
877 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => err.into_compile_errorrust-analyzer cargo syn 2.0.104 error/impl#[Error]into_compile_error().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
878 }
879 }
880
881 /// Parses the bitfield attribute, struct, and fields.
882 fn parse_bitfieldrust-analyzer cargo bitfields-impl 1.0.0 parse_bitfield().(args: TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#, input: TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#) -> synrust-analyzer cargo syn 2.0.104 crate/::Resultrust-analyzer cargo syn 2.0.104 error/Result#<TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#> {
883 // Parse the struct tokens
884 let struct_tokens = synrust-analyzer cargo syn 2.0.104 crate/::parse2rust-analyzer cargo syn 2.0.104 parse2().::<synrust-analyzer cargo syn 2.0.104 crate/::ItemStructrust-analyzer cargo syn 2.0.104 item/ItemStruct#>(input.clonerust-analyzer cargo proc-macro2 1.0.95 impl#[TokenStream][Clone]clone().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
885
886 // Parse the arguments of the '#[bitfield(arg, arg)]' attribute
887 let bitfield_attribute: BitfieldAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute# = match synrust-analyzer cargo syn 2.0.104 crate/::parse2rust-analyzer cargo syn 2.0.104 parse2().(args) {
888 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bitfield_attribute) => bitfield_attribute,
889 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
890 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(input.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(), err.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()));
891 }
892 };
893
894 // Check if the bitfield type can contain the fields.
895 let all_fields = parse_fieldsrust-analyzer cargo bitfields-impl 1.0.0 parse_fields().(&bitfield_attribute, &struct_tokens)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
896 let fields = all_fields.0;
897 let ignored_fields = all_fields.1;
898 check_bitfield_type_contain_field_bitsrust-analyzer cargo bitfields-impl 1.0.0 check_bitfield_type_contain_field_bits().(&bitfield_attribute, &fields)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
899 check_bitfield_names_uniquerust-analyzer cargo bitfields-impl 1.0.0 check_bitfield_names_unique().(&fields)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
900
901 // Generate the bitfield functions.
902 generate_functionsrust-analyzer cargo bitfields-impl 1.0.0 generate_functions().(&bitfield_attribute, &fields, &ignored_fields, &struct_tokens)
903 }
904
905 /// Check if the bitfield type can contain the field bits.
906 fn check_bitfield_type_contain_field_bitsrust-analyzer cargo bitfields-impl 1.0.0 check_bitfield_type_contain_field_bits().(
907 bitfield_attribute: &BitfieldAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#,
908 fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#],
909 ) -> synrust-analyzer cargo syn 2.0.104 crate/::Resultrust-analyzer cargo syn 2.0.104 error/Result#<()> {
910 let total_field_bits = 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| field.bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#bits.).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<u8>();
911
912 match total_field_bits.cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][Ord]cmp().(&bitfield_attribute.bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#bits.) {
913 Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Greaterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Greater# => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
914 bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty..spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
915 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
916 "The total number of bits of the fields ({} bits) is greater than the number of bits of the bitfield type '{}' ({} bits).",
917 total_field_bits,
918 get_type_identrust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_type_ident().(&bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
919 bitfield_attribute.bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#bits.
920 ),
921 )),
922 Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Lessrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Less# => {
923 let remaining_bits = bitfield_attribute.bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#bits. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().total_field_bits;
924 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
925 bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty..spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
926 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
927 "The total number of bits of the fields ({} bits) is less than the number of bits of the bitfield type '{}' ({} bits), you can add a padding field (prefixed with '_') to fill the remaining '{} bits'.",
928 total_field_bits,
929 get_type_identrust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_type_ident().(&bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
930 bitfield_attribute.bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#bits.,
931 remaining_bits,
932 ),
933 ))
934 }
935 Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#::Equalrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#Equal# => {
936 // The total number of bits of all fields is equal to the number of bits, we're
937 // good.
938 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
939 }
940 }
941 }
942
943 fn check_bitfield_names_uniquerust-analyzer cargo bitfields-impl 1.0.0 check_bitfield_names_unique().(fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#]) -> synrust-analyzer cargo syn 2.0.104 crate/::Resultrust-analyzer cargo syn 2.0.104 error/Result#<()> {
944 let mut field_names = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
945 for field in fields {
946 if field_names.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]contains().(&field.namerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#name.) {
947 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
948 field.namerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#name..spanrust-analyzer cargo proc-macro2 1.0.95 impl#[Ident]span().(),
949 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
950 "The field name '{}' is duplicated, each field must have a unique name.",
951 field.namerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#name.
952 ),
953 ));
954 }
955 if !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. {
956 field_names.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(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().());
957 }
958 }
959
960 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
961 }
962
963 /// Parses all the fields into a list of [`BitfieldField`]s.
964 fn parse_fieldsrust-analyzer cargo bitfields-impl 1.0.0 parse_fields().(
965 bitfield_attribute: &BitfieldAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#,
966 struct_tokens: &synrust-analyzer cargo syn 2.0.104 crate/::ItemStructrust-analyzer cargo syn 2.0.104 item/ItemStruct#,
967 ) -> synrust-analyzer cargo syn 2.0.104 crate/::Resultrust-analyzer cargo syn 2.0.104 error/Result#<(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#>, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#>)> {
968 let fields_tokens = match &struct_tokens.fieldsrust-analyzer cargo syn 2.0.104 item/ItemStruct#fields. {
969 Fieldsrust-analyzer cargo syn 2.0.104 data/Fields#::Namedrust-analyzer cargo syn 2.0.104 data/Fields#Named#(named_files) => named_files,
970 _ => {
971 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
972 struct_tokens.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
973 "Non-named fields are not supported.",
974 ));
975 }
976 };
977
978 let mut fields = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
979 let mut ignored_fields = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
980 for field_token in fields_tokens.namedrust-analyzer cargo syn 2.0.104 data/FieldsNamed#named..clonerust-analyzer cargo syn 2.0.104 punctuated/impl#[`Punctuated<T, P>`][Clone]clone().() {
981 let field = do_parse_fieldrust-analyzer cargo bitfields-impl 1.0.0 do_parse_field().(bitfield_attribute, field_token, &fields)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
982 if field.ignorerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#ignore. {
983 ignored_fields.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(field);
984 } else {
985 fields.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(field);
986 }
987 }
988
989 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((fields, ignored_fields))
990 }
991
992 /// Internal implementation of [`parse_fields`] to parse a single field.
993 fn do_parse_fieldrust-analyzer cargo bitfields-impl 1.0.0 do_parse_field().(
994 bitfield_attribute: &BitfieldAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#,
995 field_tokens: synrust-analyzer cargo syn 2.0.104 crate/::Fieldrust-analyzer cargo syn 2.0.104 data/Field#,
996 prev_fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#],
997 ) -> synrust-analyzer cargo syn 2.0.104 crate/::Resultrust-analyzer cargo syn 2.0.104 error/Result#<BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#> {
998 // Parse field attribute, a field could have multiple attributes, but we only
999 // care about our 'bits' attribute.
1000 let field_bit_attribute = field_tokens.attrsrust-analyzer cargo syn 2.0.104 data/Field#attrs..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]find().(|attr| {
1001 attr.pathrust-analyzer cargo syn 2.0.104 attr/impl#[Attribute]path().().is_identrust-analyzer cargo syn 2.0.104 path/impl#[Path]is_ident().(BIT_ATTRIBUTE_NAMErust-analyzer cargo bitfields-impl 1.0.0 BIT_ATTRIBUTE_NAME.) && attr.stylerust-analyzer cargo syn 2.0.104 attr/Attribute#style. rust-analyzer cargo syn 2.0.104 gen/eq/impl#[`crate::AttrStyle`][`PartialEq<Self>`]eq().==rust-analyzer cargo syn 2.0.104 gen/eq/impl#[`crate::AttrStyle`][`PartialEq<Self>`]eq(). rust-analyzer cargo syn 2.0.104 gen/eq/impl#[`crate::AttrStyle`][`PartialEq<Self>`]eq().synrust-analyzer cargo syn 2.0.104 crate/::AttrStylerust-analyzer cargo syn 2.0.104 attr/AttrStyle#::Outerrust-analyzer cargo syn 2.0.104 attr/AttrStyle#Outer#
1002 });
1003
1004 let visibility = match field_tokens.visrust-analyzer cargo syn 2.0.104 data/Field#vis. {
1005 // Pass the visibility to the field.
1006 Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#::Publicrust-analyzer cargo syn 2.0.104 restriction/Visibility#Public#(_) | Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#::Restrictedrust-analyzer cargo syn 2.0.104 restriction/Visibility#Restricted#(_) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(field_tokens.visrust-analyzer cargo syn 2.0.104 data/Field#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().()),
1007 // Use the visibility of the struct
1008 Visibilityrust-analyzer cargo syn 2.0.104 restriction/Visibility#::Inheritedrust-analyzer cargo syn 2.0.104 restriction/Visibility#Inherited# => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
1009 };
1010
1011 let field_type = if is_custom_field_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/is_custom_field_type().(&field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty.) {
1012 FieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#::CustomFieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#CustomFieldType#
1013 } else {
1014 FieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#::IntegerFieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#IntegerFieldType#
1015 };
1016
1017 let padding =
1018 field_tokens.identrust-analyzer cargo syn 2.0.104 data/Field#ident..clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Clone]clone().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(PADDING_FIELD_NAME_PREFIXrust-analyzer cargo bitfields-impl 1.0.0 PADDING_FIELD_NAME_PREFIX.);
1019
1020 let bitfield = if field_bit_attribute.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() {
1021 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_supported_field_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/is_supported_field_type().(&field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty.) {
1022 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1023 field_tokens.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1024 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
1025 "The field type {:?} is not supported.",
1026 get_type_identrust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_type_ident().(&field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()
1027 ),
1028 ));
1029 }
1030
1031 // We have to determine the number of bits from the field type since there's no
1032 // '#[bits]' attribute.
1033 if is_size_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/is_size_type().(&field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty.) {
1034 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1035 field_tokens.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1036 "The types isize and usize require a bit size, otherwise we can't determine the size of the field.",
1037 ));
1038 }
1039
1040 if field_type rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().FieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#::IntegerFieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#IntegerFieldType# {
1041 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1042 field_tokens.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1043 "Custom and nested field types require a defined bit size, otherwise we can't determine the size of the field.",
1044 ));
1045 }
1046
1047 let bits = get_bits_from_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_bits_from_type().(&field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
1048 let offset = calculate_field_offsetrust-analyzer cargo bitfields-impl 1.0.0 calculate_field_offset().(bits, bitfield_attribute, prev_fields)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
1049 let access = if padding { FieldAccessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldAccess#::Nonerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldAccess#None# } else { FieldAccessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldAccess#::ReadWriterust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldAccess#ReadWrite# };
1050
1051 // Create a bitfield field with default values since we don't have one to
1052 // parse.
1053 BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField# {
1054 namerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#name.: field_tokens.identrust-analyzer cargo syn 2.0.104 data/Field#ident..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
1055 tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#ty.: field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Type`][Clone]clone().(),
1056 visrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#vis.: visibility,
1057 bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#bits.,
1058 offsetrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#offset.,
1059 default_value_tokensrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#default_value_tokens.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
1060 unsignedrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#unsigned.: true,
1061 paddingrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#padding.,
1062 accessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#access.,
1063 field_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#field_type.: FieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#::IntegerFieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#IntegerFieldType#,
1064 ignorerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#ignore.: false,
1065 }
1066 } else {
1067 let bit_attribute_tokens = match &field_bit_attribute.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().metarust-analyzer cargo syn 2.0.104 attr/Attribute#meta. {
1068 Metarust-analyzer cargo syn 2.0.104 attr/Meta#::Listrust-analyzer cargo syn 2.0.104 attr/Meta#List#(list) => list,
1069 _ => {
1070 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1071 field_tokens.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1072 "The '#[bits]' attribute must be a list.",
1073 ));
1074 }
1075 };
1076
1077 let bits_attribute: BitsAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitsAttribute# = synrust-analyzer cargo syn 2.0.104 crate/::parse2rust-analyzer cargo syn 2.0.104 parse2().(bit_attribute_tokens.tokensrust-analyzer cargo syn 2.0.104 attr/MetaList#tokens..clonerust-analyzer cargo proc-macro2 1.0.95 impl#[TokenStream][Clone]clone().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
1078
1079 if bits_attribute.ignorerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitsAttribute#ignore. {
1080 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField# {
1081 tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#ty.: field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Type`][Clone]clone().(),
1082 visrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#vis.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(field_tokens.visrust-analyzer cargo syn 2.0.104 data/Field#vis.),
1083 bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#bits.: 0,
1084 offsetrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#offset.: 0,
1085 default_value_tokensrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#default_value_tokens.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
1086 unsignedrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#unsigned.: false,
1087 paddingrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#padding.,
1088 accessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#access.: 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#,
1089 namerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#name.: field_tokens.identrust-analyzer cargo syn 2.0.104 data/Field#ident..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
1090 ignorerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#ignore.: true,
1091 field_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#field_type.,
1092 });
1093 }
1094
1095 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_supported_field_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/is_supported_field_type().(&field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty.) {
1096 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1097 field_tokens.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1098 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
1099 "The field type {:?} is not supported.",
1100 get_type_identrust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_type_ident().(&field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()
1101 ),
1102 ));
1103 }
1104
1105 let bits = match bits_attribute.bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitsAttribute#bits. {
1106 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(bits) => {
1107 // Make sure the type of the field can contain the specified number of bits if
1108 // not a custom type.
1109 if field_type rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[FieldType][`PartialEq<Self>`]eq().==rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[FieldType][`PartialEq<Self>`]eq(). rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[FieldType][`PartialEq<Self>`]eq().FieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#::IntegerFieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#IntegerFieldType#
1110 && bits rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]gt().get_bits_from_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_bits_from_type().(&field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
1111 {
1112 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1113 bit_attribute_tokens.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1114 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
1115 "The field type {:?} ({} bits) is too small to hold the specified '{} bits'.",
1116 get_type_identrust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_type_ident().(&field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
1117 get_bits_from_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_bits_from_type().(&field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
1118 bits
1119 ),
1120 ));
1121 }
1122
1123 bits
1124 }
1125 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {
1126 if field_type rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().FieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#::IntegerFieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#IntegerFieldType# {
1127 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1128 field_tokens.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1129 "Custom and nested field types require a defined bit size, otherwise we can't determine the size of the field.",
1130 ));
1131 }
1132
1133 get_bits_from_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_bits_from_type().(&field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
1134 }
1135 };
1136
1137 // Make sure the field bits are greater than 0.
1138 if bits rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0 {
1139 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1140 bit_attribute_tokens.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1141 "The field bits must be greater than 0.",
1142 ));
1143 }
1144
1145 // Make sure the default value is within the field bits. If a number was unable
1146 // to be parsed, let's take a chance and see if the user is trying to
1147 // use a const variable or a const function.
1148 let parsed_number = if field_type rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[FieldType][`PartialEq<Self>`]eq().==rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[FieldType][`PartialEq<Self>`]eq(). rust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[FieldType][`PartialEq<Self>`]eq().FieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#::IntegerFieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#IntegerFieldType#
1149 && bits_attribute.clonerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[BitsAttribute][Clone]clone().().default_value_exprrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitsAttribute#default_value_expr..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().()
1150 {
1151 check_default_value_fit_in_fieldrust-analyzer cargo bitfields-impl 1.0.0 check_default_value_fit_in_field().(
1152 &bits_attribute.clonerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/impl#[BitsAttribute][Clone]clone().().default_value_exprrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitsAttribute#default_value_expr..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
1153 bits,
1154 field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Type`][Clone]clone().(),
1155 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
1156 } else {
1157 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
1158 };
1159
1160 let unsigned =
1161 field_type rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().FieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#::IntegerFieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#IntegerFieldType# || is_unsigned_integer_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/is_unsigned_integer_type().(&field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty.);
1162 let access = if padding {
1163 if bits_attribute.accessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitsAttribute#access..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() {
1164 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1165 bit_attribute_tokens.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1166 "Padding fields can't have a specified access.",
1167 ));
1168 }
1169
1170 FieldAccessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldAccess#::Nonerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldAccess#None#
1171 } else {
1172 bits_attribute.accessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitsAttribute#access..unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(FieldAccessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldAccess#::ReadWriterust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldAccess#ReadWrite#)
1173 };
1174 let offset = calculate_field_offsetrust-analyzer cargo bitfields-impl 1.0.0 calculate_field_offset().(bits, bitfield_attribute, prev_fields)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
1175
1176 let default_value_tokens = match bits_attribute.default_value_exprrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitsAttribute#default_value_expr. {
1177 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
1178 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ref expr) => {
1179 // We want to add integer literals to default values expressions if the
1180 // expression is a negative number without a suffix. We do alot of casting
1181 // so what happens is, if there is the default value expr `-125`, when we
1182 // try to cast later like `-125 as u8`, Rust will complain that the number
1183 // is too large for the type. Adding the integer suffix will fix this since
1184 // Rust will know the type of the number and will cast it.
1185 if unsigned
1186 || field_type rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().FieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#::IntegerFieldTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/FieldType#IntegerFieldType#
1187 || parsed_number.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().()
1188 || parsed_number.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().has_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#has_integer_suffix.
1189 {
1190 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(quoterust-analyzer cargo quote 1.0.40 quote!! {
1191 #expr
1192 })
1193 } else {
1194 let tokens =
1195 add_integer_literals_to_exprrust-analyzer cargo bitfields-impl 1.0.0 add_integer_literals_to_expr().(&expr.clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Expr`][Clone]clone().(), field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Type`][Clone]clone().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
1196
1197 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(quoterust-analyzer cargo quote 1.0.40 quote!! {
1198 #tokens
1199 })
1200 }
1201 }
1202 };
1203
1204 BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField# {
1205 namerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#name.: field_tokens.identrust-analyzer cargo syn 2.0.104 data/Field#ident..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(),
1206 tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#ty.: field_tokens.tyrust-analyzer cargo syn 2.0.104 data/Field#ty..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Type`][Clone]clone().(),
1207 visrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#vis.: visibility,
1208 bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#bits.,
1209 offsetrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#offset.,
1210 default_value_tokensrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#default_value_tokens.,
1211 unsignedrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#unsigned.,
1212 paddingrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#padding.,
1213 accessrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#access.,
1214 field_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#field_type.,
1215 ignorerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#ignore.: false,
1216 }
1217 };
1218
1219 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bitfield)
1220 }
1221
1222 /// Checks if the default value can fit in the field bits.
1223 fn check_default_value_fit_in_fieldrust-analyzer cargo bitfields-impl 1.0.0 check_default_value_fit_in_field().(
1224 default_value_expr: &Exprrust-analyzer cargo syn 2.0.104 expr/Expr#,
1225 bits: u8,
1226 field_type: Typerust-analyzer cargo syn 2.0.104 ty/Type#,
1227 ) -> synrust-analyzer cargo syn 2.0.104 crate/::Resultrust-analyzer cargo syn 2.0.104 error/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ParsedNumberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#>> {
1228 let default_value_str = &quoterust-analyzer cargo quote 1.0.40 quote!!(#default_value_expr).to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().();
1229
1230 let parsed_number = match parse_number_stringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/parse_number_string().(default_value_str) {
1231 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(number) => number,
1232 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
1233 return match err {
1234 NumberParseErrorrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#::FloatNotSupportedrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#FloatNotSupported# => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1235 default_value_expr.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1236 "Floats are not supported as default values.".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
1237 )),
1238 // Maybe the user is trying to use a const variable or a const
1239 // function call as a default.
1240 NumberParseErrorrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#::InvalidNumberStringrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/NumberParseError#InvalidNumberString# => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#),
1241 };
1242 }
1243 };
1244
1245 let bits_max_value = 1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shl<Self>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shl<Self>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shl<Self>`]shl().bits as u128;
1246 if parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]ge().bits_max_value {
1247 if parsed_number.negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative. {
1248 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1249 default_value_expr.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1250 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
1251 "The default value -'{}' is too large to fit into the specified '{} bits'.",
1252 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number., bits,
1253 ),
1254 ));
1255 }
1256 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1257 default_value_expr.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1258 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
1259 "The default value '{}' is too large to fit into the specified '{} bits'.",
1260 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number., bits,
1261 ),
1262 ));
1263 }
1264
1265 let default_value_too_big_for_type = match get_integer_type_from_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_integer_type_from_type().(&field_type) {
1266 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::Boolrust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#Bool# => parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().1,
1267 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::U8rust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#U8# => parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().u8::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]MAX. as u128,
1268 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::U16rust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#U16# => parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().u16::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]MAX. as u128,
1269 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::U32rust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#U32# => parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().u32::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]MAX. as u128,
1270 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::U64rust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#U64# => parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().u64::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u64]MAX. as u128,
1271 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::U128rust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#U128# => {
1272 // Unable to happen, this is Rust's max unsigned type value.
1273 false
1274 }
1275 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::Usizerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#Usize# => parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().usize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]MAX. as u128,
1276 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::Isizerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#Isize# => {
1277 if parsed_number.negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative. {
1278 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().isize::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]MIN..unsigned_absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]unsigned_abs().() as u128
1279 } else {
1280 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().isize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]MAX. as u128
1281 }
1282 }
1283 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::I8rust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#I8# => {
1284 if parsed_number.negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative. {
1285 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().i8::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i8]MIN..unsigned_absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i8]unsigned_abs().() as u128
1286 } else {
1287 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().i8::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i8]MAX. as u128
1288 }
1289 }
1290 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::I16rust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#I16# => {
1291 if parsed_number.negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative. {
1292 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().i16::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i16]MIN..unsigned_absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i16]unsigned_abs().() as u128
1293 } else {
1294 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().i16::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i16]MAX. as u128
1295 }
1296 }
1297 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::I32rust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#I32# => {
1298 if parsed_number.negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative. {
1299 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().i32::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]MIN..unsigned_absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]unsigned_abs().() as u128
1300 } else {
1301 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().i32::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]MAX. as u128
1302 }
1303 }
1304 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::I64rust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#I64# => {
1305 if parsed_number.negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative. {
1306 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().i64::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]MIN..unsigned_absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]unsigned_abs().() as u128
1307 } else {
1308 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().i64::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i64]MAX. as u128
1309 }
1310 }
1311 IntegerTyperust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#::I128rust-analyzer cargo bitfields-impl 1.0.0 parsing/types/IntegerType#I128# => {
1312 if parsed_number.negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative. {
1313 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().i128::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i128]MIN..unsigned_absrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i128]unsigned_abs().()
1314 } else {
1315 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u128][`PartialOrd<Self>`]gt().i128::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i128]MAX. as u128
1316 }
1317 }
1318 _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(default_value_expr.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(), PANIC_ERROR_MESSAGErust-analyzer cargo bitfields-impl 1.0.0 generation/common/PANIC_ERROR_MESSAGE.))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
1319 };
1320
1321 if default_value_too_big_for_type {
1322 let negative_str = if parsed_number.negativerust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#negative. { "-" } else { "" };
1323 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(
1324 default_value_expr.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(),
1325 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
1326 "The default value '{}{}' is too large to fit into the field type '{}'.",
1327 negative_str,
1328 parsed_number.numberrust-analyzer cargo bitfields-impl 1.0.0 parsing/number_parser/ParsedNumber#number.,
1329 get_type_identrust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_type_ident().(&field_type).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()
1330 ),
1331 ));
1332 }
1333
1334 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(parsed_number))
1335 }
1336
1337 /// Calculate the offset of a field based on previous fields.
1338 fn calculate_field_offsetrust-analyzer cargo bitfields-impl 1.0.0 calculate_field_offset().(
1339 bits: u8,
1340 bitfield_attribute: &BitfieldAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#,
1341 prev_fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#],
1342 ) -> synrust-analyzer cargo syn 2.0.104 crate/::Resultrust-analyzer cargo syn 2.0.104 error/Result#<u8> {
1343 let offset = prev_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| field.bitsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#bits.).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<u8>();
1344
1345 match bitfield_attribute.bit_orderrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#bit_order. {
1346 BitOrderrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitOrder#::Lsbrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitOrder#Lsb# => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(offset),
1347 BitOrderrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitOrder#::Msbrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitOrder#Msb# => {
1348 let bitfield_type_bits = get_bits_from_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_bits_from_type().(&bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
1349 // We calculate offset starting from the left. There's a chance that
1350 // the total bits of all fields is greater than the number of bits
1351 // of the bitfield type. We will catch it later so
1352 // we can ignore for now.
1353 if 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().bits rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]lt().bitfield_type_bits {
1354 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bitfield_type_bits rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().bits rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().offset)
1355 } else {
1356 // We've underflow the bitfield type, this will be caught later.
1357 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(0)
1358 }
1359 }
1360 }
1361 }
1362
1363 /// Adds the field type integer literal suffix to the expression.
1364 ///
1365 /// For example, if the expression is '-1' and the field type is 'i8', the
1366 /// expression will be updated to '1i8'.
1367 fn add_integer_literals_to_exprrust-analyzer cargo bitfields-impl 1.0.0 add_integer_literals_to_expr().(expr: &Exprrust-analyzer cargo syn 2.0.104 expr/Expr#, field_type: Typerust-analyzer cargo syn 2.0.104 ty/Type#) -> synrust-analyzer cargo syn 2.0.104 crate/::Resultrust-analyzer cargo syn 2.0.104 error/Result#<TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#> {
1368 let updated_expr = if let Exprrust-analyzer cargo syn 2.0.104 expr/Expr#::Unaryrust-analyzer cargo syn 2.0.104 expr/Expr#Unary#(unary) = expr {
1369 let attrs = unary.attrsrust-analyzer cargo syn 2.0.104 expr/ExprUnary#attrs..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().();
1370 let op = unary.oprust-analyzer cargo syn 2.0.104 expr/ExprUnary#op.;
1371
1372 let updated_expr = if let Exprrust-analyzer cargo syn 2.0.104 expr/Expr#::Litrust-analyzer cargo syn 2.0.104 expr/Expr#Lit#(expr_lit) = *rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Deref]deref().unary.exprrust-analyzer cargo syn 2.0.104 expr/ExprUnary#expr..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T, A>`][Clone]clone().() {
1373 let new_lit = create_expr_lit_with_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 create_expr_lit_with_integer_suffix().(&expr_lit, field_type)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
1374
1375 Exprrust-analyzer cargo syn 2.0.104 expr/Expr#::Litrust-analyzer cargo syn 2.0.104 expr/Expr#Lit#(ExprLitrust-analyzer cargo syn 2.0.104 expr/ExprLit# { attrsrust-analyzer cargo syn 2.0.104 expr/ExprLit#attrs.: expr_lit.attrsrust-analyzer cargo syn 2.0.104 expr/ExprLit#attrs., litrust-analyzer cargo syn 2.0.104 expr/ExprLit#lit.: new_lit.litrust-analyzer cargo syn 2.0.104 expr/ExprLit#lit. })
1376 } else {
1377 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(expr.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(), PANIC_ERROR_MESSAGErust-analyzer cargo bitfields-impl 1.0.0 generation/common/PANIC_ERROR_MESSAGE.))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
1378 };
1379
1380 Exprrust-analyzer cargo syn 2.0.104 expr/Expr#::Unaryrust-analyzer cargo syn 2.0.104 expr/Expr#Unary#(ExprUnaryrust-analyzer cargo syn 2.0.104 expr/ExprUnary# { attrsrust-analyzer cargo syn 2.0.104 expr/ExprUnary#attrs., oprust-analyzer cargo syn 2.0.104 expr/ExprUnary#op., exprrust-analyzer cargo syn 2.0.104 expr/ExprUnary#expr.: Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(updated_expr) })
1381 } else if let Exprrust-analyzer cargo syn 2.0.104 expr/Expr#::Litrust-analyzer cargo syn 2.0.104 expr/Expr#Lit#(expr_lit) = expr {
1382 let new_lit = create_expr_lit_with_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 create_expr_lit_with_integer_suffix().(expr_lit, field_type)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
1383
1384 Exprrust-analyzer cargo syn 2.0.104 expr/Expr#::Litrust-analyzer cargo syn 2.0.104 expr/Expr#Lit#(ExprLitrust-analyzer cargo syn 2.0.104 expr/ExprLit# { attrsrust-analyzer cargo syn 2.0.104 expr/ExprLit#attrs.: expr_lit.clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::ExprLit`][Clone]clone().().attrsrust-analyzer cargo syn 2.0.104 expr/ExprLit#attrs., litrust-analyzer cargo syn 2.0.104 expr/ExprLit#lit.: new_lit.litrust-analyzer cargo syn 2.0.104 expr/ExprLit#lit. })
1385 } else {
1386 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(expr.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(), PANIC_ERROR_MESSAGErust-analyzer cargo bitfields-impl 1.0.0 generation/common/PANIC_ERROR_MESSAGE.))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
1387 };
1388
1389 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(quoterust-analyzer cargo quote 1.0.40 quote!! {
1390 #updated_expr
1391 })
1392 }
1393
1394 /// Helper for creating an integer literal with the integer suffix.
1395 fn create_expr_lit_with_integer_suffixrust-analyzer cargo bitfields-impl 1.0.0 create_expr_lit_with_integer_suffix().(lit: &ExprLitrust-analyzer cargo syn 2.0.104 expr/ExprLit#, field_type: Typerust-analyzer cargo syn 2.0.104 ty/Type#) -> synrust-analyzer cargo syn 2.0.104 crate/::Resultrust-analyzer cargo syn 2.0.104 error/Result#<ExprLitrust-analyzer cargo syn 2.0.104 expr/ExprLit#> {
1396 let integer_type = get_integer_type_from_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_integer_type_from_type().(&field_type);
1397 let integer_suffix = get_integer_suffix_from_integer_typerust-analyzer cargo bitfields-impl 1.0.0 parsing/types/get_integer_suffix_from_integer_type().(integer_type)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
1398
1399 let new_lit = match lit.litrust-analyzer cargo syn 2.0.104 expr/ExprLit#lit..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Lit`][Clone]clone().() {
1400 Litrust-analyzer cargo syn 2.0.104 lit/Lit#::Intrust-analyzer cargo syn 2.0.104 lit/Lit#Int#(lit_int) => {
1401 let new_lit_int =
1402 LitIntrust-analyzer cargo syn 2.0.104 lit/LitInt#::newrust-analyzer cargo syn 2.0.104 lit/impl#[LitInt]new().(&formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}{}", lit_int.tokenrust-analyzer cargo syn 2.0.104 lit/impl#[LitInt]token().(), integer_suffix), lit_int.spanrust-analyzer cargo syn 2.0.104 lit/impl#[LitInt]span().());
1403 ExprLitrust-analyzer cargo syn 2.0.104 expr/ExprLit# { attrsrust-analyzer cargo syn 2.0.104 expr/ExprLit#attrs.: lit.attrsrust-analyzer cargo syn 2.0.104 expr/ExprLit#attrs..clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][Clone]clone().(), litrust-analyzer cargo syn 2.0.104 expr/ExprLit#lit.: Litrust-analyzer cargo syn 2.0.104 lit/Lit#::Intrust-analyzer cargo syn 2.0.104 lit/Lit#Int#(new_lit_int) }
1404 }
1405 _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(lit.spanrust-analyzer cargo syn 2.0.104 spanned/impl#[T][Spanned]span().(), PANIC_ERROR_MESSAGErust-analyzer cargo bitfields-impl 1.0.0 generation/common/PANIC_ERROR_MESSAGE.))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
1406 };
1407
1408 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(new_lit)
1409 }
1410
1411 /// Generate the bitfield functions.
1412 fn generate_functionsrust-analyzer cargo bitfields-impl 1.0.0 generate_functions().(
1413 bitfield_attribute: &BitfieldAttributerust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#,
1414 fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#],
1415 ignored_fields: &[BitfieldFieldrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_field/BitfieldField#],
1416 struct_tokens: &synrust-analyzer cargo syn 2.0.104 crate/::ItemStructrust-analyzer cargo syn 2.0.104 item/ItemStruct#,
1417 ) -> synrust-analyzer cargo syn 2.0.104 crate/::Resultrust-analyzer cargo syn 2.0.104 error/Result#<TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream#> {
1418 let struct_attributes: TokenStreamrust-analyzer cargo proc-macro2 1.0.95 TokenStream# =
1419 struct_tokens.attrsrust-analyzer cargo syn 2.0.104 item/ItemStruct#attrs..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().(ToTokensrust-analyzer cargo quote 1.0.40 to_tokens/ToTokens#::to_token_streamrust-analyzer cargo quote 1.0.40 to_tokens/ToTokens#to_token_stream().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
1420 let struct_name = &struct_tokens.identrust-analyzer cargo syn 2.0.104 item/ItemStruct#ident.;
1421
1422 let bitfield_struct = 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().() {
1423 generate_struct_with_fields_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/tuple_struct/generate_struct_with_fields_tokens().(
1424 struct_name.clonerust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][Clone]clone().(),
1425 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1426 bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Type`][Clone]clone().(),
1427 ignored_fields,
1428 )
1429 } else {
1430 generate_tuple_struct_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/tuple_struct/generate_tuple_struct_tokens().(
1431 struct_name.clonerust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][Clone]clone().(),
1432 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1433 bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Type`][Clone]clone().(),
1434 )
1435 };
1436 let new_function = bitfield_attribute.generate_new_funcrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_new_func..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1437 generate_new_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/new_impl/generate_new_function_tokens().(
1438 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1439 fields,
1440 ignored_fields,
1441 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1442 )
1443 });
1444 let new_without_defaults_function = bitfield_attribute.generate_new_funcrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_new_func..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1445 generate_new_without_defaults_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/new_impl/generate_new_without_defaults_function_tokens().(
1446 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1447 fields,
1448 ignored_fields,
1449 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1450 )
1451 });
1452 let from_bits_function = bitfield_attribute.generate_from_bits_funcrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_from_bits_func..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1453 generate_from_bits_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/from_into_bits_conversions/generate_from_bits_function_tokens().(
1454 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1455 fields,
1456 ignored_fields,
1457 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1458 bitfield_attribute,
1459 )
1460 });
1461 let from_bits_with_defaults_function = bitfield_attribute.generate_from_bits_funcrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_from_bits_func..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1462 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().(
1463 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1464 fields,
1465 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1466 bitfield_attribute,
1467 !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().(),
1468 )
1469 });
1470 let generate_into_bits_function = bitfield_attribute.generate_into_bits_funcrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_into_bits_func..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1471 generate_into_bits_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/from_into_bits_conversions/generate_into_bits_function_tokens().(
1472 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1473 bitfield_attribute,
1474 !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().(),
1475 )
1476 });
1477 let field_consts_tokens = generate_field_constants_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/field_const_getter_setter/generate_field_constants_tokens().(struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(), fields);
1478 let field_getters_tokens = generate_field_getters_functions_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/field_const_getter_setter/generate_field_getters_functions_tokens().(
1479 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1480 bitfield_attribute,
1481 fields,
1482 !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().(),
1483 bitfield_attribute.generate_neg_funcrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_neg_func.,
1484 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
1485 let field_setters_tokens = generate_field_setters_functions_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/field_const_getter_setter/generate_field_setters_functions_tokens().(
1486 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1487 bitfield_attribute,
1488 fields,
1489 !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().(),
1490 );
1491 let default_function = bitfield_attribute.generate_default_implrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_default_impl..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1492 generate_default_implementation_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/default_impl/generate_default_implementation_tokens().(
1493 struct_name.clonerust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][Clone]clone().(),
1494 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1495 fields,
1496 ignored_fields,
1497 )
1498 });
1499 let builder_tokens = bitfield_attribute.generate_builderrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_builder..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1500 generate_builder_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/builder_struct/generate_builder_tokens().(
1501 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1502 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1503 struct_name.clonerust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][Clone]clone().(),
1504 fields,
1505 ignored_fields,
1506 )
1507 });
1508
1509 let from_bitfield_type_for_bitfield_function_tokens =
1510 bitfield_attribute.generate_from_trait_funcsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_from_trait_funcs..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1511 generate_from_bitfield_type_for_bitfield_implementation_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/from_types_impl/generate_from_bitfield_type_for_bitfield_implementation_tokens().(
1512 struct_name.clonerust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][Clone]clone().(),
1513 fields,
1514 ignored_fields,
1515 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1516 )
1517 });
1518 let from_bitfield_for_bitfield_type_function_tokens =
1519 bitfield_attribute.generate_from_trait_funcsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_from_trait_funcs..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1520 generate_from_bitfield_for_bitfield_type_implementation_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/from_types_impl/generate_from_bitfield_for_bitfield_type_implementation_tokens().(
1521 struct_name.clonerust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][Clone]clone().(),
1522 bitfield_attribute,
1523 !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().(),
1524 )
1525 });
1526 let debug_impl = bitfield_attribute.generate_debug_implrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_debug_impl..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1527 generate_debug_implementationrust-analyzer cargo bitfields-impl 1.0.0 generation/debug_impl/generate_debug_implementation().(
1528 struct_name.clonerust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][Clone]clone().(),
1529 bitfield_attribute,
1530 fields,
1531 !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().(),
1532 )
1533 });
1534 let get_bit_operations = bitfield_attribute.generate_bit_opsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_bit_ops..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1535 generate_get_bit_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/bit_operations/generate_get_bit_tokens().(
1536 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1537 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1538 fields,
1539 !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().(),
1540 )
1541 });
1542 let set_bit_operations = bitfield_attribute.generate_bit_opsrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_bit_ops..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1543 generate_set_bit_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/bit_operations/generate_set_bit_tokens().(
1544 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1545 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1546 fields,
1547 !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().(),
1548 )
1549 });
1550 let to_builder_tokens = (bitfield_attribute.generate_builderrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_builder.
1551 && bitfield_attribute.generate_to_builderrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_to_builder.)
1552 .thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| generate_to_builder_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/builder_struct/generate_to_builder_tokens().(struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(), struct_name.clonerust-analyzer cargo proc-macro2 1.0.95 impl#[Ident][Clone]clone().()));
1553 let set_bits_operations = bitfield_attribute.generate_set_bits_implrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_set_bits_impl..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1554 generate_set_bits_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/set_clear_bits_impl/generate_set_bits_function_tokens().(
1555 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1556 fields,
1557 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1558 !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().(),
1559 )
1560 });
1561 let set_bits_with_defaults_operations = bitfield_attribute.generate_set_bits_implrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_set_bits_impl..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1562 generate_set_bits_with_defaults_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/set_clear_bits_impl/generate_set_bits_with_defaults_function_tokens().(
1563 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1564 fields,
1565 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1566 !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().(),
1567 )
1568 });
1569 let clear_bits_operations = bitfield_attribute.generate_clear_bits_implrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_clear_bits_impl..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1570 generate_clear_bits_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/set_clear_bits_impl/generate_clear_bits_function_tokens().(
1571 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1572 fields,
1573 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1574 !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().(),
1575 )
1576 });
1577 let clear_bits_preserve_defaults_operations =
1578 bitfield_attribute.generate_clear_bits_implrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#generate_clear_bits_impl..thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then().(|| {
1579 generate_clear_bits_preserve_defaults_function_tokensrust-analyzer cargo bitfields-impl 1.0.0 generation/set_clear_bits_impl/generate_clear_bits_preserve_defaults_function_tokens().(
1580 struct_tokens.visrust-analyzer cargo syn 2.0.104 item/ItemStruct#vis..clonerust-analyzer cargo syn 2.0.104 gen/clone/impl#[`crate::Visibility`][Clone]clone().(),
1581 fields,
1582 &bitfield_attribute.tyrust-analyzer cargo bitfields-impl 1.0.0 parsing/bitfield_attribute/BitfieldAttribute#ty.,
1583 !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().(),
1584 )
1585 });
1586 let default_attrs = if ignored_fields.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() {
1587 quoterust-analyzer cargo quote 1.0.40 quote!! {
1588 #[repr(transparent)]
1589 }
1590 } else {
1591 quoterust-analyzer cargo quote 1.0.40 quote!! {
1592 #[repr(C)]
1593 }
1594 };
1595
1596 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(quoterust-analyzer cargo quote 1.0.40 quote!! {
1597 #struct_attributes
1598 #default_attrs
1599 #bitfield_struct
1600
1601 impl #struct_name {
1602 #new_function
1603 #new_without_defaults_function
1604
1605 #from_bits_function
1606 #from_bits_with_defaults_function
1607
1608 #generate_into_bits_function
1609
1610 #field_consts_tokens
1611 #field_getters_tokens
1612 #field_setters_tokens
1613
1614 #set_bits_operations
1615 #set_bits_with_defaults_operations
1616 #clear_bits_operations
1617 #clear_bits_preserve_defaults_operations
1618
1619 #get_bit_operations
1620 #set_bit_operations
1621
1622 #to_builder_tokens
1623 }
1624
1625 #default_function
1626
1627 #builder_tokens
1628
1629 #from_bitfield_type_for_bitfield_function_tokens
1630 #from_bitfield_for_bitfield_type_function_tokens
1631
1632 #debug_impl
1633 })
1634 }
1635
1636 /// Creates a syn error with the specified message that occurred at the
1637 /// specified span.
1638 pub(craterust-analyzer cargo bitfields-impl 1.0.0 crate/) fn create_syn_errorrust-analyzer cargo bitfields-impl 1.0.0 create_syn_error().(span: proc_macro2rust-analyzer cargo proc-macro2 1.0.95 crate/::Spanrust-analyzer cargo proc-macro2 1.0.95 Span#, msg: impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display#) -> synrust-analyzer cargo syn 2.0.104 crate/::Errorrust-analyzer cargo syn 2.0.104 error/Error# {
1639 synrust-analyzer cargo syn 2.0.104 crate/::Errorrust-analyzer cargo syn 2.0.104 error/Error#::newrust-analyzer cargo syn 2.0.104 error/impl#[Error]new().(span, msg)
1640 }