use core::time::Duration;
use void::Void;
pub trait Device {
type Error;
fn step(&mut self, ctx: &StepContext) -> Result<(), Self::Error>;
}
impl<'a, D: Device> Device for &'a mut D {
type Error = D::Error;
fn step(&mut self, ctx: &StepContext) -> Result<(), Self::Error> {
(*self).step(ctx)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct StepContext {
pub position: i64,
pub step_time: Duration,
}
pub fn func_device<F, B, T>(
forward: F,
backward: B,
) -> impl Device<Error = Void>
where
F: FnMut() -> T,
B: FnMut() -> T,
{
Infallible {
forward,
backward,
previous_position: 0,
}
}
struct Infallible<F, B> {
previous_position: i64,
forward: F,
backward: B,
}
impl<F, B, T> Device for Infallible<F, B>
where
F: FnMut() -> T,
B: FnMut() -> T,
{
type Error = Void;
#[inline]
fn step(&mut self, ctx: &StepContext) -> Result<(), Self::Error> {
let diff = ctx.position - self.previous_position;
if diff > 0 {
(self.forward)();
} else if diff < 0 {
(self.backward)();
}
self.previous_position = ctx.position;
Ok(())
}
}
pub fn fallible_func_device<F, B, T, E>(
forward: F,
backward: B,
) -> impl Device<Error = E>
where
F: FnMut() -> Result<T, E>,
B: FnMut() -> Result<T, E>,
{
Fallible {
forward,
backward,
previous_position: 0,
}
}
struct Fallible<F, B> {
previous_position: i64,
forward: F,
backward: B,
}
impl<F, B, T, E> Device for Fallible<F, B>
where
F: FnMut() -> Result<T, E>,
B: FnMut() -> Result<T, E>,
{
type Error = E;
#[inline]
fn step(&mut self, ctx: &StepContext) -> Result<(), Self::Error> {
let diff = ctx.position - self.previous_position;
if diff > 0 {
(self.forward)()?;
} else if diff < 0 {
(self.backward)()?;
}
self.previous_position = ctx.position;
Ok(())
}
}