1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
pub mod axis;
pub mod led;
pub mod spindle;

use alloc::boxed::Box;

use crate::error::{BoxError, Error};
use core::task::Poll;

// receive inspired by https://github.com/rtic-rs/rfcs/pull/0052
// poll inspired by https://docs.rs/stepper
pub trait Actuator {
    type Action;
    type Error: Error;

    fn run(&mut self, action: &Self::Action);
    fn poll(&mut self) -> Poll<Result<(), Self::Error>>;
}

pub type BoxActuator<Action> = Box<dyn Actuator<Action = Action, Error = BoxError>>;

pub struct BoxifyActuator<A: Actuator>(A);

impl<A: Actuator> BoxifyActuator<A> {
    pub fn new(actuator: A) -> Self {
        Self(actuator)
    }
}

impl<A: Actuator> Actuator for BoxifyActuator<A>
where
    A::Error: 'static,
{
    type Action = A::Action;
    type Error = BoxError;

    fn run(&mut self, action: &Self::Action) {
        self.0.run(action)
    }
    fn poll(&mut self) -> Poll<Result<(), Self::Error>> {
        self.0
            .poll()
            .map_err(|error| (Box::new(error) as Box<dyn Error>).into())
    }
}