iliekturtles / uom

Units of measurement -- type-safe zero-cost dimensional analysis
Apache License 2.0
989 stars 89 forks source link

Base unit type constants for stable conversion #430

Closed zmrocze closed 11 months ago

zmrocze commented 12 months ago

Hey, I'd like to serialize values to floats, casually:

let position: Length;
let position_ser : f64 = position.value;  // in base unit

and now to deserialize I'd just need to know the base unit for the given quantity:

let position_deser = Length::new::<Length::BASE_UNIT>(positition_ser);

would it be possible to define such a constant? or is there another way?

iliekturtles commented 11 months ago

This is one of uom's warts right now. The best way at the current time is to skip the new constructor and create the struct directly:

let position_deser = Length { dimension: PhantomData, units: PhantomData, value: position_ser, };
iliekturtles commented 11 months ago

Closing this specific issue as the problem is covered elsewhere.