Skip to content

Commit 456ce21

Browse files
committed
arbitrary return type for register modifiers
1 parent c6035f7 commit 456ce21

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

src/generate/generic_reg_vcell.rs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,20 @@ impl<REG: Resettable + Writable> Reg<REG> {
7474
/// ```
7575
/// In the latter case, other fields will be set to their reset value.
7676
#[inline(always)]
77-
pub fn write<F>(&self, f: F)
77+
pub fn write<F, T>(&self, f: F) -> T
7878
where
79-
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
79+
F: FnOnce(&mut W<REG>) -> T,
8080
{
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
8991
}
9092
}
9193

@@ -98,17 +100,20 @@ impl<REG: Writable> Reg<REG> {
98100
///
99101
/// Unsafe to use with registers which don't allow to write 0.
100102
#[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
102104
where
103-
F: FnOnce(&mut W<REG>) -> &mut W<REG>,
105+
F: FnOnce(&mut W<REG>) -> T,
104106
{
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
112117
}
113118
}
114119

@@ -139,31 +144,34 @@ impl<REG: Readable + Writable> Reg<REG> {
139144
/// ```
140145
/// Other fields will have the value they had before the call to `modify`.
141146
#[inline(always)]
142-
pub fn modify<F>(&self, f: F)
147+
pub fn modify<F, T>(&self, f: F) -> T
143148
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,
145150
{
146151
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,
160164
);
165+
166+
self.register.set(writer.bits);
167+
168+
result
161169
}
162170
}
163171

164172
impl<REG: Readable> core::fmt::Debug for crate::generic::Reg<REG>
165173
where
166-
R<REG>: core::fmt::Debug
174+
R<REG>: core::fmt::Debug,
167175
{
168176
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
169177
core::fmt::Debug::fmt(&self.read(), f)

0 commit comments

Comments
 (0)