12 releases

Uses new Rust 2021

0.5.3 Sep 14, 2022
0.5.2 Apr 1, 2022
0.5.1 Jan 1, 2022
0.5.0 Jul 11, 2021
0.1.1 Jun 5, 2016

#70 in Rust patterns

Download history 366/week @ 2022-06-06 273/week @ 2022-06-13 428/week @ 2022-06-20 625/week @ 2022-06-27 347/week @ 2022-07-04 532/week @ 2022-07-11 688/week @ 2022-07-18 603/week @ 2022-07-25 732/week @ 2022-08-01 555/week @ 2022-08-08 667/week @ 2022-08-15 405/week @ 2022-08-22 412/week @ 2022-08-29 463/week @ 2022-09-05 331/week @ 2022-09-12 279/week @ 2022-09-19

1,503 downloads per month
Used in 13 crates (8 directly)

ISC license

86KB
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.

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

~165KB