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.
std: Interopate withstd— impliesalloc. Enables the following things:- An implementation of
ErrorforParseError.
- An implementation of
alloc: Interopate withalloc. Has no effect currently.macro: Enable thebounded_integer!macro.arbitrary1: ImplementArbitraryfor the bounded integers. This is useful when using bounded integers as fuzzing inputs.bytemuck1: ImplementContiguousandNoUninitfor all bounded integers, andZeroablefor macro-generated bounded integers that support it.num-traits02: ImplementBounded,AsPrimitive,FromPrimitive,NumCast,ToPrimitive,CheckedAdd,CheckedDiv,CheckedMul,CheckedNeg,CheckedRem,CheckedSub,MulAdd,SaturatingAdd,SaturatingMulandSaturatingSubfor all bounded integers.serde1: ImplementSerializeandDeserializefor the bounded integers, making sure all values will never be out of bounds.zerocopy: ImplementIntoBytesandImmutablefor all bounded integers,Unalignedfor ones backed byu8ori8, andFromZerosfor suitable macro-generated ones.step_trait: Implement theSteptrait 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.
Modules§
- examples
__doc - Examples of bounded integers generated by the
bounded_integer!macro.
Macros§
- bounded_
integer - Generate a bounded integer type.
- unsafe_
api - Unsafely provide the bounded integer API for a custom type.
Structs§
- Bounded
I8 - An
i8constrained to be in the rangeMIN..=MAX. - Bounded
I16 - An
i16constrained to be in the rangeMIN..=MAX. - Bounded
I32 - An
i32constrained to be in the rangeMIN..=MAX. - Bounded
I64 - An
i64constrained to be in the rangeMIN..=MAX. - Bounded
I128 - An
i128constrained to be in the rangeMIN..=MAX. - Bounded
Isize - An
isizeconstrained to be in the rangeMIN..=MAX. - Bounded
U8 - An
u8constrained to be in the rangeMIN..=MAX. - Bounded
U16 - An
u16constrained to be in the rangeMIN..=MAX. - Bounded
U32 - An
u32constrained to be in the rangeMIN..=MAX. - Bounded
U64 - An
u64constrained to be in the rangeMIN..=MAX. - Bounded
U128 - An
u128constrained to be in the rangeMIN..=MAX. - Bounded
Usize - An
usizeconstrained to be in the rangeMIN..=MAX. - Parse
Error - An error which can be returned when parsing a bounded integer.
- TryFrom
Error - An error returned when a checked conversion into a bounded integer fails.
Enums§
- Parse
Error Kind - The cause of the failure to parse the integer.