-
Notifications
You must be signed in to change notification settings - Fork 156
Closed
Description
Moved from stm32-rs-nightlies
I ran into an issue when writing to a register where I wanted to return a value from the write, like this:
let val = reg.write(|w| {
T::set(w.field())
});
But, the signature of write
forbids this:
pub fn write<F, T>(&self, f: F)
where
F: FnOnce(&mut REG::Writer) -> &mut W<REG>,
So I'm curious, why not change the signature to:
pub fn write<F, T>(&self, f: F) -> T
where
F: FnOnce(&mut REG::Writer) -> T
By implementing it like this:
pub fn write<F, T>(&self, f: F) -> T
where
F: FnOnce(&mut W<REG>) -> T,
{
let mut writer = W {
bits: REG::RESET_VALUE & !REG::ONE_TO_MODIFY_FIELDS_BITMAP
| REG::ZERO_TO_MODIFY_FIELDS_BITMAP,
_reg: marker::PhantomData,
};
let result = f(&mut writer);
self.register.set(writer.bits);
result
}
Is there a drawback to this?
Metadata
Metadata
Assignees
Labels
No labels