#macro #alias #convinience

num_alias

simple macros to declare ‘type checked’ aliases for integers and floats

8 releases

Uses old Rust 2015

0.1.7 Jan 9, 2018
0.1.6 Jan 8, 2018

#738 in Rust patterns

Download history 3/week @ 2022-01-17 4/week @ 2022-01-24 3/week @ 2022-01-31 1/week @ 2022-02-07 3/week @ 2022-02-14 10/week @ 2022-02-21 3/week @ 2022-02-28 2/week @ 2022-03-07 25/week @ 2022-03-14 26/week @ 2022-03-21 1/week @ 2022-03-28 3/week @ 2022-04-04 2/week @ 2022-04-11 3/week @ 2022-04-18 52/week @ 2022-04-25 58/week @ 2022-05-02

115 downloads per month

MIT license

11KB
184 lines

num_alias

Crates.io: num_alias Documentation Build Status License: MIT 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.)

No runtime deps