@@ -413,7 +413,7 @@ pub fn fields(
413413 } ;
414414
415415 if let Some ( ( first, dim, increment, suffixes, suffixes_str) ) = & field_dim {
416- let offset_calc = calculate_offset ( * first, * increment, offset) ;
416+ let offset_calc = calculate_offset ( * first, * increment, offset, true ) ;
417417 let value = quote ! { ( ( self . bits >> #offset_calc) & #hexmask) #cast } ;
418418 let doc = & util:: replace_suffix ( & description, suffixes_str) ;
419419 r_impl_items. extend ( quote ! {
@@ -793,7 +793,7 @@ pub fn fields(
793793 }
794794
795795 if let Some ( ( first, dim, increment, suffixes, suffixes_str) ) = & field_dim {
796- let offset_calc = calculate_offset ( * first, * increment, offset) ;
796+ let offset_calc = calculate_offset ( * first, * increment, offset, false ) ;
797797 let doc = & util:: replace_suffix ( & description, suffixes_str) ;
798798 w_impl_items. extend ( quote ! {
799799 #[ doc = #doc]
@@ -985,7 +985,12 @@ fn add_from_variants(
985985 } ) ;
986986}
987987
988- fn calculate_offset ( first : u32 , increment : u32 , offset : u64 ) -> TokenStream {
988+ fn calculate_offset (
989+ first : u32 ,
990+ increment : u32 ,
991+ offset : u64 ,
992+ with_parentheses : bool ,
993+ ) -> TokenStream {
989994 let mut res = if first != 0 {
990995 let first = util:: unsuffixed ( first as u64 ) ;
991996 quote ! { n - #first }
@@ -1002,7 +1007,15 @@ fn calculate_offset(first: u32, increment: u32, offset: u64) -> TokenStream {
10021007 }
10031008 if offset != 0 {
10041009 let offset = & util:: unsuffixed ( offset) ;
1005- res = quote ! { #res + #offset } ;
1010+ if with_parentheses {
1011+ res = quote ! { ( #res + #offset) } ;
1012+ } else {
1013+ res = quote ! { #res + #offset } ;
1014+ }
1015+ } else {
1016+ if with_parentheses {
1017+ res = quote ! { ( #res) } ;
1018+ }
10061019 }
10071020 res
10081021}
0 commit comments