Skip to content

Commit bf0489c

Browse files
bors[bot]burrbull
andcommitted
Merge #358
358: One enum r=therealprof a=burrbull r? @therealprof Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents f129d7a + 5ccfc14 commit bf0489c

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
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/generic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn render() -> Result<Vec<Tokens>> {
2020
generic_items.push(quote! {
2121
///Value read from the register
2222
pub struct FR<U, T> {
23-
bits: U,
23+
pub(crate) bits: U,
2424
_reg: marker::PhantomData<T>,
2525
}
2626

src/generate/register.rs

+41-23
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,16 @@ 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 base_pc_w = None;
327+
let mut evs_r = None;
328+
329+
let _pc_r = &f._pc_r;
330+
let _pc_w = &f._pc_w;
331+
322332
if can_read {
323333
let cast = if f.width == 1 {
324334
quote! { != 0 }
@@ -328,18 +338,17 @@ pub fn fields(
328338
let value = if offset != 0 {
329339
let offset = &f.offset;
330340
quote! {
331-
((self.bits() >> #offset) & #mask) #cast
341+
((self.bits >> #offset) & #mask) #cast
332342
}
333343
} else {
334344
quote! {
335-
(self.bits() & #mask) #cast
345+
(self.bits & #mask) #cast
336346
}
337347
};
338348

339-
let pc_r = &f.pc_r;
340-
let _pc_r = &f._pc_r;
341-
342349
if let Some((evs, base)) = lookup_filter(&lookup_results, Usage::Read) {
350+
evs_r = Some(evs.clone());
351+
343352
let description = &util::escape_brackets(&f.description);
344353
let sc = &f.sc;
345354
r_impl_items.push(quote! {
@@ -350,11 +359,18 @@ pub fn fields(
350359
}
351360
});
352361

353-
if let Some(base) = &base {
362+
base_pc_w = base.as_ref().map(|base| {
354363
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);
357-
}
364+
let base_pc_r = Ident::from(&*format!("{}_A", pc));
365+
let base_pc_r = derive_from_base(mod_items, &base, &pc_r, &base_pc_r, f.name);
366+
367+
mod_items.push(quote! {
368+
///Reader of the field
369+
pub type #_pc_r = crate::FR<#fty, #base_pc_r>;
370+
});
371+
372+
base_pc_r
373+
});
358374

359375
if base.is_none() {
360376
let has_reserved_variant = evs.values.len() != (1 << f.width);
@@ -394,7 +410,7 @@ pub fn fields(
394410
#[inline(always)]
395411
pub fn variant(&self) -> crate::Variant<#fty, #pc_r> {
396412
use crate::Variant::*;
397-
match self.bits() {
413+
match self.bits {
398414
#(#arms),*
399415
}
400416
}
@@ -404,7 +420,7 @@ pub fn fields(
404420
///Enumerated values
405421
#[inline(always)]
406422
pub fn variant(&self) -> #pc_r {
407-
match self.bits() {
423+
match self.bits {
408424
#(#arms),*
409425
}
410426
}
@@ -471,16 +487,19 @@ pub fn fields(
471487
if variants.len() == 1 << f.width {
472488
unsafety = None;
473489
}
474-
let pc_w = &f.pc_w;
475490

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-
});
491+
if Some(evs) != evs_r.as_ref() {
492+
pc_w = &f.pc_w;
481493

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

486505
proxy_items.push(quote! {
@@ -556,7 +575,6 @@ pub fn fields(
556575
}
557576
});
558577

559-
let _pc_w = &f._pc_w;
560578
mod_items.push(quote! {
561579
///Proxy
562580
pub struct #_pc_w<'a> {

0 commit comments

Comments
 (0)