I'm trying to create a struct that enables a pin and then disables it after a period of time. I'm running into an issue of ownership and I'm not quite sure how to resolve it.
Here's the code I'm working with:
pub trait PumpPin {
fn enable(self);
fn disable(self);
}
impl PumpPin for Gpio4<Output> {
fn enable(mut self: Gpio4<Output>) {
self.set_high().unwrap();
}
fn disable(mut self: Gpio4<Output>) {
self.set_low().unwrap();
}
}
pub struct PumpManager<T: PumpPin + std::marker::Send + 'static + std::marker::Sync> {
pin: T,
}
impl<T: PumpPin + std::marker::Send + 'static + std::marker::Sync> PumpManager<T> {
pub fn new(pin: T) -> PumpManager<T> {
PumpManager { pin }
}
pub fn run_pump(self, time: u64) -> Result<(), EspError> {
self.pin.enable();
// Configure the timer to disable the pin
let once_timer = EspTimerService::new()
.unwrap()
.timer(move || self.pin.disable());
//actually set a time for the timer
once_timer.unwrap().after(Duration::from_secs(time))
}
}
My goal was to encapsulate all of the logic that handles the timing of the pump.
The error I get below is at self.pin.disable()
cannot move out of `self.pin`, as `self` is a captured variable in an `FnMut` closure
move occurs because `self.pin` has type `T`, which does not implement the `Copy` trait
I thought the ownership of pin would belong to PumpManager
itself but it actually belongs to whatever function is calling it. Is there a way I can refactor this to accomplish what I'm trying to do?