### 11 releases

0.5.6 | Mar 18, 2023 |
---|---|

0.5.3 | Sep 14, 2022 |

0.5.2 | Apr 1, 2022 |

0.5.1 | Jan 1, 2022 |

0.2.0 | Jun 4, 2020 |

#**894** in Procedural macros

**235** downloads per month

Used in **2** crates
(via bounded-integer)

**ISC**license

74KB

1.5K
SLoC

# Bounded Integer

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

## Macro-generated bounded integers

The

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

`bounded_integer!` `{`
`struct` `MyInteger` `{` 0..8 `}`
`}`
`let` num `=` `MyInteger``::`new`(``5``)``.``unwrap``(``)``;`
`assert_eq!``(`num`,` `5``)``;`

This macro supports both

s and `struct`

s. See the `enum`

module for the
documentation of generated types.`examples`

## 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`

`no_std`

All the integers in this crate depend only on libcore and so work in

environments.`#!``[``no_std``]`

## Crate Features

By default, no crate features are enabled.

: Interopate with`std`

— implies`std`

. Enables the following things:`alloc`- An implementation of

for`Error`

.`ParseError`

- An implementation of

: Interopate with`alloc`

. Enables the following things:`alloc`- Support for indexing with the const-generic integers on

and`Vec`

.`VecDeque`

- Support for indexing with the const-generic integers on

: Enable the`macro`

macro.`bounded_integer!`

: Enable the bounded integer types that use const generics.`types`

: Implement`arbitrary1`

for the bounded integers. This is useful when using bounded integers as fuzzing inputs.`Arbitrary`

: Implement`bytemuck1`

for all bounded integers, and`Contiguous`

for macro-generated bounded integers that support it.`Zeroable`

: Implement`num-traits02`

,`Bounded`

,`AsPrimitive`

,`FromPrimitive`

,`NumCast`

,`ToPrimitive`

,`CheckedAdd`

,`CheckedDiv`

,`CheckedMul`

,`CheckedNeg`

,`CheckedRem`

,`CheckedSub`

,`MulAdd`

,`SaturatingAdd`

and`SaturatingMul`

for all const-generic bounded integers.`SaturatingSub`

: Implement`serde1`

and`Serialize`

for the bounded integers, making sure all values will never be out of bounds. This has a deprecated alias`Deserialize`

.`serde`

: Implement`zerocopy06`

for all bounded integers, and`AsBytes`

for macro-generated ones.`Unaligned`

: Implement the`step_trait`

trait which allows the bounded integers to be easily used in ranges. This will require you to use nightly and place`Step`

in your crate root if you use the macro.`#!``[``feature``(``step_trait``)``]`

## License

Copyright © 2016, Curtis McEnroe curtis@cmcenroe.me

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#### Dependencies

~0.7–1.2MB

~28K SLoC