@@ -563,6 +563,8 @@ pub fn fields(
563
563
if can_write {
564
564
let new_pc_aw = Ident :: new ( & ( name_pc. clone ( ) + "_AW" ) , span) ;
565
565
let name_pc_w = Ident :: new ( & ( name_pc. clone ( ) + "_W" ) , span) ;
566
+ #[ cfg( feature = "const-generic" ) ]
567
+ let name_pc_cgw = Ident :: new ( & ( name_pc. clone ( ) + "_CGW" ) , span) ;
566
568
567
569
let mut proxy_items = TokenStream :: new ( ) ;
568
570
let mut unsafety = unsafety ( f. write_constraint . as_ref ( ) , width) ;
@@ -639,61 +641,108 @@ pub fn fields(
639
641
} ) ;
640
642
}
641
643
642
- proxy_items. extend ( if field_dim. is_some ( ) {
643
- quote ! {
644
+ let mut proxy_items_fa = TokenStream :: new ( ) ;
645
+ #[ cfg( feature = "const-generic" ) ]
646
+ let mut proxy_items_cg = TokenStream :: new ( ) ;
647
+ if field_dim. is_some ( ) {
648
+ proxy_items_fa. extend ( quote ! {
644
649
///Writes raw bits to the field
645
650
#inline
646
651
pub #unsafety fn #bits( self , value: #fty) -> & ' a mut W {
647
652
self . w. bits = ( self . w. bits & !( #hexmask << self . offset) ) | ( ( value as #rty & #hexmask) << self . offset) ;
648
653
self . w
649
654
}
650
- }
651
- } else if offset != 0 {
652
- let offset = & util:: unsuffixed ( offset) ;
653
- quote ! {
655
+ } ) ;
656
+ #[ cfg( feature="const-generic" ) ]
657
+ proxy_items_cg. extend ( quote ! {
654
658
///Writes raw bits to the field
655
659
#inline
656
660
pub #unsafety fn #bits( self , value: #fty) -> & ' a mut W {
657
- self . w. bits = ( self . w. bits & !( #hexmask << #offset ) ) | ( ( value as #rty & #hexmask) << #offset ) ;
661
+ self . w. bits = ( self . w. bits & !( #hexmask << O ) ) | ( ( value as #rty & #hexmask) << O ) ;
658
662
self . w
659
663
}
660
- }
664
+ } ) ;
661
665
} 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
666
+ proxy_items. extend ( if offset != 0 {
667
+ let offset = & util:: unsuffixed ( offset) ;
668
+ quote ! {
669
+ ///Writes raw bits to the field
670
+ #inline
671
+ pub #unsafety fn #bits( self , value: #fty) -> & ' a mut W {
672
+ self . w. bits = ( self . w. bits & !( #hexmask << #offset) ) | ( ( value as #rty & #hexmask) << #offset) ;
673
+ self . w
674
+ }
668
675
}
669
- }
670
- } ) ;
676
+ } else {
677
+ quote ! {
678
+ ///Writes raw bits to the field
679
+ #inline
680
+ pub #unsafety fn #bits( self , value: #fty) -> & ' a mut W {
681
+ self . w. bits = ( self . w. bits & !#hexmask) | ( value as #rty & #hexmask) ;
682
+ self . w
683
+ }
684
+ }
685
+ } ) ;
686
+ }
671
687
672
- let doc;
673
- let offset_entry;
674
- if let Some ( ( _, _, _, _, suffixes_str) ) = & field_dim {
675
- doc = format ! (
688
+ #[ cfg( feature = "const-generic" ) ]
689
+ let mut cgdoc = String :: new ( ) ;
690
+ let doc = if let Some ( ( _, _, _, _, suffixes_str) ) = & field_dim {
691
+ #[ cfg( feature = "const-generic" ) ]
692
+ {
693
+ cgdoc = format ! (
694
+ "Fields `{}` const generic writer - {}" ,
695
+ util:: replace_suffix( & f. name, suffixes_str) ,
696
+ description
697
+ ) ;
698
+ }
699
+ format ! (
676
700
"Fields `{}` writer - {}" ,
677
701
util:: replace_suffix( & f. name, suffixes_str) ,
678
702
description
679
- ) ;
680
- offset_entry = quote ! { offset: usize , } ;
703
+ )
681
704
} else {
682
- doc = format ! ( "Field `{}` writer - {}" , f. name, description) ;
683
- offset_entry = quote ! { } ;
684
- }
705
+ format ! ( "Field `{}` writer - {}" , f. name, description)
706
+ } ;
685
707
686
- mod_items. extend ( quote ! {
687
- #[ doc = #doc]
688
- pub struct #name_pc_w<' a> {
689
- w: & ' a mut W ,
690
- #offset_entry
691
- }
708
+ if field_dim. is_some ( ) {
709
+ mod_items. extend ( quote ! {
710
+ #[ doc = #doc]
711
+ pub struct #name_pc_w<' a> {
712
+ w: & ' a mut W ,
713
+ offset: usize ,
714
+ }
692
715
693
- impl <' a> #name_pc_w<' a> {
694
- #proxy_items
695
- }
696
- } ) ;
716
+ impl <' a> #name_pc_w<' a> {
717
+ #proxy_items
718
+ #proxy_items_fa
719
+ }
720
+ } ) ;
721
+
722
+ #[ cfg( feature = "const-generic" ) ]
723
+ mod_items. extend ( quote ! {
724
+ #[ doc = #cgdoc]
725
+ pub struct #name_pc_cgw<' a, const O : usize > {
726
+ w: & ' a mut W ,
727
+ }
728
+
729
+ impl <' a, const O : usize > #name_pc_cgw<' a, O > {
730
+ #proxy_items
731
+ #proxy_items_cg
732
+ }
733
+ } ) ;
734
+ } else {
735
+ mod_items. extend ( quote ! {
736
+ #[ doc = #doc]
737
+ pub struct #name_pc_w<' a> {
738
+ w: & ' a mut W ,
739
+ }
740
+
741
+ impl <' a> #name_pc_w<' a> {
742
+ #proxy_items
743
+ }
744
+ } ) ;
745
+ }
697
746
698
747
if let Some ( ( first, dim, increment, suffixes, suffixes_str) ) = & field_dim {
699
748
let offset_calc = calculate_offset ( * first, * increment, offset) ;
@@ -716,13 +765,22 @@ pub fn fields(
716
765
& suffix,
717
766
) ;
718
767
let sub_offset = util:: unsuffixed ( sub_offset as u64 ) ;
768
+ #[ cfg( not( feature = "const-generic" ) ) ]
719
769
w_impl_items. extend ( quote ! {
720
770
#[ doc = #doc]
721
771
#inline
722
772
pub fn #name_sc_n( & mut self ) -> #name_pc_w {
723
773
#name_pc_w { w: self , offset: #sub_offset }
724
774
}
725
775
} ) ;
776
+ #[ cfg( feature = "const-generic" ) ]
777
+ w_impl_items. extend ( quote ! {
778
+ #[ doc = #doc]
779
+ #inline
780
+ pub fn #name_sc_n( & mut self ) -> #name_pc_cgw<#sub_offset> {
781
+ #name_pc_cgw { w: self }
782
+ }
783
+ } ) ;
726
784
}
727
785
} else {
728
786
let doc = description_with_bits ( & description, offset, width) ;
0 commit comments