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

macro bounded-integer-macro

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

12 releases

new 0.5.8 Jan 18, 2025
0.5.6 Mar 18, 2023
0.5.3 Sep 14, 2022
0.5.2 Apr 1, 2022
0.2.0 Jun 4, 2020

#1002 in Procedural macros

Download history 111/week @ 2024-09-27 66/week @ 2024-10-04 68/week @ 2024-10-11 117/week @ 2024-10-18 56/week @ 2024-10-25 113/week @ 2024-11-01 63/week @ 2024-11-08 79/week @ 2024-11-15 100/week @ 2024-11-22 65/week @ 2024-11-29 206/week @ 2024-12-06 106/week @ 2024-12-13 86/week @ 2024-12-20 40/week @ 2024-12-27 78/week @ 2025-01-03 98/week @ 2025-01-10

322 downloads per month
Used in 2 crates (via bounded-integer)

ISC license

72KB
1.5K SLoC

Bounded Integer

Crate version docs.rs checks

This crate provides two types of bounded integer for use in Rust.

Macro-generated bounded integers

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

bounded_integer! {
    struct MyInteger { 0..8 }
}
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, and they interoperate better with other integers that have different ranges. However due to the limits of const generics, they do not implement some traits like Default.

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.

  • std: Interopate with std — implies alloc. Enables the following things:
  • alloc: Interopate with alloc. Enables the following things:
    • Support for indexing with the const-generic integers on Vec and VecDeque.
  • macro: Enable the bounded_integer! macro.
  • types: Enable the bounded integer types that use const generics.
  • arbitrary1: Implement Arbitrary for the bounded integers. This is useful when using bounded integers as fuzzing inputs.
  • bytemuck1: Implement Contiguous for all bounded integers, and Zeroable for macro-generated bounded integers that support it.
  • num-traits02: Implement Bounded, AsPrimitive, FromPrimitive, NumCast, ToPrimitive, CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedSub, MulAdd, SaturatingAdd, SaturatingMul and SaturatingSub for all const-generic bounded integers.
  • serde1: Implement Serialize and Deserialize for the bounded integers, making sure all values will never be out of bounds. This has a deprecated alias serde.
  • zerocopy: Implement IntoBytes for all bounded integers, and Unaligned for macro-generated ones.
  • step_trait: Implement the Step trait which allows the bounded integers to be easily used in ranges. This will require you to use nightly and place #![feature(step_trait)] in your crate root if you use the macro.

Dependencies

~0.6–1.1MB
~26K SLoC