@@ -956,11 +956,10 @@ macro_rules! register {
956956 (
957957 @bitfield $( #[ $attr: meta] ) * $vis: vis struct $name: ident( $storage: ty) { $( $fields: tt) * }
958958 ) => {
959- $crate:: register! ( @bitfield_core
959+ $crate:: bitfield! (
960960 #[ allow( non_camel_case_types) ]
961- $( #[ $attr] ) * $vis $name $storage
961+ $( #[ $attr] ) * $vis struct $name( $storage) { $ ( $fields ) * }
962962 ) ;
963- $crate:: register!( @bitfield_fields $vis $name $storage { $( $fields) * } ) ;
964963 } ;
965964
966965 // Implementations shared by all registers types.
@@ -1016,245 +1015,4 @@ macro_rules! register {
10161015
10171016 impl $crate:: io:: register:: RelativeRegisterArray for $name { }
10181017 } ;
1019-
1020- // Defines the wrapper `$name` type and its conversions from/to the storage type.
1021- ( @bitfield_core $( #[ $attr: meta] ) * $vis: vis $name: ident $storage: ty) => {
1022- $( #[ $attr] ) *
1023- #[ repr( transparent) ]
1024- #[ derive( Clone , Copy , PartialEq , Eq ) ]
1025- $vis struct $name {
1026- inner: $storage,
1027- }
1028-
1029- #[ allow( dead_code) ]
1030- impl $name {
1031- /// Creates a bitfield from a raw value.
1032- #[ inline( always) ]
1033- $vis const fn from_raw( value: $storage) -> Self {
1034- Self { inner: value }
1035- }
1036-
1037- /// Turns this bitfield into its raw value.
1038- ///
1039- /// This is similar to the [`From`] implementation, but is shorter to invoke in
1040- /// most cases.
1041- #[ inline( always) ]
1042- $vis const fn into_raw( self ) -> $storage {
1043- self . inner
1044- }
1045- }
1046-
1047- // SAFETY: `$storage` is `Zeroable` and `$name` is transparent.
1048- unsafe impl :: pin_init:: Zeroable for $name { }
1049-
1050- impl :: core:: convert:: From <$name> for $storage {
1051- #[ inline( always) ]
1052- fn from( val: $name) -> $storage {
1053- val. into_raw( )
1054- }
1055- }
1056-
1057- impl :: core:: convert:: From <$storage> for $name {
1058- #[ inline( always) ]
1059- fn from( val: $storage) -> $name {
1060- Self :: from_raw( val)
1061- }
1062- }
1063- } ;
1064-
1065- // Definitions requiring knowledge of individual fields: private and public field accessors,
1066- // and `Debug` implementation.
1067- ( @bitfield_fields $vis: vis $name: ident $storage: ty {
1068- $( $( #[ doc = $doc: expr] ) * $hi: literal: $lo: literal $field: ident
1069- $( ?=> $try_into_type: ty) ?
1070- $( => $into_type: ty) ?
1071- ;
1072- ) *
1073- }
1074- ) => {
1075- #[ allow( dead_code) ]
1076- impl $name {
1077- $(
1078- $crate:: register!( @private_field_accessors $vis $name $storage : $hi: $lo $field) ;
1079- $crate:: register!(
1080- @public_field_accessors $( #[ doc = $doc] ) * $vis $name $storage : $hi: $lo $field
1081- $( ?=> $try_into_type) ?
1082- $( => $into_type) ?
1083- ) ;
1084- ) *
1085- }
1086-
1087- $crate:: register!( @debug $name { $( $field; ) * } ) ;
1088- } ;
1089-
1090- // Private field accessors working with the exact `Bounded` type for the field.
1091- (
1092- @private_field_accessors $vis: vis $name: ident $storage: ty : $hi: tt: $lo: tt $field: ident
1093- ) => {
1094- :: kernel:: macros:: paste!(
1095- $vis const [ <$field: upper _RANGE>] : :: core:: ops:: RangeInclusive <u8 > = $lo..=$hi;
1096- $vis const [ <$field: upper _MASK>] : $storage =
1097- ( ( ( ( 1 << $hi) - 1 ) << 1 ) + 1 ) - ( ( 1 << $lo) - 1 ) ;
1098- $vis const [ <$field: upper _SHIFT>] : u32 = $lo;
1099- ) ;
1100-
1101- :: kernel:: macros:: paste!(
1102- fn [ <__ $field>] ( self ) ->
1103- :: kernel:: num:: Bounded <$storage, { $hi + 1 - $lo } > {
1104- // Left shift to align the field's MSB with the storage MSB.
1105- const ALIGN_TOP : u32 = $storage:: BITS - ( $hi + 1 ) ;
1106- // Right shift to move the top-aligned field to bit 0 of the storage.
1107- const ALIGN_BOTTOM : u32 = ALIGN_TOP + $lo;
1108-
1109- // Extract the field using two shifts. `Bounded::shr` produces the correctly-sized
1110- // output type.
1111- let val = :: kernel:: num:: Bounded :: <$storage, { $storage:: BITS } >:: from(
1112- self . inner << ALIGN_TOP
1113- ) ;
1114- val. shr:: <ALIGN_BOTTOM , { $hi + 1 - $lo } >( )
1115- }
1116-
1117- const fn [ <__with_ $field>] (
1118- mut self ,
1119- value: :: kernel:: num:: Bounded <$storage, { $hi + 1 - $lo } >,
1120- ) -> Self
1121- {
1122- const MASK : $storage = <$name>:: [ <$field: upper _MASK>] ;
1123- const SHIFT : u32 = <$name>:: [ <$field: upper _SHIFT>] ;
1124-
1125- let value = value. get( ) << SHIFT ;
1126- self . inner = ( self . inner & !MASK ) | value;
1127-
1128- self
1129- }
1130- ) ;
1131- } ;
1132-
1133- // Public accessors for fields infallibly (`=>`) converted to a type.
1134- (
1135- @public_field_accessors $( #[ doc = $doc: expr] ) * $vis: vis $name: ident $storage: ty :
1136- $hi: literal: $lo: literal $field: ident => $into_type: ty
1137- ) => {
1138- :: kernel:: macros:: paste!(
1139-
1140- $( #[ doc = $doc] ) *
1141- #[ doc = "Returns the value of this field." ]
1142- #[ inline( always) ]
1143- $vis fn $field( self ) -> $into_type
1144- {
1145- self . [ <__ $field>] ( ) . into( )
1146- }
1147-
1148- $( #[ doc = $doc] ) *
1149- #[ doc = "Sets this field to the given `value`." ]
1150- #[ inline( always) ]
1151- $vis fn [ <with_ $field>] ( self , value: $into_type) -> Self
1152- {
1153- self . [ <__with_ $field>] ( value. into( ) )
1154- }
1155-
1156- ) ;
1157- } ;
1158-
1159- // Public accessors for fields fallibly (`?=>`) converted to a type.
1160- (
1161- @public_field_accessors $( #[ doc = $doc: expr] ) * $vis: vis $name: ident $storage: ty :
1162- $hi: tt: $lo: tt $field: ident ?=> $try_into_type: ty
1163- ) => {
1164- :: kernel:: macros:: paste!(
1165-
1166- $( #[ doc = $doc] ) *
1167- #[ doc = "Returns the value of this field." ]
1168- #[ inline( always) ]
1169- $vis fn $field( self ) ->
1170- Result <
1171- $try_into_type,
1172- <$try_into_type as :: core:: convert:: TryFrom <
1173- :: kernel:: num:: Bounded <$storage, { $hi + 1 - $lo } >
1174- >>:: Error
1175- >
1176- {
1177- self . [ <__ $field>] ( ) . try_into( )
1178- }
1179-
1180- $( #[ doc = $doc] ) *
1181- #[ doc = "Sets this field to the given `value`." ]
1182- #[ inline( always) ]
1183- $vis fn [ <with_ $field>] ( self , value: $try_into_type) -> Self
1184- {
1185- self . [ <__with_ $field>] ( value. into( ) )
1186- }
1187-
1188- ) ;
1189- } ;
1190-
1191- // Public accessors for fields not converted to a type.
1192- (
1193- @public_field_accessors $( #[ doc = $doc: expr] ) * $vis: vis $name: ident $storage: ty :
1194- $hi: tt: $lo: tt $field: ident
1195- ) => {
1196- :: kernel:: macros:: paste!(
1197-
1198- $( #[ doc = $doc] ) *
1199- #[ doc = "Returns the value of this field." ]
1200- #[ inline( always) ]
1201- $vis fn $field( self ) ->
1202- :: kernel:: num:: Bounded <$storage, { $hi + 1 - $lo } >
1203- {
1204- self . [ <__ $field>] ( )
1205- }
1206-
1207- $( #[ doc = $doc] ) *
1208- #[ doc = "Sets this field to the compile-time constant `VALUE`." ]
1209- #[ inline( always) ]
1210- $vis const fn [ <with_const_ $field>] <const VALUE : $storage>( self ) -> Self {
1211- self . [ <__with_ $field>] (
1212- :: kernel:: num:: Bounded :: <$storage, { $hi + 1 - $lo } >:: new:: <VALUE >( )
1213- )
1214- }
1215-
1216- $( #[ doc = $doc] ) *
1217- #[ doc = "Sets this field to the given `value`." ]
1218- #[ inline( always) ]
1219- $vis fn [ <with_ $field>] <T >(
1220- self ,
1221- value: T ,
1222- ) -> Self
1223- where T : Into <:: kernel:: num:: Bounded <$storage, { $hi + 1 - $lo } >>,
1224- {
1225- self . [ <__with_ $field>] ( value. into( ) )
1226- }
1227-
1228- $( #[ doc = $doc] ) *
1229- #[ doc = "Tries to set this field to `value`, returning an error if it is out of range." ]
1230- #[ inline( always) ]
1231- $vis fn [ <try_with_ $field>] <T >(
1232- self ,
1233- value: T ,
1234- ) -> :: kernel:: error:: Result <Self >
1235- where T : :: kernel:: num:: TryIntoBounded <$storage, { $hi + 1 - $lo } >,
1236- {
1237- Ok (
1238- self . [ <__with_ $field>] (
1239- value. try_into_bounded( ) . ok_or( :: kernel:: error:: code:: EOVERFLOW ) ?
1240- )
1241- )
1242- }
1243-
1244- ) ;
1245- } ;
1246-
1247- // `Debug` implementation.
1248- ( @debug $name: ident { $( $field: ident; ) * } ) => {
1249- impl :: kernel:: fmt:: Debug for $name {
1250- fn fmt( & self , f: & mut :: kernel:: fmt:: Formatter <' _>) -> :: kernel:: fmt:: Result {
1251- f. debug_struct( stringify!( $name) )
1252- . field( "<raw>" , & :: kernel:: prelude:: fmt!( "{:#x}" , self . inner) )
1253- $(
1254- . field( stringify!( $field) , & self . $field( ) )
1255- ) *
1256- . finish( )
1257- }
1258- }
1259- } ;
12601018}
0 commit comments