Expand description
§unitscale_macros
§Overview
unitscale_macros provides procedural macros to simplify creating unit types and scale constants. This crate depends on unitscale_core and is intended to reduce boilerplate.
It exports two attribute macros:
#[unit_scale(to = float)]: Declares a scale struct implementingUnitScaleF32.#[unit_data]: Generates a generic struct for the unit and implementsTryFrom<f32>,Scaled<U>, andnew().- Optional
withto specify"f32"or"f64"precision (Defaults tof32).- Note You cannot mix and match
f32andf64versions of UnitTypeF* and UnitScaleF*.
- Note You cannot mix and match
§Example
use unitscale_core::*;
use unitscale_macros::*;
// Default is `f32`. If you want f64 then you can add `with = "f64"`.
// #[unit_scale(to = 0.1, with = "f64")]
#[unit_scale(to = 0.1)]
struct Scale0_1;
// #[unit_type(with = "f64")]
#[unit_type]
struct Volts;
let v = Volts::<Scale0_1, u16>::try_from(3.2).unwrap();
// v.to_f32()) gives back 3.2 within the precision limit of the type (e.g. u16)
assert_eq!(v.scaled_value(), 32);Attribute Macros§
- unit_
scale unit_scalemacro to create a valid scaler- unit_
type unit_typemacro to create a valid scaled value bounds ofU