Skip to content

Commit 3c8cf2c

Browse files
committed
Try #358:
2 parents f129d7a + 8b39c1f commit 3c8cf2c

File tree

2 files changed

+35
-40
lines changed

2 files changed

+35
-40
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1111

1212
- `variant()` method for field reader and `Variant` enum for fields with reserved values
1313

14+
- Field readers and writers use one enum where it is possible
15+
1416
## [v0.15.2] - 2019-07-29
1517

1618
- No changes, just fixing the metadata since crates.io didn't like the keywords

src/generate/register.rs

+33-40
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,10 @@ pub fn fields(
255255
} = f.bit_range;
256256
let sc = f.name.to_sanitized_snake_case();
257257
let pc = f.name.to_sanitized_upper_case();
258-
let pc_r = Ident::from(&*format!("{}R", pc));
258+
let pc_r = Ident::from(&*format!("{}_A", pc));
259259
let _pc_r = Ident::from(&*format!("{}_R", pc));
260-
let pc_w = Ident::from(&*format!("{}W", pc));
261-
let _pc_w = Ident::from(&*format!("_{}W", pc));
260+
let pc_w = Ident::from(&*format!("{}_AW", pc));
261+
let _pc_w = Ident::from(&*format!("{}_W", pc));
262262
let _sc = Ident::from(&*format!("_{}", sc));
263263
let bits = if width == 1 {
264264
Ident::from("bit")
@@ -319,6 +319,15 @@ pub fn fields(
319319
all_peripherals,
320320
)?;
321321

322+
323+
let pc_r = &f.pc_r;
324+
let mut pc_w = &f.pc_r;
325+
326+
let mut evs_r = None;
327+
328+
let _pc_r = &f._pc_r;
329+
let _pc_w = &f._pc_w;
330+
322331
if can_read {
323332
let cast = if f.width == 1 {
324333
quote! { != 0 }
@@ -336,10 +345,9 @@ pub fn fields(
336345
}
337346
};
338347

339-
let pc_r = &f.pc_r;
340-
let _pc_r = &f._pc_r;
341-
342348
if let Some((evs, base)) = lookup_filter(&lookup_results, Usage::Read) {
349+
evs_r = Some(evs.clone());
350+
343351
let description = &util::escape_brackets(&f.description);
344352
let sc = &f.sc;
345353
r_impl_items.push(quote! {
@@ -352,8 +360,13 @@ pub fn fields(
352360

353361
if let Some(base) = &base {
354362
let pc = base.field.to_sanitized_upper_case();
355-
let base_pc_r = Ident::from(&*format!("{}_R", pc));
356-
derive_from_base(mod_items, &base, &_pc_r, &base_pc_r, f.name);
363+
let base_pc_r = Ident::from(&*format!("{}_A", pc));
364+
let base_pc_r = derive_from_base(mod_items, &base, &pc_r, &base_pc_r, f.name);
365+
366+
mod_items.push(quote! {
367+
///Reader of the field
368+
pub type #_pc_r = crate::FR<#fty, #base_pc_r>;
369+
});
357370
}
358371

359372
if base.is_none() {
@@ -471,16 +484,19 @@ pub fn fields(
471484
if variants.len() == 1 << f.width {
472485
unsafety = None;
473486
}
474-
let pc_w = &f.pc_w;
475487

476-
let base_pc_w = base.as_ref().map(|base| {
477-
let pc = base.field.to_sanitized_upper_case();
478-
let base_pc_w = Ident::from(&*format!("{}W", pc));
479-
derive_from_base(mod_items, &base, &pc_w, &base_pc_w, f.name)
480-
});
488+
if Some(evs) != evs_r.as_ref() {
489+
pc_w = &f.pc_w;
481490

482-
if base.is_none() {
483-
add_from_variants(mod_items, &variants, pc_w, &f);
491+
base.as_ref().map(|base| {
492+
let pc = base.field.to_sanitized_upper_case();
493+
let base_pc_w = Ident::from(&*format!("{}_AW", pc));
494+
derive_from_base(mod_items, &base, &pc_w, &base_pc_w, f.name)
495+
});
496+
497+
if base.is_none() {
498+
add_from_variants(mod_items, &variants, pc_w, &f);
499+
}
484500
}
485501

486502
proxy_items.push(quote! {
@@ -499,23 +515,13 @@ pub fn fields(
499515
let sc = &v.sc;
500516

501517
let doc = util::escape_brackets(util::respace(&v.doc).as_ref());
502-
if let Some(enum_) = base_pc_w.as_ref() {
503-
proxy_items.push(quote! {
504-
#[doc = #doc]
505-
#[inline(always)]
506-
pub fn #sc(self) -> &'a mut W {
507-
self.variant(#enum_::#pc)
508-
}
509-
});
510-
} else {
511518
proxy_items.push(quote! {
512519
#[doc = #doc]
513520
#[inline(always)]
514521
pub fn #sc(self) -> &'a mut W {
515522
self.variant(#pc_w::#pc)
516523
}
517524
});
518-
}
519525
}
520526
}
521527

@@ -556,7 +562,6 @@ pub fn fields(
556562
}
557563
});
558564

559-
let _pc_w = &f._pc_w;
560565
mod_items.push(quote! {
561566
///Proxy
562567
pub struct #_pc_w<'a> {
@@ -680,7 +685,7 @@ fn add_from_variants(mod_items: &mut Vec<Tokens>, variants: &Vec<Variant>, pc: &
680685
});
681686
}
682687

683-
fn derive_from_base(mod_items: &mut Vec<Tokens>, base: &Base, pc: &Ident, base_pc: &Ident, fname: &str) -> quote::Tokens {
688+
fn derive_from_base(mod_items: &mut Vec<Tokens>, base: &Base, pc: &Ident, base_pc: &Ident, fname: &str) {
684689
let desc = format!("Possible values of the field `{}`", fname,);
685690

686691
if let (Some(peripheral), Some(register)) = (&base.peripheral, &base.register) {
@@ -694,10 +699,6 @@ fn derive_from_base(mod_items: &mut Vec<Tokens>, base: &Base, pc: &Ident, base_p
694699
pub type #pc =
695700
crate::#pmod_::#rmod_::#base_pc;
696701
});
697-
698-
quote! {
699-
crate::#pmod_::#rmod_::#base_pc
700-
}
701702
} else if let Some(register) = &base.register {
702703
let mod_ = register.to_sanitized_snake_case();
703704
let mod_ = Ident::from(&*mod_);
@@ -707,19 +708,11 @@ fn derive_from_base(mod_items: &mut Vec<Tokens>, base: &Base, pc: &Ident, base_p
707708
pub type #pc =
708709
super::#mod_::#base_pc;
709710
});
710-
711-
quote! {
712-
super::#mod_::#base_pc
713-
}
714711
} else {
715712
mod_items.push(quote! {
716713
#[doc = #desc]
717714
pub type #pc = #base_pc;
718715
});
719-
720-
quote! {
721-
#base_pc
722-
}
723716
}
724717
}
725718

0 commit comments

Comments
 (0)