pub trait UnitConvert<To> {
fn convert(self) -> To;
}
pub trait Unit {
type BaseUnit;
fn to_base(self) -> Self::BaseUnit;
fn from_base(base: Self::BaseUnit) -> Self;
}
pub trait UnitInto<To> {
fn convert(self) -> To;
}
impl<T, Mid, To> UnitInto<To> for T
where
T: Unit<BaseUnit = Mid>,
To: Unit<BaseUnit = Mid>,
{
fn convert(self) -> To {
To::from_base(self.to_base())
}
}
pub trait BaseInto<To> {
fn to_non_base(self) -> To;
}
impl<T, To> BaseInto<To> for T
where
Self: Unit<BaseUnit = Self>,
To: Unit<BaseUnit = Self>,
{
fn to_non_base(self) -> To {
To::from_base(self)
}
}
pub trait BaseIntoBase<To>
where
To: Unit<BaseUnit = To>,
{
fn convert_base(self) -> To;
}
pub trait NonBaseIntoOuter<To> {
fn convert_outer(self) -> To;
}
impl<T, Base, ToBase, To> NonBaseIntoOuter<To> for T
where
T: Unit<BaseUnit = Base>,
To: Unit<BaseUnit = ToBase>,
Base: Unit + BaseIntoBase<ToBase>,
ToBase: Unit<BaseUnit = ToBase>,
{
fn convert_outer(self) -> To {
To::from_base(self.to_base().convert_base())
}
}