Skip to content

Commit 36c4490

Browse files
committed
only perform check for non-array fields
1 parent 678cef7 commit 36c4490

File tree

1 file changed

+73
-54
lines changed

1 file changed

+73
-54
lines changed

bitbybit/src/bitfield/parsing.rs

Lines changed: 73 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -344,62 +344,19 @@ fn parse_field(base_data_size: usize, field: &Field) -> Result<FieldDefinition>
344344

345345
// Verify bounds for arrays
346346
if let Some(indexed_count) = indexed_count {
347-
if ranges.len() == 1 {
348-
// If stride wasn't given, use the field width
349-
if indexed_stride.is_none() {
350-
indexed_stride = Some(number_of_bits)
351-
}
352-
if number_of_bits > indexed_stride.unwrap() {
353-
return Err(Error::new_spanned(
354-
field.attrs.first(),
355-
format!(
356-
"bitfield!: Field {} is declared as {} bits, which is larger than the stride {}",
357-
field_name,
358-
number_of_bits,
359-
indexed_stride.unwrap()
360-
),
361-
));
362-
}
363-
} else {
364-
// With multiple ranges, strides are mandatory
365-
if indexed_stride.is_none() {
366-
return Err(Error::new_spanned(
367-
field,
368-
format!(
369-
"bitfield!: Field {} is declared as non-contiguous and array, so it needs a stride. Specify using \"stride = x\".",
370-
field_name,
371-
),
372-
));
373-
}
374-
}
375-
376-
let highest_bit_index_in_ranges = ranges.iter().map(|range| range.end).max().unwrap_or(0);
377-
let number_of_bits_indexed =
378-
(indexed_count - 1) * indexed_stride.unwrap() + highest_bit_index_in_ranges;
379-
if number_of_bits_indexed > base_data_size {
380-
return Err(Error::new_spanned(
381-
field.attrs.first(),
382-
format!(
383-
"bitfield!: Array-field {} requires {number_of_bits_indexed} bits for the array, but only has ({})",
384-
field_name,
385-
base_data_size
386-
)
387-
));
388-
}
389-
390-
if indexed_count < 2 {
391-
return Err(Error::new_spanned(
392-
&field.ty,
393-
format!(
394-
"bitfield!: Field {} is declared as array, but with fewer than 2 elements.",
395-
field_name
396-
),
397-
));
398-
}
347+
verify_bounds_for_array(
348+
field,
349+
field_name,
350+
&ranges,
351+
indexed_count,
352+
indexed_stride,
353+
number_of_bits,
354+
base_data_size,
355+
)?;
356+
} else {
357+
verify_bits_in_range(field, field_name, &ranges, base_data_size)?;
399358
}
400359

401-
verify_bits_in_range(field, field_name, &ranges, base_data_size)?;
402-
403360
let (custom_type, getter_type, setter_type) = if field_type_size_from_data_type.is_none() {
404361
parse_enumeration(ty, &primitive_type)?
405362
} else {
@@ -458,6 +415,68 @@ fn verify_bits_in_range(
458415
Ok(())
459416
}
460417

418+
fn verify_bounds_for_array(
419+
field: &Field,
420+
field_name: &Ident,
421+
ranges: &[Range<usize>],
422+
indexed_count: usize,
423+
indexed_stride: Option<usize>,
424+
number_of_bits: usize,
425+
base_data_size: usize,
426+
) -> syn::Result<()> {
427+
if ranges.len() == 1 {
428+
// If stride wasn't given, use the field width
429+
let indexed_stride = indexed_stride.unwrap_or(number_of_bits);
430+
if number_of_bits > indexed_stride {
431+
return Err(Error::new_spanned(
432+
field.attrs.first(),
433+
format!(
434+
"bitfield!: Field {} is declared as {} bits, which is larger than the stride {}",
435+
field_name,
436+
number_of_bits,
437+
indexed_stride
438+
),
439+
));
440+
}
441+
} else {
442+
// With multiple ranges, strides are mandatory
443+
if indexed_stride.is_none() {
444+
return Err(Error::new_spanned(
445+
field,
446+
format!(
447+
"bitfield!: Field {} is declared as non-contiguous and array, so it needs a stride. Specify using \"stride = x\".",
448+
field_name,
449+
),
450+
));
451+
}
452+
}
453+
454+
let highest_bit_index_in_ranges = ranges.iter().map(|range| range.end).max().unwrap_or(0);
455+
let number_of_bits_indexed =
456+
(indexed_count - 1) * indexed_stride.unwrap() + highest_bit_index_in_ranges;
457+
if number_of_bits_indexed > base_data_size {
458+
return Err(Error::new_spanned(
459+
field.attrs.first(),
460+
format!(
461+
"bitfield!: Array-field {} requires {number_of_bits_indexed} bits for the array, but only has ({})",
462+
field_name,
463+
base_data_size
464+
)
465+
));
466+
}
467+
468+
if indexed_count < 2 {
469+
return Err(Error::new_spanned(
470+
&field.ty,
471+
format!(
472+
"bitfield!: Field {} is declared as array, but with fewer than 2 elements.",
473+
field_name
474+
),
475+
));
476+
}
477+
Ok(())
478+
}
479+
461480
/// Parses the arguments of a field. At the beginning and after each comma, Reset is used. After
462481
/// that, the various take_xxx functions are used to switch states.
463482
#[derive(Copy, Clone, PartialEq, Eq, Debug)]

0 commit comments

Comments
 (0)