use super::*;
pub mod prelude
{
pub use super::PreviousValue;
pub use super::traits::*;
}
pub mod traits
{
pub use super::{Evolution, EvolutionWithContext};
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Default)]
pub struct PreviousValue<T>
where
T: Copy,
{
pub value: T,
pub old_value: T,
}
impl<V> PreviousValue<V>
where
V: Copy,
{
pub fn new(value: V, old_value: V) -> Self { Self { value, old_value } }
}
impl<T> Evolution<T> for PreviousValue<T>
where
T: Copy,
{
fn value(&self) -> T { self.value }
fn old_value(&self) -> T { self.old_value }
}
pub trait EvolutionWithContext<I, Ctx>
where
I: Copy,
{
fn value(&self, ctx: &mut Ctx) -> I;
fn old_value(&self, ctx: &mut Ctx) -> I;
fn delta(&self, ctx: &mut Ctx) -> I::Output
where
I: Sub,
{
self.value(ctx) - self.old_value(ctx)
}
fn evolution(&self, ctx: &mut Ctx) -> PreviousValue<I> { PreviousValue::new(self.value(ctx), self.old_value(ctx)) }
}
pub trait Evolution<I>
where
I: Copy,
{
fn value(&self) -> I;
fn old_value(&self) -> I;
fn delta(&self) -> I::Output
where
I: Sub,
{
self.value() - self.old_value()
}
fn evolution(&self) -> PreviousValue<I> { PreviousValue::new(self.value(), self.old_value()) }
}