19 releases

Uses new Rust 2024

0.6.1 Oct 17, 2025
0.5.8 Jan 18, 2025
0.5.7 Jun 15, 2023
0.5.6 Mar 18, 2023
0.1.1 Jun 5, 2016

#89 in Rust patterns

Download history 5928/week @ 2025-07-16 6270/week @ 2025-07-23 7077/week @ 2025-07-30 7045/week @ 2025-08-06 6253/week @ 2025-08-13 6269/week @ 2025-08-20 8644/week @ 2025-08-27 8644/week @ 2025-09-03 11149/week @ 2025-09-10 9031/week @ 2025-09-17 10815/week @ 2025-09-24 11119/week @ 2025-10-01 12231/week @ 2025-10-08 10875/week @ 2025-10-15 8941/week @ 2025-10-22 6632/week @ 2025-10-29

40,828 downloads per month
Used in 158 crates (26 directly)

ISC license

130KB
2.5K SLoC

This crate provides two types of bounded integer.

Macro-generated bounded integers

The bounded_integer! macro allows you to define your own bounded integer type, given a specific (inclusive) range it inhabits. For example:

bounded_integer! {
    struct MyInteger(0, 7);
}
let num = MyInteger::new(5).unwrap();
assert_eq!(num, 5);

This macro supports both structs and enums. See the examples module for the documentation of generated types.

Const generics-based bounded integers

You can also create ad-hoc bounded integers via types in this library that use const generics, for example:

let num = <BoundedU8<0, 7>>::new(5).unwrap();
assert_eq!(num, 5);

These integers are shorter to use as they don't require a type declaration or explicit name. However due to the limits of const generics, they may not implement some traits – namely Default, bytemuck’s Zeroable and zerocopy’s FromZeros. Also, unlike their macro counterparts they will not be subject to niche layout optimizations.

no_std

All the integers in this crate depend only on libcore and so work in #![no_std] environments.

Crate Features

By default, no crate features are enabled.

Dependencies

~0–440KB