Crates.io | number-as |
lib.rs | number-as |
version | 1.0.10 |
source | src |
created_at | 2018-09-04 13:30:49.234065 |
updated_at | 2022-03-18 03:05:06.305211 |
description | Extend primitive number types to make them have a `number_as` method. |
homepage | https://magiclen.org/number-as |
repository | https://github.com/magiclen/number-as |
max_upload_size | |
id | 82915 |
size | 6,381 |
Use the trait NumberAs
in the current scope to let all primitive number types have a number_as
method.
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.
use number_as::{Number, NumberAs};
fn add<T, J, K>(a: T, b: J) -> K where T: Number, J: Number, K: Number, i128: 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));
https://crates.io/crates/number-as