embedded-hal icon indicating copy to clipboard operation
embedded-hal copied to clipboard

Using `Delay` via shared references

Open tgross35 opened this issue 2 years ago • 9 comments
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 Delay be cloneable (possibly !Send/!Sync in these cases) or otherwise provide a way to get >1 impl Delay objects
  • Users provide a impl Delay type that is a RefCell around a Delay. That is what I currently plan on doing, but it's kind of icky (10+ extra steps to borrow_mut a delay is deadly)
  • Change these function signatures to take &self rather than mut, 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

tgross35 avatar Jan 29 '23 02:01 tgross35