2 releases
Uses old Rust 2015
0.1.1 | Dec 13, 2017 |
---|---|
0.1.0 | Dec 13, 2017 |
#25 in #fail
26 downloads per month
7KB
116 lines
Rust RangeType
An Ada-inspired Range type for Rust.
Documentation
Here.
Features
Compile-time checks
The following will fail to compile:
#[macro_use]
extern crate rangetype;
fn main() {
// Range with a value of 5 that should be within in range [-5, 2]
let x = range!(5, -5..2);
}
Run-time checks
// Will panic since the two numbers are for different ranges
let x = range!(5, 0..10);
let y = range!(10, 10..128);
let z = x + y; // panic!
// Will panic because 5 + 10 = 15 which will overflow the range of 0..10
let x = range!(5, 0..10);
let y = range!(10, 0..10);
let z = x + y; // panic!
lib.rs
:
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.
Dependencies
~46KB