Skip to content

Commit 541b7d7

Browse files
committed
implement both owning and const generic field array writers
1 parent 26baf2c commit 541b7d7

File tree

4 files changed

+77
-36
lines changed

4 files changed

+77
-36
lines changed

.github/bors.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ status = [
1515
"ci-linux (stable, Spansion, x86_64-unknown-linux-gnu, linux)",
1616
"ci-linux (stable, STMicro, x86_64-unknown-linux-gnu, linux)",
1717
"ci-linux (stable, Toshiba, x86_64-unknown-linux-gnu, linux)",
18-
"ci-linux (1.40.0, Nordic, x86_64-unknown-linux-gnu, linux)",
18+
"ci-linux (1.51.0, Nordic, x86_64-unknown-linux-gnu, linux)",
1919
"ci-linux (stable, x86_64-apple-darwin, osx)",
2020
"ci-linux (stable, x86_64-pc-windows-msvc, windows)",
2121
]

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525

2626
include:
2727
# Test MSRV
28-
- rust: 1.40.0
28+
- rust: 1.51.0
2929
VENDOR: Nordic
3030
TARGET: x86_64-unknown-linux-gnu
3131
TRAVIS_OS_NAME: linux

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3030

3131
### Changed
3232

33+
- [breaking-change] support for "field arrays"
34+
3335
- [breaking-change] remove `Variant<U, ENUM_A>`, use `Option<ENUM_A>` instead
3436

3537
- split out register size type (`RawType`) from `ResetValue` trait

src/generate/register.rs

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ pub fn fields(
563563
if can_write {
564564
let new_pc_aw = Ident::new(&(name_pc.clone() + "_AW"), span);
565565
let name_pc_w = Ident::new(&(name_pc.clone() + "_W"), span);
566+
let name_pc_cgw = Ident::new(&(name_pc.clone() + "_CGW"), span);
566567

567568
let mut proxy_items = TokenStream::new();
568569
let mut unsafety = unsafety(f.write_constraint.as_ref(), width);
@@ -639,59 +640,97 @@ pub fn fields(
639640
});
640641
}
641642

642-
proxy_items.extend(if field_dim.is_some() {
643-
quote! {
643+
let mut proxy_items_fa = TokenStream::new();
644+
let mut proxy_items_cg = TokenStream::new();
645+
if field_dim.is_some() {
646+
proxy_items_fa.extend(quote! {
644647
///Writes raw bits to the field
645648
#inline
646649
pub #unsafety fn #bits(self, value: #fty) -> &'a mut W {
647650
self.w.bits = (self.w.bits & !(#hexmask << self.offset)) | ((value as #rty & #hexmask) << self.offset);
648651
self.w
649652
}
650-
}
651-
} else if offset != 0 {
652-
let offset = &util::unsuffixed(offset);
653-
quote! {
653+
});
654+
proxy_items_cg.extend(quote! {
654655
///Writes raw bits to the field
655656
#inline
656657
pub #unsafety fn #bits(self, value: #fty) -> &'a mut W {
657-
self.w.bits = (self.w.bits & !(#hexmask << #offset)) | ((value as #rty & #hexmask) << #offset);
658+
self.w.bits = (self.w.bits & !(#hexmask << O)) | ((value as #rty & #hexmask) << O);
658659
self.w
659660
}
660-
}
661+
});
661662
} else {
662-
quote! {
663-
///Writes raw bits to the field
664-
#inline
665-
pub #unsafety fn #bits(self, value: #fty) -> &'a mut W {
666-
self.w.bits = (self.w.bits & !#hexmask) | (value as #rty & #hexmask);
667-
self.w
663+
proxy_items.extend(if offset != 0 {
664+
let offset = &util::unsuffixed(offset);
665+
quote! {
666+
///Writes raw bits to the field
667+
#inline
668+
pub #unsafety fn #bits(self, value: #fty) -> &'a mut W {
669+
self.w.bits = (self.w.bits & !(#hexmask << #offset)) | ((value as #rty & #hexmask) << #offset);
670+
self.w
671+
}
668672
}
669-
}
670-
});
673+
} else {
674+
quote! {
675+
///Writes raw bits to the field
676+
#inline
677+
pub #unsafety fn #bits(self, value: #fty) -> &'a mut W {
678+
self.w.bits = (self.w.bits & !#hexmask) | (value as #rty & #hexmask);
679+
self.w
680+
}
681+
}
682+
});
683+
}
671684

672-
let doc;
673-
let offset_entry;
674-
if let Some((_, _, _, _, suffixes_str)) = &field_dim {
675-
doc = format!(
676-
"Fields `{}` writer - {}",
685+
let mut cgdoc = String::new();
686+
let doc = if let Some((_, _, _, _, suffixes_str)) = &field_dim {
687+
cgdoc = format!(
688+
"Fields `{}` const generic writer - {}",
677689
util::replace_suffix(&f.name, suffixes_str),
678690
description
679691
);
680-
offset_entry = quote! {offset: usize,};
692+
format!(
693+
"Fields `{}` writer - {}",
694+
util::replace_suffix(&f.name, suffixes_str),
695+
description
696+
)
681697
} else {
682-
doc = format!("Field `{}` writer - {}", f.name, description);
683-
offset_entry = quote! {};
684-
}
698+
format!("Field `{}` writer - {}", f.name, description)
699+
};
700+
701+
mod_items.extend(if field_dim.is_some() {
702+
quote! {
703+
#[doc = #doc]
704+
pub struct #name_pc_w<'a> {
705+
w: &'a mut W,
706+
offset: usize,
707+
}
708+
709+
impl<'a> #name_pc_w<'a> {
710+
#proxy_items
711+
#proxy_items_fa
712+
}
685713

686-
mod_items.extend(quote! {
687-
#[doc = #doc]
688-
pub struct #name_pc_w<'a> {
689-
w: &'a mut W,
690-
#offset_entry
714+
#[doc = #cgdoc]
715+
pub struct #name_pc_cgw<'a, const O: usize> {
716+
w: &'a mut W,
717+
}
718+
719+
impl<'a, const O: usize> #name_pc_cgw<'a, O> {
720+
#proxy_items
721+
#proxy_items_cg
722+
}
691723
}
724+
} else {
725+
quote! {
726+
#[doc = #doc]
727+
pub struct #name_pc_w<'a> {
728+
w: &'a mut W,
729+
}
692730

693-
impl<'a> #name_pc_w<'a> {
694-
#proxy_items
731+
impl<'a> #name_pc_w<'a> {
732+
#proxy_items
733+
}
695734
}
696735
});
697736

@@ -719,8 +758,8 @@ pub fn fields(
719758
w_impl_items.extend(quote! {
720759
#[doc = #doc]
721760
#inline
722-
pub fn #name_sc_n(&mut self) -> #name_pc_w {
723-
#name_pc_w { w: self, offset: #sub_offset }
761+
pub fn #name_sc_n(&mut self) -> #name_pc_cgw<#sub_offset> {
762+
#name_pc_cgw { w: self }
724763
}
725764
});
726765
}

0 commit comments

Comments
 (0)