Typed Constants via Traits: Zero, One, ..., OneHundredTwentySeven
This crate provides a family of traits, one per integer constant in the range 0 through 127. Each trait defines a single associated constant whose value is implemented across common numeric types (i8, u8, i16, u16, i32, i64, usize, f32, f64, etc.).
The traits allow writing generic code that refers to numeric constants without requiring conversion or From bounds, and without depending on unstable generic constant features.
Motivation
When writing generic numeric code in Rust, one often wants to refer to numeric constants that work across different numeric types. However:
T::from(127) requires trait bounds and may not be available for all numeric types
as conversions are lossy when crossing from signed to unsigned or integer to float
Generic constant expressions are still limited in stable Rust
This crate provides a simple and predictable alternative: one trait per number, one associated constant, implemented for common numeric types.
Example
use OneHundredTwentySeven;
No casts, no conversion traits, no intermediate types.
Trait Structure
Each trait follows the same pattern:
and is implemented for a consistent set of numeric types:
All signed and unsigned integers: i8 u8 i16 u16 i32 u32 i64 u64 i128 u128 isize usize
Floating types: f32, f64
Example implementation:
// etc...
Naming Conventions
Traits are named using English number words: Zero, One, Two, ..., FortyTwo, ..., OneHundredTwentySeven
Associated constants follow the same English spelling, but in uppercase with underscores
This ensures:
-
Fully deterministic naming
-
No ambiguity regarding spelling, hyphens, short forms, or ordinals
-
Easy auto-completion in IDEs
Use Cases
-
Generic numeric algorithms
-
Compile-time bounds and limits
-
Avoiding lossy casts
-
Writing crates that work across integer and float types without feature gates
-
Type-directed constant selection
Status and Scope
Range: 0 to 127 inclusive
All traits are stable, pure Rust, and require no features
No dependencies
If desired, the range can be extended. Open an issue if you need OneHundredTwentyEight and beyond.
License
This project is available under:
-
MIT License
-
Apache License 2.0
At your choice.