1 // implements the unary operator "op &T"
2 // based on "op T" where T is expected to be `Copy`able
3 macro_rules! forward_ref_unop {
4 (impl $imp:ident, $method:ident for $t:ty) => {
5 forward_ref_unop!(impl $imp, $method for $t,
6 #[stable(feature = "rust1", since = "1.0.0")]);
8 (impl $imp:ident, $method:ident for $t:ty, #[$attr:meta]) => {
11 type Output = <$t as $imp>::Output;
14 fn $method(self) -> <$t as $imp>::Output {
21 // implements binary operators "&T op U", "T op &U", "&T op &U"
22 // based on "T op U" where T and U are expected to be `Copy`able
23 macro_rules! forward_ref_binop {
24 (impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
25 forward_ref_binop!(impl $imp, $method for $t, $u,
26 #[stable(feature = "rust1", since = "1.0.0")]);
28 (impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
30 impl<'a> $imp<$u> for &'a $t {
31 type Output = <$t as $imp<$u>>::Output;
34 fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
35 $imp::$method(*self, other)
40 impl $imp<&$u> for $t {
41 type Output = <$t as $imp<$u>>::Output;
44 fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
45 $imp::$method(self, *other)
50 impl $imp<&$u> for &$t {
51 type Output = <$t as $imp<$u>>::Output;
54 fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
55 $imp::$method(*self, *other)
61 // implements "T op= &U", based on "T op= U"
62 // where U is expected to be `Copy`able
63 macro_rules! forward_ref_op_assign {
64 (impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
65 forward_ref_op_assign!(impl $imp, $method for $t, $u,
66 #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]);
68 (impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
70 impl $imp<&$u> for $t {
72 fn $method(&mut self, other: &$u) {
73 $imp::$method(self, *other);
79 /// Create a zero-size type similar to a closure type, but named.
80 #[unstable(feature = "std_internals", issue = "0")]
81 macro_rules! impl_fn_for_zst {
84 struct $Name: ident impl$( <$( $lifetime : lifetime ),+> )? Fn =
85 |$( $arg: ident: $ArgTy: ty ),*| -> $ReturnTy: ty
92 impl $( <$( $lifetime ),+> )? Fn<($( $ArgTy, )*)> for $Name {
94 extern "rust-call" fn call(&self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
99 impl $( <$( $lifetime ),+> )? FnMut<($( $ArgTy, )*)> for $Name {
101 extern "rust-call" fn call_mut(
103 ($( $arg, )*): ($( $ArgTy, )*)
105 Fn::call(&*self, ($( $arg, )*))
109 impl $( <$( $lifetime ),+> )? FnOnce<($( $ArgTy, )*)> for $Name {
110 type Output = $ReturnTy;
113 extern "rust-call" fn call_once(self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
114 Fn::call(&self, ($( $arg, )*))
121 /// A macro for defining `#[cfg]` if-else statements.
123 /// The macro provided by this crate, `cfg_if`, is similar to the `if/elif` C
124 /// preprocessor macro by allowing definition of a cascade of `#[cfg]` cases,
125 /// emitting the implementation which matches first.
127 /// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code
128 /// without having to rewrite each clause multiple times.
134 /// extern crate cfg_if;
137 /// if #[cfg(unix)] {
138 /// fn foo() { /* unix specific functionality */ }
139 /// } else if #[cfg(target_pointer_width = "32")] {
140 /// fn foo() { /* non-unix, 32-bit functionality */ }
142 /// fn foo() { /* fallback implementation */ }
148 macro_rules! cfg_if {
149 // match if/else chains with a final `else`
151 if #[cfg($($meta:meta),*)] { $($it:item)* }
158 $( ( ($($meta),*) ($($it)*) ), )*
163 // match if/else chains lacking a final `else`
165 if #[cfg($($i_met:meta),*)] { $($i_it:item)* }
167 else if #[cfg($($e_met:meta),*)] { $($e_it:item)* }
173 ( ($($i_met),*) ($($i_it)*) ),
174 $( ( ($($e_met),*) ($($e_it)*) ), )*
179 // Internal and recursive macro to emit all the items
181 // Collects all the negated cfgs in a list at the beginning and after the
182 // semicolon is all the remaining items
183 (@__items ($($not:meta,)*) ; ) => {};
184 (@__items ($($not:meta,)*) ; ( ($($m:meta),*) ($($it:item)*) ), $($rest:tt)*) => {
185 // Emit all items within one block, applying an approprate #[cfg]. The
186 // #[cfg] will require all `$m` matchers specified and must also negate
187 // all previous matchers.
188 cfg_if! { @__apply cfg(all($($m,)* not(any($($not),*)))), $($it)* }
190 // Recurse to emit all other items in `$rest`, and when we do so add all
191 // our `$m` matchers to the list of `$not` matchers as future emissions
192 // will have to negate everything we just matched as well.
193 cfg_if! { @__items ($($not,)* $($m,)*) ; $($rest)* }
196 // Internal macro to Apply a cfg attribute to a list of items
197 (@__apply $m:meta, $($it:item)*) => {