Std. Measurements
(Cure Standard Library v0.37.2)
View Source
Literal units of measure (design 2026-07-08-units-macro-design).
A number written immediately before a registered suffix expands, at parse
time, into the matching constructor call — no runtime cost beyond the
wrapped integer. The suffixes are globally active (like :syntax macros),
so use Std.Measurements is only needed to bring the operations and the wrapper
types into scope, not to enable the sugar.
500ms ==> Std.Measurements.ms(500) # a Duration of 500_000 microseconds 3khz ==> Std.Measurements.khz(3) # a Frequency of 3_000 hertz 80pct ==> Std.Measurements.pct(80) # a Percent 9600baud ==> Std.Measurements.baud(9600) # a Baud rate
Each quantity is a closed record over one Int, so arithmetic stays
same-unit: add/sub/scale are defined per quantity and there is no
cross-quantity operation, which is what makes 500ms and 3khz unmixable.
Examples
use Std.Measurements
let budget = add(500ms, 250ms) # a Duration of 750_000 microseconds
as_ms(budget) # => 750
Summary
Types
A serial signalling rate, stored as symbols per second.
A length of time, stored as microseconds.
A rate, stored as hertz.
A percentage. The wrapper keeps percentages from being mixed with bare
integers or other quantities; range enforcement (0..100) awaits the return
of refinement/Bounded literals and is not checked here.
Functions
Sum of two Durations.
The Duration truncated to whole milliseconds.
The Duration truncated to whole seconds.
The Duration as a whole number of microseconds.
A Baud rate of n symbols per second.
A Frequency of n hertz.
A Frequency of n kilohertz.
A Duration of n milliseconds.
A Percent of n.
A Duration of n seconds.
A Duration scaled by an integer factor.
Difference of two Durations (a - b).
A Duration of n microseconds.
Types
struct Baud
?
A serial signalling rate, stored as symbols per second.
struct Duration
?
A length of time, stored as microseconds.
struct Frequency
?
A rate, stored as hertz.
struct Percent
?
A percentage. The wrapper keeps percentages from being mixed with bare
integers or other quantities; range enforcement (0..100) awaits the return
of refinement/Bounded literals and is not checked here.
Functions
fn add(a: Duration, b: Duration) -> Duration
Sum of two Durations.
fn as_ms(d: Duration) -> Int
The Duration truncated to whole milliseconds.
fn as_s(d: Duration) -> Int
The Duration truncated to whole seconds.
fn as_us(d: Duration) -> Int
The Duration as a whole number of microseconds.
fn baud(n: Int) -> Baud
A Baud rate of n symbols per second.
fn hz(n: Int) -> Frequency
A Frequency of n hertz.
fn khz(n: Int) -> Frequency
A Frequency of n kilohertz.
fn ms(n: Int) -> Duration
A Duration of n milliseconds.
fn pct(n: Int) -> Percent
A Percent of n.
fn s(n: Int) -> Duration
A Duration of n seconds.
fn scale(d: Duration, k: Int) -> Duration
A Duration scaled by an integer factor.
fn sub(a: Duration, b: Duration) -> Duration
Difference of two Durations (a - b).
fn us(n: Int) -> Duration
A Duration of n microseconds.