Re:Some notes on Rust, mutable aliasing and formal verification
by steveklabnik on Thursday May 16, 2024 @01:11PM
(#40380732)
Wouldn't you want the option outside? That's the usual pattern with "I want to assign later."
Anyway, I haven't seen this type used in the embedded projects I've worked on, do you have an example? Maybe I can suss out the reason from that.
Re:Some notes on Rust, mutable aliasing and formal verification
by the__alchemist on Thursday May 16, 2024 @01:53PM
(#40381225)
I don't have examples, as the OSS code bases I can find either A: Don't have practical examples, or B: Use RTIC or Embassy.
Here's what happens if you leave out the RefCell:
TEST.borrow(cs).replace(Test {});
error[E0596]: cannot borrow data in a `&` reference as mutable
If you use Option on the outside, I'm not sure how to then set or access the variable: static TEST: Mutex<Option<RefCell<Test>>> = Mutex::new(None);
What would you recommend as an alternative? Use case: Sharing state between interrupt handlers and similar.
Re:Some notes on Rust, mutable aliasing and formal verification
by steveklabnik on Thursday May 16, 2024 @04:43PM
(#40382878)
Ah! So this is an unfamiliarity issue on my part: I didn’t realize cortex_m::interrupt::Mutex has a different API than std::sync::Mutex or the various spinlock mutices. It deliberately only provides immutability because they want you to be able to choose how to do the interior mutability yourself. Now this all seems reasonable. Tricky!
(With hubris the interrupt stuff is abstracted away so you don’t need to access stuff this way, hence at least some of that unfamiliarity.)
Re:Some notes on Rust, mutable aliasing and formal verification
by the__alchemist on Thursday May 16, 2024 @04:54PM
(#40383002)
I appreciate the insight! Forgot to mention it was that Mutex. Of note, the syntax is kind of messy (Especially the access syntax, which I omitted), but it's not so bad with macros.
For copy types, you can use `Cell` instead. And of course, atomics for primitive types.