Integers that are constrained within inclusive ranges
Constrained
types are represented simply as primitive integers, but their
values will always be contained by inclusive range bounds. The range is
defined at compile time, by assigning values to appropriate const generic
parameters. Constrained types provide fallible APIs for construction and
value assignment, they also implement wrapping, saturating, overflowing
and checked arithmetics for the range boundaries. See each desired type
documentation for more information.
The constrained_int
crate relies on the const_guards crate to define compile
time constraints, which itself uses the incomplete generic_const_exprs
feature. Therefore, this crate can only be compile with nightly, and, more
importantly, must be considered as an experimental crate only.
This crate is no_std
by default. See features section for more information.
Install
// Cargo.toml
[]
= "0.1"
Example
use ;
// Lower bound = -5, upper bound = 10, default = -1.
type Constrained = 5, 10, -1>;
type ConstrainedError = 5, 10>;
Documentation
This project documentation is hosted at docs.rs.
Feature flags
This crate does not link against the standard library by default, so it is
suitable for no_std
environments. It does provide a std
feature though,
that enables the standard library as a dependency. By enabling this crate's
std
feature, these additional features are provided:
- All crate's error types will implement the
std::error::Error
trait.
If users already are importing the standard library on their crate, enabling
std
feature comes at no additional cost.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Code review
It is recommended to always use cargo-crev to verify the trustworthiness of each of your dependencies, including this one.