Ranged integers [nightly only]
Note: the library relies on the incomplete Rust features:
- It causes ICEson some Rust toolchains,
- The usage may increase the time compilation time heavily.
- The current version (0.11.0) was tested on nightly-2026-01-31.
Provides a generic type Ranged<MIN, MAX> representing an integer
within a specified range with the following features:
- Auto data size. The data size is chosen guided by the range specified, so
sizeof<Ranged<-50, 50>>is 1, whilesizeof<Ranged<-20_000, 100_000>>is 4. - Auto bounds recalculation. During arithmetic operations, the bounds of the results are calculated at compile time, so the errors such as possible overflow and zero division are caught.
- Array slicing. The fixed size arrays may be indexed with a ranged integer having the fitting bounds, so the bounds check is performed at compile time. The result of the slicing yields arrays rather than unsized slices.
- Const compatibility. The Ranged operations work in const context.
Example
extern crate ranged_integers;
use *;
// Consider a simple race game. The player rolls a
// die and then moves forward, backward or forward
// with the double speed according to some rules.
// Get a die roll using a standard random number generator
// Calculate where the player must move
// The result fits the range -6..=12
6, 12>