Skip to content

Commit 3262d71

Browse files
committed
Untangle and deduplicate code a bit
Signed-off-by: Daniel Egger <daniel@eggers-club.de>
1 parent d35250c commit 3262d71

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

src/generate/register.rs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,26 @@ pub fn fields(
223223
r_impl_items: &mut Vec<TokenStream>,
224224
w_impl_items: &mut Vec<TokenStream>,
225225
) -> Result<()> {
226+
struct F<'a> {
227+
_pc_w: Ident,
228+
_sc: Ident,
229+
access: Option<Access>,
230+
description: String,
231+
description_with_bits: String,
232+
evs: &'a [EnumeratedValues],
233+
mask: u64,
234+
name: &'a str,
235+
offset: u64,
236+
pc_r: Ident,
237+
_pc_r: Ident,
238+
pc_w: Ident,
239+
sc: Ident,
240+
bits: Ident,
241+
ty: Ident,
242+
width: u32,
243+
write_constraint: Option<&'a WriteConstraint>,
244+
}
245+
226246
impl<'a> F<'a> {
227247
fn from(f: &'a Field) -> Result<Self> {
228248
// TODO(AJM) - do we need to do anything with this range type?
@@ -235,7 +255,7 @@ pub fn fields(
235255
let pc_w = Ident::new(&format!("{}_AW", pc), span);
236256
let _pc_w = Ident::new(&format!("{}_W", pc), span);
237257
let _sc = Ident::new(&format!("_{}", sc), span);
238-
let bits = Ident::new(if width == 1 { "bit" } else { "bits" }, Span::call_site());
258+
let bits = Ident::new(if width == 1 { "bit" } else { "bits" }, span);
239259
let mut description_with_bits = if width == 1 {
240260
format!("Bit {}", offset)
241261
} else {
@@ -263,7 +283,7 @@ pub fn fields(
263283
width,
264284
access: f.access,
265285
evs: &f.enumerated_values,
266-
sc: Ident::new(&sc, Span::call_site()),
286+
sc: Ident::new(&sc, span),
267287
mask: 1u64.wrapping_neg() >> (64 - width),
268288
name: &f.name,
269289
offset: u64::from(offset),
@@ -273,10 +293,10 @@ pub fn fields(
273293
}
274294
}
275295

276-
let fs = fields.iter().map(F::from).collect::<Result<Vec<_>>>()?;
277-
278296
// TODO enumeratedValues
279-
for f in &fs {
297+
for f in fields.iter().map(F::from) {
298+
let f = f?;
299+
280300
let can_read = [Access::ReadOnly, Access::ReadWriteOnce, Access::ReadWrite]
281301
.contains(&access)
282302
&& (f.access != Some(Access::WriteOnly))
@@ -357,7 +377,7 @@ pub fn fields(
357377
let has_reserved_variant = evs.values.len() != (1 << f.width);
358378
let variants = Variant::from_enumerated_values(evs)?;
359379

360-
add_from_variants(mod_items, &variants, pc_r, &f, description, rv);
380+
add_from_variants(mod_items, &variants, pc_r, fty, description, rv);
361381

362382
let mut enum_items = vec![];
363383

@@ -481,7 +501,7 @@ pub fn fields(
481501
});
482502

483503
if base.is_none() {
484-
add_from_variants(mod_items, &variants, pc_w, &f, description, rv);
504+
add_from_variants(mod_items, &variants, pc_w, fty, description, rv);
485505
}
486506
}
487507

@@ -641,13 +661,11 @@ fn add_from_variants(
641661
mod_items: &mut Vec<TokenStream>,
642662
variants: &[Variant],
643663
pc: &Ident,
644-
f: &F,
664+
fty: &Ident,
645665
desc: &str,
646666
reset_value: Option<u64>,
647667
) {
648-
let fty = &f.ty;
649-
650-
let (repr, cast) = if f.ty == "bool" {
668+
let (repr, cast) = if fty == "bool" {
651669
(quote! {}, quote! { variant as u8 != 0 })
652670
} else {
653671
(quote! { #[repr(#fty)] }, quote! { variant as _ })
@@ -736,26 +754,6 @@ fn derive_from_base(
736754
}
737755
}
738756

739-
struct F<'a> {
740-
_pc_w: Ident,
741-
_sc: Ident,
742-
access: Option<Access>,
743-
description: String,
744-
description_with_bits: String,
745-
evs: &'a [EnumeratedValues],
746-
mask: u64,
747-
name: &'a str,
748-
offset: u64,
749-
pc_r: Ident,
750-
_pc_r: Ident,
751-
pc_w: Ident,
752-
sc: Ident,
753-
bits: Ident,
754-
ty: Ident,
755-
width: u32,
756-
write_constraint: Option<&'a WriteConstraint>,
757-
}
758-
759757
#[derive(Clone, Debug)]
760758
pub struct Base<'a> {
761759
pub peripheral: Option<&'a str>,

0 commit comments

Comments
 (0)