Skip to main content

impl_si_serialize_as

Macro impl_si_serialize_as 

Source
macro_rules! impl_si_serialize_as {
    ($prefix:ident, $qty_type:ty, $unit:path) => { ... };
}
Expand description

Generate a family of #[serde(serialize_with)]-compatible helpers that serialize a UOM SI quantity as a specific (non-base) unit.

Expands to helper functions covering wrapper combinations used in this codebase:

Generated functionField type
$prefix$qty_type
tracked_$prefixTrackedState<$qty_type>
opt_$prefixOption<$qty_type>
vec_$prefixVec<$qty_type>
vec_tracked_$prefixVec<TrackedState<$qty_type>>
tracked_opt_$prefixTrackedState<Option<$qty_type>>
opt_tracked_$prefixOption<TrackedState<$qty_type>>
vec_tracked_opt_$prefixVec<TrackedState<Option<$qty_type>>>

§Example

// In serde_helpers.rs:
use crate::{si, utils::tracked_state::TrackedState};
crate::impl_si_serialize_as!(power_as_kilowatts, si::Power, uom::si::power::kilowatt);

// In serde_api/serde_utils.rs serialize_with match:
"Power" => Some("crate::serde_helpers::power_as_kilowatts"),

// In the unit_impls match (must match the target unit):
"Power" => extract_units!(uom::si::power::kilowatt),