#primitive #number #from #into #as

number-as

Extend primitive number types to make them have a number_as method

3 stable releases

1.0.2 Nov 14, 2018
1.0.1 Sep 4, 2018

#10 in Value formatting

Download history 9/week @ 2018-10-08 24/week @ 2018-10-15 98/week @ 2018-10-22 367/week @ 2018-10-29 347/week @ 2018-11-05 1077/week @ 2018-11-12 137/week @ 2018-11-19 7/week @ 2018-11-26 16/week @ 2018-12-03 53/week @ 2018-12-10 6/week @ 2018-12-17 120/week @ 2018-12-24 282/week @ 2018-12-31

846 downloads per month
Used in 7 crates (1 directly)

MIT license

6KB
66 lines

Number As

Build Status Build status

Use the trait NumberAs in the current scope to let all primitive number types have a number_as method.

extern crate number_as;

use number_as::NumberAs;

let a: i32 = 2u16.number_as();

assert_eq!(2i32, a);

assert_eq!(2i32, 2u16.number_as());

assert_eq!(20i32, 20.6.number_as());

All implements for the NumberAs trait can be considered as a whole Primitive Number. If you want to design an add function, which accepts all kinds of primitive numbers, you can use the Number trait as a trait bound.

extern crate number_as;

use number_as::{Number, NumberAs};

fn add<T, J, K>(a: T, b: J) -> K where T: Number, J: Number, K: Number, i128: number_as::NumberAs<K> {
    let a: i128 = a.number_as();
    let b: i128 = b.number_as();

    let c = a + b;

    c.number_as()
}

assert_eq!(40i32, add(5u8, 35i16));

Crates.io

https://crates.io/crates/number-as

Documentation

https://docs.rs/number-as

License

MIT

No runtime deps