Skip to main content

num

Macro num 

Source
num!() { /* proc-macro */ }
Expand description

Converts a numeric literal into a Num type, generating a compile-time error if the literal is out of range for the type.

  • the type doesn’t have to implement Num, but it must implement FromIntLiteral and FromFloatLiteral for float literal support.

§Syntax

num!(<literal>) or num!(<literal>: <type>).

§Example

use newnum::*;

fn inc(value: &mut impl Num) {
   *value += num!(1)
}

§Compile-Time Error

Because of rust const-fn limitations, the error shown when the literal is out of range is cryptic and is shown at the macro call-site.

Example:

 use newnum::*;

 fn add_alot(value: &mut impl Num) {
     //        |<- ERROR: evaluation of `add_alot::num_macro_fn::<u8>::{constant#0}` failed
     //        |
     *value += num!(1000)
 }

 fn main() {
     let mut i = 5u8;
     add_alot(&mut i);
 }