1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! This macro defines set of common macros which are useful across different projects.


/// Allows for nicer definition of impls, similar to what Haskell or Scala does. Reduces the needed
/// boilerplate. For example, the following usage:
///
/// ```compile_fail
/// struct A { name:String };
/// impls! { From<A> for String { |t| t.name.clone() } }
/// ```
///
/// compiles to:
/// ```
/// struct A { name:String };
/// impl From<A> for String {
///     fn from(t:A) -> Self {
///         t.name.clone()
///     }
/// }
/// ```
///
/// This macro is meant to support many standard traits (like From) and should grow in the future.
/// Currently supported ones are:
/// * From<…>
/// * From + &From<…>
/// * Into + &Into<…>
/// * PhantomFrom<…>
#[macro_export]
macro_rules! impls {
    ($([$($impl_params:tt)*])? From<$ty:ty> for $target:ty $(where [$($bounds:tt)*])? {
        |$arg:tt| $($result:tt)*
    } ) => {
        #[allow(clippy::redundant_closure_call)]
        impl <$($($impl_params)*)?> From <$ty> for $target $(where $($bounds)*)? {
            fn from (arg:$ty) -> Self {
                (|$arg:$ty| $($result)*)(arg)
            }
        }
    };

    ($([$($impl_params:tt)*])? From + &From <$ty:ty> for $target:ty $(where [$($bounds:tt)*])? {
        |$arg:tt| $($result:tt)*
    } ) => {
        #[allow(clippy::redundant_closure_call)]
        #[allow(clippy::identity_conversion)]
        impl <$($($impl_params)*)?> From <$ty> for $target $(where $($bounds)*)? {
            fn from (arg:$ty) -> Self {
                (|$arg:$ty| $($result)*)(arg)
            }
        }

        #[allow(clippy::redundant_closure_call)]
        #[allow(clippy::identity_conversion)]
        impl <$($($impl_params)*)?> From <&$ty> for $target $(where $($bounds)*)? {
            fn from (arg:&$ty) -> Self {
                (|$arg:&$ty| $($result)*)(arg)
            }
        }
    };

    ($([$($impl_params:tt)*])? Into + &Into <$ty:ty> for $target:ty $(where [$($bounds:tt)*])? {
        |$arg:tt| $($result:tt)*
    } ) => {
        #[allow(clippy::redundant_closure_call)]
        #[allow(clippy::identity_conversion)]
        impl <$($($impl_params)*)?> Into <$ty> for $target $(where $($bounds)*)? {
            fn into(self) -> $ty {
                (|$arg:Self| $($result)*)(self)
            }
        }

        #[allow(clippy::redundant_closure_call)]
        #[allow(clippy::identity_conversion)]
        impl <$($($impl_params)*)?> Into <$ty> for &$target $(where $($bounds)*)? {
            fn into(self) -> $ty {
                (|$arg:Self| $($result)*)(self)
            }
        }
    };

    ($([$($impl_params:tt)*])? PhantomFrom<$ty:ty> for $target:ty {
        $($result:tt)*
    } ) => {
        impl <$($($impl_params)*)?> From <PhantomData<$ty>> for $target {
            fn from (_:PhantomData<$ty>) -> Self {
                $($result)*
            }
        }
    };
}

#[macro_export]
macro_rules! alias {
    ($( $(#$meta:tt)* $name:ident = {$($tok:tt)*} )*) => {$(
        $(#$meta)*
        pub trait $name: $($tok)* {}
        impl<T:$($tok)*> $name for T {}
    )*};

    (no_docs $( $(#$meta:tt)* $name:ident = {$($tok:tt)*} )*) => {$(
        $(#$meta)*
        #[allow(missing_docs)]
        pub trait $name: $($tok)* {}
        impl<T:$($tok)*> $name for T {}
    )*};
}



// ==============
// === Lambda ===
// ==============

/// Clones all arguments from the first argument list by using `CloneRef` and defines lambda with
/// arguments from the second argument list (if present). For example, the following usage
///
/// ```compile_fail
/// f! { (a,b)(c) a + b + c }
/// ```
///
/// is equivalent to:
///
/// ```compile_fail
/// {
///     let a = a.clone_ref();
///     let b = b.clone_ref();
///     move |c| { a + b + c }
/// }
/// ```
#[macro_export]
macro_rules! f {
    ([$($name:ident),*] ($($args:tt)*) $($expr:tt)*) => {
        {
            $(let $name = $name.clone_ref();)*
            move |$($args)*| { $($expr)* }
        }
    };

    ([$($name:ident),*] $($expr:tt)*) => {
        {
            $(let $name = $name.clone_ref();)*
            move || { $($expr)* }
        }
    };

    (($($args:tt)*) $name:ident . $($toks:tt)*) => {
        f! { [$name] ($($args)*) $name . $($toks)* }
    };

    (($($args:tt)*) { $name:ident . $($toks:tt)* }) => {
        f! { [$name] ($($args)*) { $name . $($toks)* } }
    };

    ($name:ident . $($toks:tt)*) => {
        f! { [$name] $name . $($toks)* }
    };
}

/// Variant of the `f` macro producing a lambda which drops its first argument.
#[macro_export]
macro_rules! f_ {
    ([$($name:ident),*] $($expr:tt)*) => {
        f! { [$($name),*] (_) $($expr)*  }
    };

    ($name:ident . $($toks:tt)*) => {
        f_! { [$name] $name . $($toks)* }
    };

    ( { $name:ident . $($toks:tt)* } ) => {
        f_! { [$name] { $name . $($toks)* } }
    };
}

/// Variant of the `f` macro producing a lambda which drops its first and second arguments.
#[macro_export]
macro_rules! f__ {
    ([$($name:ident),*] $($expr:tt)*) => {
        f! { [$($name),*] (_,_) $($expr)*  }
    };

    ($name:ident . $($toks:tt)*) => {
        f__! { [$name] $name . $($toks)* }
    };

    ( { $name:ident . $($toks:tt)* } ) => {
        f__! { [$name] { $name . $($toks)* } }
    };
}

/// A macro for use in situations where the code is unreachable.
///
/// This macro will panic in debug builds, but in release builds it expands to
/// the unsafe [`std::hint::unreachable_unchecked()`] function, which allows the
/// compiler to optimise more.
#[macro_export]
macro_rules! unreachable_panic {
    () => (
        unreachable_panic!("This code was marked as unreachable.")
    );
    ($msg:tt) => (
        if cfg!(debug_assertions) {
            panic!($msg)
        } else {
            use std::hint::unreachable_unchecked;
            #[allow(unsafe_code)]
            unsafe { unreachable_unchecked() }
        }
    )
}