Skip to content

Commit c1085b3

Browse files
bors[bot]burrbull
andauthored
Merge #595
595: const generic field array r=therealprof a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents bbd3a79 + 3801ace commit c1085b3

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
- Generate const generic version of field array only if `const_generic` enabled
1011
- Clean `FieldReader`
1112
- Optional PascalCase for Enum values instead of UPPER_CASE
1213

src/generate/register.rs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,6 @@ pub fn fields(
606606
if can_write {
607607
let new_pc_aw = Ident::new(&(name_pc.clone() + "_AW"), span);
608608
let name_pc_w = Ident::new(&(name_pc.clone() + "_W"), span);
609-
let name_pc_cgw = Ident::new(&(name_pc.clone() + "_CGW"), span);
610609

611610
let mut proxy_items = TokenStream::new();
612611
let mut unsafety = unsafety(f.write_constraint.as_ref(), width);
@@ -752,27 +751,27 @@ pub fn fields(
752751
};
753752

754753
if field_dim.is_some() {
755-
mod_items.extend(quote! {
756-
#[doc = #doc]
757-
pub struct #name_pc_w<'a> {
758-
w: &'a mut W,
759-
offset: usize,
760-
}
761-
762-
impl<'a> #name_pc_w<'a> {
763-
#proxy_items
764-
#proxy_items_fa
765-
}
766-
});
754+
if !config.const_generic {
755+
mod_items.extend(quote! {
756+
#[doc = #doc]
757+
pub struct #name_pc_w<'a> {
758+
w: &'a mut W,
759+
offset: usize,
760+
}
767761

768-
if config.const_generic {
762+
impl<'a> #name_pc_w<'a> {
763+
#proxy_items
764+
#proxy_items_fa
765+
}
766+
});
767+
} else {
769768
mod_items.extend(quote! {
770769
#[doc = #cgdoc]
771-
pub struct #name_pc_cgw<'a, const O: usize> {
770+
pub struct #name_pc_w<'a, const O: usize> {
772771
w: &'a mut W,
773772
}
774773

775-
impl<'a, const O: usize> #name_pc_cgw<'a, O> {
774+
impl<'a, const O: usize> #name_pc_w<'a, O> {
776775
#proxy_items
777776
#proxy_items_cg
778777
}
@@ -792,15 +791,25 @@ pub fn fields(
792791
}
793792

794793
if let Some((first, dim, increment, suffixes, suffixes_str)) = &field_dim {
795-
let offset_calc = calculate_offset(*first, *increment, offset, false);
796794
let doc = &util::replace_suffix(&description, suffixes_str);
797-
w_impl_items.extend(quote! {
798-
#[doc = #doc]
799-
#inline
800-
pub unsafe fn #name_sc(&mut self, n: usize) -> #name_pc_w {
801-
#name_pc_w { w: self, offset: #offset_calc }
802-
}
803-
});
795+
if !config.const_generic {
796+
let offset_calc = calculate_offset(*first, *increment, offset, false);
797+
w_impl_items.extend(quote! {
798+
#[doc = #doc]
799+
#inline
800+
pub unsafe fn #name_sc(&mut self, n: usize) -> #name_pc_w {
801+
#name_pc_w { w: self, offset: #offset_calc }
802+
}
803+
});
804+
} else {
805+
w_impl_items.extend(quote! {
806+
#[doc = #doc]
807+
#inline
808+
pub unsafe fn #name_sc<const O: usize>(&mut self) -> #name_pc_w<O> {
809+
#name_pc_w { w: self }
810+
}
811+
});
812+
}
804813
for (i, suffix) in (0..*dim).zip(suffixes.iter()) {
805814
let sub_offset = offset + (i as u64) * (*increment as u64);
806815
let name_sc_n = Ident::new(
@@ -824,8 +833,8 @@ pub fn fields(
824833
w_impl_items.extend(quote! {
825834
#[doc = #doc]
826835
#inline
827-
pub fn #name_sc_n(&mut self) -> #name_pc_cgw<#sub_offset> {
828-
#name_pc_cgw { w: self }
836+
pub fn #name_sc_n(&mut self) -> #name_pc_w<#sub_offset> {
837+
#name_pc_w { w: self }
829838
}
830839
});
831840
}

0 commit comments

Comments
 (0)