@@ -74,18 +74,20 @@ impl<REG: Resettable + Writable> Reg<REG> {
74
74
/// ```
75
75
/// In the latter case, other fields will be set to their reset value.
76
76
#[ inline( always) ]
77
- pub fn write < F > ( & self , f : F )
77
+ pub fn write < F , T > ( & self , f : F ) -> T
78
78
where
79
- F : FnOnce ( & mut W < REG > ) -> & mut W < REG > ,
79
+ F : FnOnce ( & mut W < REG > ) -> T ,
80
80
{
81
- self . register . set (
82
- f ( & mut W {
83
- bits : REG :: RESET_VALUE & !REG :: ONE_TO_MODIFY_FIELDS_BITMAP
84
- | REG :: ZERO_TO_MODIFY_FIELDS_BITMAP ,
85
- _reg : marker:: PhantomData ,
86
- } )
87
- . bits ,
88
- ) ;
81
+ let mut writer = W {
82
+ bits : REG :: RESET_VALUE & !REG :: ONE_TO_MODIFY_FIELDS_BITMAP
83
+ | REG :: ZERO_TO_MODIFY_FIELDS_BITMAP ,
84
+ _reg : marker:: PhantomData ,
85
+ } ;
86
+ let result = f ( & mut writer) ;
87
+
88
+ self . register . set ( writer. bits ) ;
89
+
90
+ result
89
91
}
90
92
}
91
93
@@ -98,17 +100,20 @@ impl<REG: Writable> Reg<REG> {
98
100
///
99
101
/// Unsafe to use with registers which don't allow to write 0.
100
102
#[ inline( always) ]
101
- pub unsafe fn write_with_zero < F > ( & self , f : F )
103
+ pub unsafe fn write_with_zero < F , T > ( & self , f : F ) -> T
102
104
where
103
- F : FnOnce ( & mut W < REG > ) -> & mut W < REG > ,
105
+ F : FnOnce ( & mut W < REG > ) -> T ,
104
106
{
105
- self . register . set (
106
- f ( & mut W {
107
- bits : REG :: Ux :: default ( ) ,
108
- _reg : marker:: PhantomData ,
109
- } )
110
- . bits ,
111
- ) ;
107
+ let mut writer = W {
108
+ bits : REG :: Ux :: default ( ) ,
109
+ _reg : marker:: PhantomData ,
110
+ } ;
111
+
112
+ let result = f ( & mut writer) ;
113
+
114
+ self . register . set ( writer. bits ) ;
115
+
116
+ result
112
117
}
113
118
}
114
119
@@ -139,31 +144,34 @@ impl<REG: Readable + Writable> Reg<REG> {
139
144
/// ```
140
145
/// Other fields will have the value they had before the call to `modify`.
141
146
#[ inline( always) ]
142
- pub fn modify < F > ( & self , f : F )
147
+ pub fn modify < F , T > ( & self , f : F ) -> T
143
148
where
144
- for < ' w > F : FnOnce ( & R < REG > , & ' w mut W < REG > ) -> & ' w mut W < REG > ,
149
+ for < ' w > F : FnOnce ( & R < REG > , & ' w mut W < REG > ) -> T ,
145
150
{
146
151
let bits = self . register . get ( ) ;
147
- self . register . set (
148
- f (
149
- & R {
150
- bits,
151
- _reg : marker:: PhantomData ,
152
- } ,
153
- & mut W {
154
- bits : bits & !REG :: ONE_TO_MODIFY_FIELDS_BITMAP
155
- | REG :: ZERO_TO_MODIFY_FIELDS_BITMAP ,
156
- _reg : marker:: PhantomData ,
157
- } ,
158
- )
159
- . bits ,
152
+
153
+ let mut writer = W {
154
+ bits : bits & !REG :: ONE_TO_MODIFY_FIELDS_BITMAP | REG :: ZERO_TO_MODIFY_FIELDS_BITMAP ,
155
+ _reg : marker:: PhantomData ,
156
+ } ;
157
+
158
+ let result = f (
159
+ & R {
160
+ bits,
161
+ _reg : marker:: PhantomData ,
162
+ } ,
163
+ & mut writer,
160
164
) ;
165
+
166
+ self . register . set ( writer. bits ) ;
167
+
168
+ result
161
169
}
162
170
}
163
171
164
172
impl < REG : Readable > core:: fmt:: Debug for crate :: generic:: Reg < REG >
165
173
where
166
- R < REG > : core:: fmt:: Debug
174
+ R < REG > : core:: fmt:: Debug ,
167
175
{
168
176
fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
169
177
core:: fmt:: Debug :: fmt ( & self . read ( ) , f)
0 commit comments