typewit/macros/
type_ne_macro.rs

1
2/// Constructs a [`TypeNe`](crate::TypeNe)
3/// of types that are statically known to be different.
4/// 
5/// This macro is syntactic sugar for calling 
6/// [`TypeNe::with_fn`](crate::TypeNe::with_fn) with a private 
7/// [`InjTypeFn`](crate::InjTypeFn) implementor.
8/// 
9/// # Syntax 
10/// 
11/// This macro takes this syntax:
12/// 
13/// ```text
14/// $( < $($generic_param:generic_param),* $(,)? > )?  $left_type:ty, $right_type:ty
15/// $(where $($where_cause:tt)*)?
16/// ```
17/// 
18/// # Limitations
19/// 
20/// This macro can't use generic parameters from the surrounding scope,
21/// they must be redeclared within the macro to be used.
22/// 
23/// # Example
24/// 
25/// ```rust
26/// use typewit::TypeNe;
27/// 
28/// const _: TypeNe<u8, (u8,)> = foo();
29/// 
30/// const fn foo<T>() -> TypeNe<T, (T,)> 
31/// where
32///     (T,): Copy,
33/// {
34///     typewit::type_ne!{
35///         <X>  X, (X,) 
36///         where (X,): Copy
37///     }
38/// }
39/// 
40/// 
41/// ```
42#[macro_export]
43macro_rules! type_ne {
44    (< $($generics:tt)* ) => {
45        $crate::__::__parse_in_generics! {
46            ($crate::__tyne_parsed_capture_generics !())
47            [] [] [$($generics)*]
48        }
49    };
50    ($left_ty:ty, $($rem:tt)*) => {
51        $crate::__tyne_parsed_capture_generics! {
52            []
53            []
54            []
55            $left_ty, $($rem)*
56        }
57    };
58    ($($rem:tt)*) => {
59        $crate::__::compile_error!{"invalid arguments for `type_ne` macro"}
60    }
61}
62
63
64#[doc(hidden)]
65#[macro_export]
66macro_rules! __tyne_parsed_capture_generics {
67    (
68        [$(($gen_arg:tt ($($($gen_phantom:tt)+)?) $($gen_rem:tt)*))*]
69        [$(($($gen_params:tt)*))*]
70        $deleted_markers:tt
71
72        $left_ty:ty, $right_ty:ty $(,)?
73
74        $(where $($where:tt)*)?
75    ) => ({
76        struct __TypeNeParameterizer<$($($gen_params)*,)*>(
77            $crate::__::PhantomData<(
78                $($($crate::__::PhantomData<$($gen_phantom)+>,)?)*
79            )>
80        )$( where $($where)* )?;
81
82        impl<$($($gen_params)*,)*> __TypeNeParameterizer<$($gen_arg,)*> 
83        $( where $($where)* )?
84        {
85            const NEW: Self = Self($crate::__::PhantomData);
86        }
87
88        $crate::__impl_with_span! {
89            $left_ty // span
90            () // impl attrs
91            ( <$($($gen_params)*,)*> $crate::TypeFn<$crate::type_ne::LeftArg> )
92            // for
93            (__TypeNeParameterizer<$($gen_arg,)*>)
94            ( $( where $($where)* )? )
95            (
96                type Output = $left_ty;
97
98                const TYPE_FN_ASSERTS: () = { 
99                    let _: $crate::CallInjFn<Self, $crate::type_ne::LeftArg>; 
100                };
101            )
102        }
103        
104        $crate::__impl_with_span! {
105            $left_ty // span
106            () // impl attrs
107            ( <$($($gen_params)*,)*> $crate::RevTypeFn<$left_ty> )
108            // for
109            (__TypeNeParameterizer<$($gen_arg,)*>)
110            ( $( where $($where)* )? )
111            (
112                type Arg = $crate::type_ne::LeftArg;
113            )
114        }
115
116        $crate::__impl_with_span! {
117            $right_ty // span
118            () // impl attrs
119            ( <$($($gen_params)*,)*> $crate::TypeFn<$crate::type_ne::RightArg> )
120            // for
121            (__TypeNeParameterizer<$($gen_arg,)*>)
122            ( $( where $($where)* )? )
123            (
124                type Output = $right_ty;
125
126                const TYPE_FN_ASSERTS: () = { 
127                    let _: $crate::CallInjFn<Self, $crate::type_ne::RightArg>; 
128                };
129            )
130        }
131
132        $crate::__impl_with_span! {
133            $right_ty // span
134            () // impl attrs
135            ( <$($($gen_params)*,)*> $crate::RevTypeFn<$right_ty> )
136            // for
137            (__TypeNeParameterizer<$($gen_arg,)*>)
138            ( $( where $($where)* )? )
139            (
140                type Arg = $crate::type_ne::RightArg;
141            )
142        }
143
144        $crate::TypeNe::with_fn(
145            __TypeNeParameterizer::NEW
146        )
147    });
148}