Closed David-OConnor closed 4 years ago
Because the embedded_hal Delay trait requires a mutable reference to self it's not possible to share such a reference without using interior mutability with the help of something like RefCell
Here is a Rust playground example of doing such that to solve your problem. https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=826e9dbebd5075c1f7dada1c8e96ecba
use core::cell::RefCell;
trait Delay {
fn delay(&mut self, milli: u32);
}
#[derive(Clone, Copy)]
struct DelayProvider {}
impl Delay for DelayProvider {
fn delay(&mut self, milli: u32) {
println!("Delaying for {}ms", milli);
}
}
fn consumes_ownership<T: Delay>(mut delay: T, milli: u32) {
delay.delay(milli);
}
fn main() {
let delay = DelayProvider {};
let delay: RefCell<DelayProvider> = RefCell::new(delay);
consumes_ownership(*RefCell::borrow_mut(&delay), 42);
consumes_ownership(*RefCell::borrow_mut(&delay), 64);
}
Awesome. Was already writing the PR. Took a diff approach, where we never consume delay. It's subjective which approach is more elegant. Adv of yours is we don't have to keep passing delay
around. Adv of mine is we can avoid smart pointers, and consuming things that don't need to be consumed.
Working for me with PR merged
Ie, since the lcd requires consuming a delay, how do you use one later, like in the loop? The current examples don't have a delay in the loop.
How do you use the delay after this line ?