embedded-hal
embedded-hal copied to clipboard
Using `Delay` via shared references
trafficstars
This is mentioned in https://github.com/rust-embedded/book/issues/246: what is the recommended way to share a Delay?
Currently all the Delay methods take a mutable self reference, e.g. Delay::delay_x(&mut self, val: u32). This makes it pretty tricky to use a delay in more than one place since you only get one (at least in atsamd-hal). A couple possible solutions I see:
- HAL crates make their
Delaybe cloneable (possibly !Send/!Sync in these cases) or otherwise provide a way to get >1impl Delayobjects - Users provide a
impl Delaytype that is aRefCellaround aDelay. That is what I currently plan on doing, but it's kind of icky (10+ extra steps toborrow_muta delay is deadly) - Change these function signatures to take
&selfrather thanmut, and HAL crates internally keep a RefCell or something to make this work. Also icky, since your delays become failable. - Driver libraries always use Countdown instead of drivers
So, I'm just curious what the recommendation is