Skip to content

Commit ab03e5e

Browse files
bors[bot]burrbull
andauthored
Merge #381
381: simplify enum derive r=therealprof a=burrbull r? @therealprof IMPORTANT. Requires [Rust 1.37](https://blog.rust-lang.org/2019/08/15/Rust-1.37.0.html#referring-to-enum-variants-through-type-aliases) Co-authored-by: Andrey Zgarbul <zgarbul.andrey@gmail.com>
2 parents deedb67 + 0338f9d commit ab03e5e

File tree

1 file changed

+15
-45
lines changed

1 file changed

+15
-45
lines changed

src/generate/register.rs

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ pub fn fields(
273273
let pc_r = Ident::new(&(pc.clone() + "_A"), span);
274274
let mut pc_w = &pc_r;
275275

276-
let mut base_pc_w = None;
277276
let mut evs_r = None;
278277

279278
if can_read {
@@ -306,22 +305,17 @@ pub fn fields(
306305
}
307306
});
308307

309-
base_pc_w = base.as_ref().map(|base| {
308+
if let Some(base) = base {
310309
let pc = base.field.to_sanitized_upper_case();
311310
let base_pc_r = Ident::new(&(pc.clone() + "_A"), span);
312-
let base_pc_r =
313-
derive_from_base(mod_items, &base, &pc_r, &base_pc_r, &description);
311+
derive_from_base(mod_items, &base, &pc_r, &base_pc_r, &description);
314312

315313
let doc = format!("Reader of field `{}`", f.name);
316314
mod_items.push(quote! {
317315
#[doc = #doc]
318-
pub type #_pc_r = crate::R<#fty, #base_pc_r>;
316+
pub type #_pc_r = crate::R<#fty, #pc_r>;
319317
});
320-
321-
base_pc_r
322-
});
323-
324-
if base.is_none() {
318+
} else {
325319
let has_reserved_variant = evs.values.len() != (1 << width);
326320
let variants = Variant::from_enumerated_values(evs)?;
327321

@@ -441,14 +435,12 @@ pub fn fields(
441435

442436
if Some(evs) != evs_r.as_ref() {
443437
pc_w = &new_pc_w;
444-
base_pc_w = base.as_ref().map(|base| {
438+
if let Some(base) = base {
445439
let pc = base.field.to_sanitized_upper_case();
446440
let base_pc_w = Ident::new(&(pc + "_AW"), span);
447441
derive_from_base(mod_items, &base, &pc_w, &base_pc_w, &description)
448-
});
449-
450-
if base.is_none() {
451-
add_from_variants(mod_items, &variants, &pc_w, &fty, &description, rv);
442+
} else {
443+
add_from_variants(mod_items, &variants, pc_w, &fty, &description, rv);
452444
}
453445
}
454446

@@ -467,23 +459,13 @@ pub fn fields(
467459
let sc = &v.sc;
468460

469461
let doc = util::escape_brackets(util::respace(&v.doc).as_ref());
470-
if let Some(enum_) = base_pc_w.as_ref() {
471-
proxy_items.push(quote! {
472-
#[doc = #doc]
473-
#[inline(always)]
474-
pub fn #sc(self) -> &'a mut W {
475-
self.variant(#enum_::#pc)
476-
}
477-
});
478-
} else {
479-
proxy_items.push(quote! {
480-
#[doc = #doc]
481-
#[inline(always)]
482-
pub fn #sc(self) -> &'a mut W {
483-
self.variant(#pc_w::#pc)
484-
}
485-
});
486-
}
462+
proxy_items.push(quote! {
463+
#[doc = #doc]
464+
#[inline(always)]
465+
pub fn #sc(self) -> &'a mut W {
466+
self.variant(#pc_w::#pc)
467+
}
468+
});
487469
}
488470
}
489471

@@ -658,7 +640,7 @@ fn derive_from_base(
658640
pc: &Ident,
659641
base_pc: &Ident,
660642
desc: &str,
661-
) -> TokenStream {
643+
) {
662644
let span = Span::call_site();
663645
if let (Some(peripheral), Some(register)) = (&base.peripheral, &base.register) {
664646
let pmod_ = peripheral.to_sanitized_snake_case();
@@ -671,10 +653,6 @@ fn derive_from_base(
671653
pub type #pc =
672654
crate::#pmod_::#rmod_::#base_pc;
673655
});
674-
675-
quote! {
676-
crate::#pmod_::#rmod_::#base_pc
677-
}
678656
} else if let Some(register) = &base.register {
679657
let mod_ = register.to_sanitized_snake_case();
680658
let mod_ = Ident::new(&mod_, span);
@@ -684,19 +662,11 @@ fn derive_from_base(
684662
pub type #pc =
685663
super::#mod_::#base_pc;
686664
});
687-
688-
quote! {
689-
super::#mod_::#base_pc
690-
}
691665
} else {
692666
mod_items.push(quote! {
693667
#[doc = #desc]
694668
pub type #pc = #base_pc;
695669
});
696-
697-
quote! {
698-
#base_pc
699-
}
700670
}
701671
}
702672

0 commit comments

Comments
 (0)