8 releases
Uses old Rust 2015
0.1.7 | Jan 9, 2018 |
---|---|
0.1.6 | Jan 8, 2018 |
#1979 in Rust patterns
29 downloads per month
11KB
184 lines
num_alias
Provides simple and useful macros to declare 'type and range checked' aliases for integers and floats.
#[macro_use]
extern crate num_alias;
fn main() {
// declare alias with range[3, 6)
int_alias!(Val, i32, 3 => 6);
let a = Val(5);
let b = Val(4);
// this code panics
let c = a * b;
}
lib.rs
:
Simple macros to declare 'type and range checked' aliases for integers and floats.
Examples
Basic usage: just aliasing float.
#[macro_use]
extern crate num_alias;
fn main() {
float_alias!(Fval, f64);
let a = Fval(5.0);
let b = Fval(4.0);
let c = (a * b).sqrt();
}
In addition, you can declare an alias with a 'range checked' constructor.
#[macro_use]
extern crate num_alias;
fn main() {
// declare alias with range[3, 6)
int_alias!(Val, i32, 3 => 6);
let a = Val(5);
let b = Val(4);
// this code panics
let c = a * b;
}
Note: When using with range, these macros declare aliases not as Tuple Struct, but as Normal Struct and declare global functions with their name(In above example, fn Val(i: i32)-> Val
is declared).
This spec is for ease of use, but make alias' range safety imcomplete(If you can construct an alias with default constructor(like Val{inner: 5}
), range cheking won't run.)