7 releases
Uses new Rust 2024
new 0.1.7 | Apr 15, 2025 |
---|---|
0.1.5 | Apr 14, 2025 |
#1810 in Procedural macros
558 downloads per month
Used in unitscale
18KB
223 lines
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 implementingUnitScale
.#[unit_data]
: Generates a generic struct for the unit and implementsTryFrom<f64>
,Scaled<U>
, andnew()
.
Example
use unitscale_core::*;
use unitscale_macros::*;
#[unit_scale(to = 0.1)]
struct Scale0_1;
#[unit_data]
struct Volts;
let v = Volts::<Scale0_1, u16>::try_from(3.2).unwrap();
// v.to_f64() gives back 3.2 within the precision limit of the type (e.g. u16)
assert_eq!(v.scaled_value(), 32);
Dependencies
~0.3–0.8MB
~18K SLoC