#bounded-integer #integer #refinement #macro #bounded

macro bounded-integer-macro

Proc macro for bounded-integer. Do not use directly.

13 releases

Uses new Rust 2024

0.6.0 Aug 8, 2025
0.5.8 Jan 18, 2025
0.5.6 Mar 18, 2023
0.5.3 Sep 14, 2022
0.2.0 Jun 4, 2020

#1756 in Procedural macros

Download history 225/week @ 2025-08-05 307/week @ 2025-08-12 310/week @ 2025-08-19 151/week @ 2025-08-26 292/week @ 2025-09-02 200/week @ 2025-09-09 221/week @ 2025-09-16 296/week @ 2025-09-23 355/week @ 2025-09-30 372/week @ 2025-10-07 272/week @ 2025-10-14 362/week @ 2025-10-21 491/week @ 2025-10-28 808/week @ 2025-11-04 587/week @ 2025-11-11 415/week @ 2025-11-18

2,356 downloads per month
Used in 3 crates (via bounded-integer)

ISC license

24KB
427 lines

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

~72KB