Crate bounded_integer

Source
Expand description

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.

Modules§

examples__doc
Examples of bounded integers generated by the bounded_integer! macro.

Macros§

bounded_integermacro
Generate a bounded integer type.
unsafe_api
Unsafely provide the bounded integer API for a custom type.

Structs§

BoundedI8
An i8 constrained to be in the range MIN..=MAX.
BoundedI16
An i16 constrained to be in the range MIN..=MAX.
BoundedI32
An i32 constrained to be in the range MIN..=MAX.
BoundedI64
An i64 constrained to be in the range MIN..=MAX.
BoundedI128
An i128 constrained to be in the range MIN..=MAX.
BoundedIsize
An isize constrained to be in the range MIN..=MAX.
BoundedU8
An u8 constrained to be in the range MIN..=MAX.
BoundedU16
An u16 constrained to be in the range MIN..=MAX.
BoundedU32
An u32 constrained to be in the range MIN..=MAX.
BoundedU64
An u64 constrained to be in the range MIN..=MAX.
BoundedU128
An u128 constrained to be in the range MIN..=MAX.
BoundedUsize
An usize constrained to be in the range MIN..=MAX.
ParseError
An error which can be returned when parsing a bounded integer.
TryFromError
An error returned when a checked conversion into a bounded integer fails.

Enums§

ParseErrorKind
The cause of the failure to parse the integer.