use crate::*;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Datum<T> {
pub time: Time,
pub value: T,
}
impl<T> Datum<T> {
pub const fn new(time: Time, value: T) -> Datum<T> {
Datum {
time: time,
value: value,
}
}
pub fn replace_if_older_than(&mut self, maybe_replace_with: Self) -> bool {
if maybe_replace_with.time > self.time {
*self = maybe_replace_with;
return true;
}
false
}
}
pub trait OptionDatumExt<T> {
fn replace_if_none_or_older_than(&mut self, maybe_replace_with: Datum<T>) -> bool;
fn replace_if_none_or_older_than_option(&mut self, maybe_replace_with: Self) -> bool;
}
impl<T> OptionDatumExt<T> for Option<Datum<T>> {
fn replace_if_none_or_older_than(&mut self, maybe_replace_with: Datum<T>) -> bool {
if let Some(self_datum) = self {
if self_datum.time >= maybe_replace_with.time {
return false;
}
}
*self = Some(maybe_replace_with);
true
}
fn replace_if_none_or_older_than_option(&mut self, maybe_replace_with: Self) -> bool {
let maybe_replace_with = match maybe_replace_with {
Some(x) => x,
None => return false,
};
self.replace_if_none_or_older_than(maybe_replace_with)
}
}
impl<T: Not<Output = O>, O> Not for Datum<T> {
type Output = Datum<O>;
fn not(self) -> Datum<O> {
Datum::new(self.time, !self.value)
}
}
impl<T: Neg<Output = O>, O> Neg for Datum<T> {
type Output = Datum<O>;
fn neg(self) -> Datum<O> {
Datum::new(self.time, -self.value)
}
}
impl<T: Add<Output = O>, O> Add for Datum<T> {
type Output = Datum<O>;
fn add(self, other: Self) -> Datum<O> {
let output_value = self.value + other.value;
let output_time = if self.time >= other.time {
self.time
} else {
other.time
};
Datum::new(output_time, output_value)
}
}
impl<T: AddAssign> AddAssign for Datum<T> {
fn add_assign(&mut self, other: Self) {
self.value += other.value;
self.time = if self.time >= other.time {
self.time
} else {
other.time
};
}
}
impl<T: Add<Output = O>, O> Add<T> for Datum<T> {
type Output = Datum<O>;
fn add(self, other: T) -> Datum<O> {
let output_value = self.value + other;
Datum::new(self.time, output_value)
}
}
impl<T: AddAssign> AddAssign<T> for Datum<T> {
fn add_assign(&mut self, other: T) {
self.value += other;
}
}
impl<T: Sub<Output = O>, O> Sub for Datum<T> {
type Output = Datum<O>;
fn sub(self, other: Self) -> Datum<O> {
let output_value = self.value - other.value;
let output_time = if self.time >= other.time {
self.time
} else {
other.time
};
Datum::new(output_time, output_value)
}
}
impl<T: SubAssign> SubAssign for Datum<T> {
fn sub_assign(&mut self, other: Self) {
self.value -= other.value;
self.time = if self.time >= other.time {
self.time
} else {
other.time
};
}
}
impl<T: Sub<Output = O>, O> Sub<T> for Datum<T> {
type Output = Datum<O>;
fn sub(self, other: T) -> Datum<O> {
let output_value = self.value - other;
Datum::new(self.time, output_value)
}
}
impl<T: SubAssign> SubAssign<T> for Datum<T> {
fn sub_assign(&mut self, other: T) {
self.value -= other;
}
}
impl<T: Mul<Output = O>, O> Mul for Datum<T> {
type Output = Datum<O>;
fn mul(self, other: Self) -> Datum<O> {
let output_value = self.value * other.value;
let output_time = if self.time >= other.time {
self.time
} else {
other.time
};
Datum::new(output_time, output_value)
}
}
impl<T: MulAssign> MulAssign for Datum<T> {
fn mul_assign(&mut self, other: Self) {
self.value *= other.value;
self.time = if self.time >= other.time {
self.time
} else {
other.time
};
}
}
impl<T: Mul<Output = O>, O> Mul<T> for Datum<T> {
type Output = Datum<O>;
fn mul(self, other: T) -> Datum<O> {
let output_value = self.value * other;
Datum::new(self.time, output_value)
}
}
impl<T: MulAssign> MulAssign<T> for Datum<T> {
fn mul_assign(&mut self, other: T) {
self.value *= other;
}
}
impl<T: Div<Output = O>, O> Div for Datum<T> {
type Output = Datum<O>;
fn div(self, other: Self) -> Datum<O> {
let output_value = self.value / other.value;
let output_time = if self.time >= other.time {
self.time
} else {
other.time
};
Datum::new(output_time, output_value)
}
}
impl<T: DivAssign> DivAssign for Datum<T> {
fn div_assign(&mut self, other: Self) {
self.value /= other.value;
self.time = if self.time >= other.time {
self.time
} else {
other.time
};
}
}
impl<T: Div<Output = O>, O> Div<T> for Datum<T> {
type Output = Datum<O>;
fn div(self, other: T) -> Datum<O> {
let output_value = self.value / other;
Datum::new(self.time, output_value)
}
}
impl<T: DivAssign> DivAssign<T> for Datum<T> {
fn div_assign(&mut self, other: T) {
self.value /= other;
}
}
impl Mul<Datum<f32>> for Datum<State> {
type Output = Self;
fn mul(self, other: Datum<f32>) -> Self {
let output_value = self.value * other.value;
let output_time = if self.time >= other.time {
self.time
} else {
other.time
};
Datum::new(output_time, output_value)
}
}
impl MulAssign<Datum<f32>> for Datum<State> {
fn mul_assign(&mut self, other: Datum<f32>) {
self.value *= other.value;
self.time = if self.time >= other.time {
self.time
} else {
other.time
};
}
}
impl Mul<f32> for Datum<State> {
type Output = Self;
fn mul(self, other: f32) -> Self {
let output_value = self.value * other;
Datum::new(self.time, output_value)
}
}
impl MulAssign<f32> for Datum<State> {
fn mul_assign(&mut self, other: f32) {
self.value *= other;
}
}
impl Div<Datum<f32>> for Datum<State> {
type Output = Self;
fn div(self, other: Datum<f32>) -> Self {
let output_value = self.value / other.value;
let output_time = if self.time >= other.time {
self.time
} else {
other.time
};
Datum::new(output_time, output_value)
}
}
impl DivAssign<Datum<f32>> for Datum<State> {
fn div_assign(&mut self, other: Datum<f32>) {
self.value /= other.value;
self.time = if self.time >= other.time {
self.time
} else {
other.time
};
}
}
impl Div<f32> for Datum<State> {
type Output = Self;
fn div(self, other: f32) -> Self {
let output_value = self.value / other;
Datum::new(self.time, output_value)
}
}
impl DivAssign<f32> for Datum<State> {
fn div_assign(&mut self, other: f32) {
self.value /= other;
}
}
impl Mul<Datum<f32>> for Datum<Command> {
type Output = Self;
fn mul(self, other: Datum<f32>) -> Self {
let output_value = self.value * other.value;
let output_time = if self.time >= other.time {
self.time
} else {
other.time
};
Datum::new(output_time, output_value)
}
}
impl MulAssign<Datum<f32>> for Datum<Command> {
fn mul_assign(&mut self, other: Datum<f32>) {
self.value *= other.value;
self.time = if self.time >= other.time {
self.time
} else {
other.time
};
}
}
impl Mul<f32> for Datum<Command> {
type Output = Self;
fn mul(self, other: f32) -> Self {
let output_value = self.value * other;
Datum::new(self.time, output_value)
}
}
impl MulAssign<f32> for Datum<Command> {
fn mul_assign(&mut self, other: f32) {
self.value *= other;
}
}
impl Div<Datum<f32>> for Datum<Command> {
type Output = Self;
fn div(self, other: Datum<f32>) -> Self {
let output_value = self.value / other.value;
let output_time = if self.time >= other.time {
self.time
} else {
other.time
};
Datum::new(output_time, output_value)
}
}
impl DivAssign<Datum<f32>> for Datum<Command> {
fn div_assign(&mut self, other: Datum<f32>) {
self.value /= other.value;
self.time = if self.time >= other.time {
self.time
} else {
other.time
};
}
}
impl Div<f32> for Datum<Command> {
type Output = Self;
fn div(self, other: f32) -> Self {
let output_value = self.value / other;
Datum::new(self.time, output_value)
}
}
impl DivAssign<f32> for Datum<Command> {
fn div_assign(&mut self, other: f32) {
self.value /= other;
}
}