Generate safe bits writers when possible#554
Conversation
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @adamgreig (or someone else) soon. Please see the contribution instructions for more information. |
|
Explicit |
8f49896 to
3b57eac
Compare
I think The
Done! Good catch. |
I missed this is |
3b57eac to
abaff9c
Compare
This will not compile. |
if register
.fields
.as_ref()
.and_then(|fields| fields.iter().next())
.and_then(|field| field.write_constraint)
.and_then(|constraint| match constraint {
WriteConstraint::Range(range)
if range.min == 0 && range.max == 2_u64.pow(rsize) - 1 =>
{
None
}
_ => Some(()),
})
.is_some()
{
mod_items.extend(quote! {
#[doc = "Writes raw bits to the register."]
#[inline(always)]
pub unsafe fn bits(&mut self, bits: #rty) -> &mut Self {
self.0.bits(bits);
self
}
});
} else {
mod_items.extend(quote! {
#[doc = "Writes raw bits to the register."]
#[inline(always)]
pub fn bits(&mut self, bits: #rty) -> &mut Self {
unsafe { self.0.bits(bits); }
self
}
});
} |
|
I've looked at this twice now and I can't for the life of me work out what's going on here. If it's just me, then that's ok. But if anyone else is struggling, maybe some comments would be useful, or to break up the big chain into smaller pieces. But I love the theory, and this will clean up a bunch of code. |
abaff9c to
07a1f3c
Compare
Can you explain more? I am testing this with the STM32WL and stm32-rs, I can compile the generated crate with these changes. Edit: Nevermind, you're right. Something broke.
Good catch, that's much better. Changed. |
|
07a1f3c to
a02e5e1
Compare
Fixed! I forgot a
I broke it up to separate the logic of "can the write be safe" and the code generated as a result. Let me know if that helped :) |
The 'bits' method of register writers can be safe if: * there is a single field that covers the entire register * that field can represent all values
a02e5e1 to
4d6bcd4
Compare
|
bors r+ |
|
Build succeeded: |
The 'bits' method of register writers can be safe if: