Crate rangetype [−] [src]
This crate provides a numerical type that automatically performs range checks during all mathematical operations. If a range is violated the code will instantly panic.
When using the range!
macro you'll automatically get compile-time range
checks. You'll need to import the static_assertions
crate to use range!
.
You can also import the RangeType
, although using the macro is
recommended.
Example:
#[macro_use] extern crate rangetype; #[macro_use] extern crate static_assertions; use rangetype::RangeType; fn main() { // A value of 5 that must be between 0 and 128 let x = range!(32, 0..127); // You can also use floating-point types (and the constructor) let y = RangeType::new(4.5, 0.1..99.9); let z = range!(64, 0..127); // x + z = 96 println!("{}", x + z); // println!("{}", z + z); // This would panic // Ranges can be adjusted let a = x.with_range(0..255); // And the raw value can be retrieved like so let b = x.as_raw(); }
The Mul
, Div
, Add
, Sub
, and Neg
traits are implemented on the
RangeType
struct.
Macros
range |
Create a RangeType and employ static checks on its value. |
Structs
RangeType |
A range-checked number type. |