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
// Copyright 2014 The Algebra Developers. For a full listing of the authors,
// refer to the AUTHORS file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Implements empty traits aka marker traits for types provided.
/// # Examples
///
/// ```
/// # #[macro_use]
/// # extern crate alga;
/// # fn main() {}
/// trait Marker {}
/// struct Struct;
/// impl_marker!(Marker; u32; Struct);
/// ```
/// ```
/// # #[macro_use]
/// # extern crate alga;
/// # use std::fmt::Debug;
/// # fn main() {}
/// trait Marker<T: Debug> {}
/// struct Struct<T>(T);
/// impl_marker!(Marker<T>; Struct<T> where T: Debug);
/// ```
macro_rules! impl_marker(
// Finds the generic parameters of the type and implements the trait for it
(@para_rec
[$tra1t:ty, ($($clause:tt)+), ($($type_constr:tt)*)]
(< $($params:tt)*)
) => {
impl< $($params)* $tra1t for $($type_constr)*< $($params)*
where $($clause)+
{}
};
// Munches some token trees for searching generic parameters of the type
(@para_rec
[$tra1t:ty, ($($clause:tt)+), ($($prev:tt)*)]
($cur:tt $($rest:tt)*)
) => {
impl_marker!(@para_rec
[$tra1t, ($($clause)+), ($($prev)* $cur)]
($($rest)*)
);
};
// Handles the trailing separator after where clause
(@where_rec
[$tra1t:ty, ($($typ3:tt)+), ($($clause:tt)+)]
($(;)*)
) => {
impl_marker!(@para_rec
[$tra1t, ($($clause)+), ()]
($($typ3)+)
);
};
// Implements the trait for the generic type and continues searching other types
(@where_rec
[$tra1t:ty, ($($typ3:tt)+), ($($clause:tt)+)]
(; $($rest:tt)+)
) => {
impl_marker!(@para_rec
[$tra1t, ($($clause)+), ()]
($($typ3)+)
);
impl_marker!(@rec
[$tra1t, ()]
($($rest)+)
);
};
// Munches some token trees for searching the end of the where clause
(@where_rec
[$tra1t:ty, ($($typ3:tt)+), ($($prev:tt)*)]
($cur:tt $($rest:tt)*)
) => {
impl_marker!(@where_rec
[$tra1t, ($($typ3)+), ($($prev)* $cur)]
($($rest)*)
);
};
// Handles the trailing separator for non-generic type and implements the trait
(@rec
[$tra1t:ty, ($($typ3:tt)*)]
($(;)*)
) => {
impl $tra1t for $($typ3)* { }
};
// Implements the trait for the non-generic type and continues searching other types
(@rec
[$tra1t:ty, ($($typ3:tt)*)]
(; $($rest:tt)+)
) => {
impl $tra1t for $($typ3)* { }
impl_marker!(@rec
[$tra1t, ()]
($($rest)+)
);
};
// Detects that there is indeed a where clause for the type and tries to find where it ends.
(@rec
[$tra1t:ty, ($($prev:tt)+)]
(where $($rest:tt)+)
) => {
impl_marker!(@where_rec
[$tra1t, ($($prev)+), ()]
($($rest)+)
);
};
// Munches some token trees for detecting if we have where clause or not
(@rec
[$tra1t:ty, ($($prev:tt)*)]
($cur:tt $($rest:tt)*)
) => {
impl_marker!(@rec
[$tra1t, ($($prev)* $cur)]
($($rest)*)
);
};
// Entry point to the macro
($tra1t:ty; $($rest:tt)+) => {
impl_marker!(@rec
[$tra1t, ()]
($($rest)+)
);
};
);
macro_rules! impl_ident {
($M:ty; $V:expr; $($T:ty),* $(,)*) => {
$(impl Identity<$M> for $T { #[inline] fn identity() -> $T {$V} })+
}
}
macro_rules! impl_approx_eq {
($V:expr; $($T:ty),* $(,)*) => {
$(impl ApproxEq for $T {
type Eps = $T;
#[inline]
fn default_epsilon() -> Self::Eps { $V }
#[inline]
fn approx_eq_eps(&self, b: &$T, epsilon: &$T) -> bool {
if self < b {
*b - *self <= *epsilon
} else {
*self - *b <= *epsilon
}
}
})+
}
}