Rust doesn’t offer a very good way to specify that (m / s) * s is actually the same type as m.
This particular example is in the works:
#![feature(generic_const_exprs)] use std::ops::Mul; pub struct UnitValue<T, const M: i8, const KG: i8, const S: i8, const A: i8, const K: i8>(T); pub fn meters<T>(val: T) -> UnitValue::<T, 1, 0, 0, 0, 0> { UnitValue(val) } impl<T, U, const M: i8, const M_RHS: i8, const KG: i8, const KG_RHS: i8, const S: i8, const S_RHS: i8, const A: i8, const A_RHS: i8, const K: i8, const K_RHS: i8> Mul<UnitValue<U, M_RHS, KG_RHS, S_RHS, A_RHS, K_RHS>> for UnitValue<T, M, KG, S, A, K> where T: Mul<U>, UnitValue<T::Output, { M + M_RHS }, { KG + KG_RHS }, { S + S_RHS }, { A + A_RHS }, { K + K_RHS }>: , { type Output = UnitValue<T::Output, { M + M_RHS }, { KG + KG_RHS }, { S + S_RHS }, { A + A_RHS }, { K + K_RHS }>; fn mul(self, rhs: UnitValue<U, M_RHS, KG_RHS, S_RHS, A_RHS, K_RHS>) -> Self::Output { UnitValue(self.0 * rhs.0) } }
(with apologies to the candela)
uom does this well enough for me.
uom
While it’s not a general solution, for the example quoted it works fine.
This particular example is in the works:
(with apologies to the candela)
uomdoes this well enough for me.While it’s not a general solution, for the example quoted it works fine.