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 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 struct
s and enum
s. 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 withstd
— impliesalloc
. Enables the following things:- An implementation of
Error
forParseError
. - Support for indexing with the const-generic integers on
VecDeque
.
- An implementation of
alloc
: Interopate withalloc
. Enables the following things:- Support for indexing with the const-generic integers on
Vec
.
- Support for indexing with the const-generic integers on
macro
: Enable thebounded_integer!
macro.types
: Enable the bounded integer types that use const generics.arbitrary1
: ImplementArbitrary
for the bounded integers. This is useful when using bounded integers as fuzzing inputs.bytemuck1
: ImplementContiguous
for all bounded integers, andZeroable
for macro-generated bounded integers that support it.num-traits02
: ImplementBounded
,AsPrimitive
,FromPrimitive
,NumCast
,ToPrimitive
,CheckedAdd
,CheckedDiv
,CheckedMul
,CheckedNeg
,CheckedRem
,CheckedSub
,MulAdd
,SaturatingAdd
,SaturatingMul
andSaturatingSub
for all const-generic bounded integers.serde1
: ImplementSerialize
andDeserialize
for the bounded integers, making sure all values will never be out of bounds. This has a deprecated aliasserde
.zerocopy06
: ImplementAsBytes
for all bounded integers, andUnaligned
for macro-generated ones.step_trait
: Implement theStep
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.
Modules
- Examples of bounded integers generated by the
bounded_integer!
macro.
Macros
- bounded_integer
macro
Generate a bounded integer type.
Structs
- BoundedI8
types
Ani8
constrained to be in the rangeMIN..=MAX
. - BoundedI16
types
Ani16
constrained to be in the rangeMIN..=MAX
. - BoundedI32
types
Ani32
constrained to be in the rangeMIN..=MAX
. - BoundedI64
types
Ani64
constrained to be in the rangeMIN..=MAX
. - BoundedI128
types
Ani128
constrained to be in the rangeMIN..=MAX
. - BoundedIsize
types
Anisize
constrained to be in the rangeMIN..=MAX
. - BoundedU8
types
Anu8
constrained to be in the rangeMIN..=MAX
. - BoundedU16
types
Anu16
constrained to be in the rangeMIN..=MAX
. - BoundedU32
types
Anu32
constrained to be in the rangeMIN..=MAX
. - BoundedU64
types
Anu64
constrained to be in the rangeMIN..=MAX
. - BoundedU128
types
Anu128
constrained to be in the rangeMIN..=MAX
. - BoundedUsize
types
Anusize
constrained to be in the rangeMIN..=MAX
. - An error which can be returned when parsing a bounded integer.
Enums
- The cause of the failure to parse the integer.