typewit

Macro inj_type_fn

Source
macro_rules! inj_type_fn {
    ($($args:tt)*) => { ... };
}
Expand description

Declares an injective type-level function

This macro takes in the exact same syntax as the type_fn macro.

This macro generates the same items as the type_fn macro, in addition to implementing RevTypeFn, so that the function implements InjTypeFn.

§Example

This macro is also demonstrated in TypeNe::{map, project, unmap, unproject}.

§Basic

use typewit::{CallFn, UncallFn, inj_type_fn};
 
// Calls the `ToSigned` function with `u64` as the argument.
let _: CallFn<ToSigned, u64> = 3i64;
 
// Gets the argument of the `ToSigned` function from the `i8` return value.
let _: UncallFn<ToSigned, i8> = 5u8;
 
inj_type_fn!{
    struct ToSigned;
 
    impl u128 => i128;
    impl u64 => i64;
    impl u32 => i32;
    impl u16 => i16;
    impl u8 => i8;
}

the above inj_type_fn macro invocation roughly expands to this code

struct ToSigned;
 
impl ToSigned {
    const NEW: Self = Self;
}
 
impl ::typewit::TypeFn<u128> for ToSigned {
    type Output = i128;
}
 
impl ::typewit::RevTypeFn<i128> for ToSigned {
    type Arg = u128;
}
 
impl ::typewit::TypeFn<u64> for ToSigned {
    type Output = i64;
}
 
impl ::typewit::RevTypeFn<i64> for ToSigned {
    type Arg = u64;
}
 
impl ::typewit::TypeFn<u32> for ToSigned {
    type Output = i32;
}
 
impl ::typewit::RevTypeFn<i32> for ToSigned {
    type Arg = u32;
}
 
impl ::typewit::TypeFn<u16> for ToSigned {
    type Output = i16;
}
 
impl ::typewit::RevTypeFn<i16> for ToSigned {
    type Arg = u16;
}
 
impl ::typewit::TypeFn<u8> for ToSigned {
    type Output = i8;
}
 
impl ::typewit::RevTypeFn<i8> for ToSigned {
    type Arg = u8;
}