Module constraint

Source
Expand description

Type-level numeric constraints with zero runtime cost.

This module lets you express numeric constraints like “non-negative”, “non-zero”, or “strictly positive” at the type level, with zero runtime overhead after construction.

With these types, your APIs and components can trust that values always satisfy the required numeric constraints. This guarantee leads to designs that are both safer and more self-documenting.

§Provided Constraints

The following marker types represent the most common numeric invariants:

Each marker can be used with the generic Constrained<T, C> wrapper, where C is the marker type implementing Constraint<T>. Each also provides an associated [new()] constructor for convenience.

See the documentation and tests for each constraint for more usage patterns.

§Extending

You can define custom numeric invariants by implementing Constraint<T> for your own zero-sized marker types.

Structs§

Constrained
A wrapper enforcing a numeric constraint at construction time.
NonNegative
Marker type enforcing that a value is non-negative (zero or greater).
NonPositive
Marker type enforcing that a value is non-positive (zero or less).
NonZero
Marker type enforcing that a value is non-zero (not equal to zero).
StrictlyNegative
Marker type enforcing that a value is strictly negative (less than zero).
StrictlyPositive
Marker type enforcing that a value is strictly positive (greater than zero).

Enums§

ConstraintError
An error returned when a Constraint is violated.

Traits§

Constraint
A trait for enforcing numeric invariants at construction time.