Macro type_level_values::type_fn[][src]

macro_rules! type_fn {
    (   $(#[$attr_op:meta])*
        alias $op_name:ident[$lhs:ident$(,$param:ident)*] $(::$assoc_ty:ident)* =$trait_name:ident
        $(where[$($bound:tt)*])*
    ) => { ... };
    (inner_alias_associated_type; $lhs:ident :: $assoc:ident ) => { ... };
    (inner_alias_associated_type; $lhs:ident ) => { ... };
    (define_trait

        $(#[$attr_trait:meta])*
        trait=$trait_name:ident[$($param:ident),*]
        $(where[$($bound:tt)*])*

        $(#[$attr_type:meta])*
        type=$type_alias_name:ident

        $(#[$attr_op:meta])*
        fn_type=$op_name:ident
    ) => { ... };
    (
        $(#[$attr_above:meta])*
        $(captures($($bound_vars:ident $(= $bound_def:ty )* ),*)
            $(#[$attr_bellow:meta])*
        )*
        $(pub $(($($visibility:tt)*))*)*
        fn $($rest:tt)+
    ) => { ... };
    (inner-function-decl-struct;
        captures[$($bound_vars:ident $(= $bound_def:ty )* ),*]
        $(#[$attr:meta])*
        $(pub $(($($visibility:tt)*))*)*
        fn $op_name:ident $($rest:tt)*
    ) => { ... };
    (inner_function_decl0;
        captures[ $bound_vars:tt ]
        fn
        $(
            $op_name:ident
            $([$($param_type:tt)*])*
            ($($param:tt)*)
            $(where[$($bound:tt)*])*
            {
                $(let $variable:ident $(=$value:ty)*;)*
                $ret:ty
            }
        )+
    ) => { ... };
    (inner-function-decl1;
        captures[ $bound_vars:tt ]
        $op_name:ident
        []
        $($rest:tt)*
    ) => { ... };
    (inner-function-decl1;
        captures[ $bound_vars:tt ]
        $op_name:ident
        [$($param_type:tt)*]
        $($rest:tt)*
    ) => { ... };
    (inner-function-decl2;
        captures[ ($($bound_vars:ident),*) ]
        $op_name:ident
        [$($param_type:tt)*]
        ($($param:tt)*)
        $(where[$($bound:tt)*])*
        {
            $(let $variable:ident $(=$value:ty)*;)*
            $ret:ty
        }
    ) => { ... };
}

A macro for implementing TypeFn_ .

For usage examples of declaring a new TypeFn_ please look at the documentation for the TypeFn_ trait

Syntax for declaring a new TypeFn_

$( ... )* means repeated 0 or more times.

$( ... )+ means repeated 1 or more times.

$( ... )? means that this is optional.

< ... > is a syntactic variable,replaced with whatever it refers to.



$( captures( $( <captured_variable> ,)* ) )?

$( <visibility_specifier> )? fn
$(
    <function_name> $( [ <generic_params> ] )? ( $( <function_parameter:ty> ,)* )
    $( where [ <where_predicates> ] )
    {
        $( let <variable_name> $( = <type> )? ; )*
        <returned_type>
    }
)+

All the captures inside captures(...) get translated to type parameters on the generated struct.

The <visibility_specifier> gets translated to the visibility of the constructor for the generated struct.

Declaring a TypeFn_ alias for a pre-existing trait

Syntax

This example is not tested

$(#[<attribute>])*
alias <function_name> 
[<self_identifier> $( ,<type_param> )*] $( ::$assoc_ty:ident )? =<trait_name> 
$( where[ <where_predicates> ] )?

Example

#[macro_use]
extern crate type_level_values;

use std::ops::{Add,Deref};
use type_level_values::runtime_value::ConstTypeOf_;
use type_level_values::ops::*;
use type_level_values::prelude::*;
use type_level_values::extern_types::typenum::UnsignedInteger;

type_fn!{alias AdditionOp[This,Rhs]=Add}

type_fn!{alias DerefOp[This]::Target=Deref}

type_fn!{alias ConstTypeOfOp[This]::Type = ConstTypeOf_}

fn main(){
    let _:U10=TypeFn::<AdditionOp,(U2,U8)>::MTVAL;
    let _:U16=TypeFn::<AdditionOp,(U2,U14)>::MTVAL;
    
    let _:VariantPhantom<usize> =
        TypeFn::<DerefOp,Box<usize>>::T;
    let _:VariantPhantom<String>=
        TypeFn::<DerefOp,&'static String>::T;

    let _:VariantPhantom<BooleanType>=
        TypeFn::<ConstTypeOfOp, True >::T;
    let _:VariantPhantom<BooleanType>=
        TypeFn::<ConstTypeOfOp, False >::T;
    let _:VariantPhantom<UnsignedInteger>=
        TypeFn::<ConstTypeOfOp, U0 >::T;

}

Defining a new trait ,type alias and TypeFn_ for that trait

This is the way to define a trait for type-level values, defining a type alias for the trait, and defining a TypeFn_ which delegates to the trait.

Syntax


define_trait

$( #[ <attribute_for_trait> ] )*
trait= <name_of_trait> [ $( <type_parameter_of_trait> ),* ]
$( where[ <where_predicates> ] )?

$( #[ <attribute_for_type_alias> ] )*
type= <name_of_type_alias> t
    
$( #[ <attribute_for_TypeFn_impl_block> ] )*
fn_type=$op_name:ident

Example

#[macro_use]
extern crate type_level_values;

use type_level_values::prelude::*;
use type_level_values::ops::*;
use std::ops::*;

type_fn!{define_trait
    trait=Rotate_[By]
    type=Rotate
    fn_type=RotateOp
}

impl<This,By,Res0,Res1> Rotate_<By> for This
where
    This:Add<By,Output=Res0>,
    Res0:Rem<U16,Output=Res1>,
{
    type Output=Res1;
}

fn main(){

    let _:U8=Rotate::<U2,U6>::MTVAL;
    let _:U1=Rotate::<U11,U6>::MTVAL;

    let _:U8=TypeFn::<RotateOp,(U2,U6)>::MTVAL;
    let _:U1=TypeFn::<RotateOp,(U11,U6)>::MTVAL;

}