typenum

Macro op

source
macro_rules! op {
    ($($tail:tt)*) => { ... };
}
Expand description

Convenient type operations.

Any types representing values must be able to be expressed as idents. That means they need to be in scope.

For example, P5 is okay, but typenum::P5 is not.

You may combine operators arbitrarily, although doing so excessively may require raising the recursion limit.

ยงExample

#![recursion_limit="128"]
#[macro_use] extern crate typenum;
use typenum::consts::*;

fn main() {
    assert_type!(
        op!(min((P1 - P2) * (N3 + N7), P5 * (P3 + P4)) == P10)
    );
}

Operators are evaluated based on the operator precedence outlined here.

The full list of supported operators and functions is as follows:

*, /, %, +, -, <<, >>, &, ^, |, ==, !=, <=, >=, <, >, cmp, sqr, sqrt, abs, cube, pow, min, max, log2, gcd

They all expand to type aliases defined in the operator_aliases module. Here is an expanded list, including examples:


Operator *. Expands to Prod.

assert_type_eq!(op!(P2 * P3), P6);

Operator /. Expands to Quot.

assert_type_eq!(op!(P6 / P2), P3);

Operator %. Expands to Mod.

assert_type_eq!(op!(P5 % P3), P2);

Operator +. Expands to Sum.

assert_type_eq!(op!(P2 + P3), P5);

Operator -. Expands to Diff.

assert_type_eq!(op!(P2 - P3), N1);

Operator <<. Expands to Shleft.

assert_type_eq!(op!(U1 << U5), U32);

Operator >>. Expands to Shright.

assert_type_eq!(op!(U32 >> U5), U1);

Operator &. Expands to And.

assert_type_eq!(op!(U5 & U3), U1);

Operator ^. Expands to Xor.

assert_type_eq!(op!(U5 ^ U3), U6);

Operator |. Expands to Or.

assert_type_eq!(op!(U5 | U3), U7);

Operator ==. Expands to Eq.

assert_type_eq!(op!(P5 == P3 + P2), True);

Operator !=. Expands to NotEq.

assert_type_eq!(op!(P5 != P3 + P2), False);

Operator <=. Expands to LeEq.

assert_type_eq!(op!(P6 <= P3 + P2), False);

Operator >=. Expands to GrEq.

assert_type_eq!(op!(P6 >= P3 + P2), True);

Operator <. Expands to Le.

assert_type_eq!(op!(P4 < P3 + P2), True);

Operator >. Expands to Gr.

assert_type_eq!(op!(P5 < P3 + P2), False);

Operator cmp. Expands to Compare.

assert_type_eq!(op!(cmp(P2, P3)), Less);

Operator sqr. Expands to Square.

assert_type_eq!(op!(sqr(P2)), P4);

Operator sqrt. Expands to Sqrt.

assert_type_eq!(op!(sqrt(U9)), U3);

Operator abs. Expands to AbsVal.

assert_type_eq!(op!(abs(N2)), P2);

Operator cube. Expands to Cube.

assert_type_eq!(op!(cube(P2)), P8);

Operator pow. Expands to Exp.

assert_type_eq!(op!(pow(P2, P3)), P8);

Operator min. Expands to Minimum.

assert_type_eq!(op!(min(P2, P3)), P2);

Operator max. Expands to Maximum.

assert_type_eq!(op!(max(P2, P3)), P3);

Operator log2. Expands to Log2.

assert_type_eq!(op!(log2(U9)), U3);

Operator gcd. Expands to Gcf.

assert_type_eq!(op!(gcd(U9, U21)), U3);