use super::{DivideBy, Flt, MeasureSystem as MS, MultiplyBy, UnitTrait as UT};
use core::fmt;
use core::marker::PhantomData;
use core::ops::{Add, Div, Mul, Sub};
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq)]
pub struct Measure<S: MS> {
pub system: PhantomData<S>,
pub val: Flt,
}
impl<S: MS> Measure<S> {
pub fn new<U: UT<S>>(unit: &U, val: Flt) -> Self {
let val = unit.to_base(val);
Self {
system: PhantomData,
val,
}
}
pub fn val_as<U: UT<S>>(&self, unit: &U) -> Flt {
unit.to_self(self.val)
}
pub fn as_base(&self) -> Flt {
self.val
}
}
impl<S: MS> fmt::Debug for Measure<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Measure")
.field("as_base", &self.val)
.finish()
}
}
impl<S: MS> Add for Measure<S> {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
system: PhantomData,
val: self.val + other.val,
}
}
}
impl<S: MS> Add for &Measure<S> {
type Output = Measure<S>;
fn add(self, other: Self) -> Measure<S> {
Measure::<S> {
system: PhantomData,
val: self.val + other.val,
}
}
}
impl<S: MS> Sub for Measure<S> {
type Output = Self;
fn sub(self, other: Self) -> Self {
Self {
system: PhantomData,
val: self.val - other.val,
}
}
}
impl<S: MS> Sub for &Measure<S> {
type Output = Measure<S>;
fn sub(self, other: Self) -> Measure<S> {
Measure::<S> {
system: PhantomData,
val: self.val - other.val,
}
}
}
impl<OTH: MS, S: MS + MultiplyBy<OTH>> Mul<Measure<OTH>> for Measure<S> {
type Output = Measure<S::Output>;
fn mul(self, other: Measure<OTH>) -> Measure<S::Output> {
Self::Output {
system: PhantomData,
val: self.val * other.val,
}
}
}
impl<OTH: MS, S: MS + DivideBy<OTH>> Div<Measure<OTH>> for Measure<S> {
type Output = Measure<S::Output>;
fn div(self, other: Measure<OTH>) -> Self::Output {
Self::Output {
system: PhantomData,
val: self.val / other.val,
}
}
}