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 const $imp:ident, $method:ident for $t:ty) => {
9 forward_ref_unop!(impl const $imp, $method for $t,
10 #[stable(feature = "rust1", since = "1.0.0")]);
12 // Equivalent to the non-const version, with the addition of `rustc_const_unstable`
13 (impl const $imp:ident, $method:ident for $t:ty, #[$attr:meta]) => {
15 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
16 impl const $imp for &$t {
17 type Output = <$t as $imp>::Output;
20 fn $method(self) -> <$t as $imp>::Output {
25 (impl $imp:ident, $method:ident for $t:ty, #[$attr:meta]) => {
28 type Output = <$t as $imp>::Output;
31 fn $method(self) -> <$t as $imp>::Output {
38 // implements binary operators "&T op U", "T op &U", "&T op &U"
39 // based on "T op U" where T and U are expected to be `Copy`able
40 macro_rules! forward_ref_binop {
41 (impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
42 forward_ref_binop!(impl $imp, $method for $t, $u,
43 #[stable(feature = "rust1", since = "1.0.0")]);
45 (impl const $imp:ident, $method:ident for $t:ty, $u:ty) => {
46 forward_ref_binop!(impl const $imp, $method for $t, $u,
47 #[stable(feature = "rust1", since = "1.0.0")]);
49 // Equivalent to the non-const version, with the addition of `rustc_const_unstable`
50 (impl const $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
52 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
53 impl<'a> const $imp<$u> for &'a $t {
54 type Output = <$t as $imp<$u>>::Output;
57 fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
58 $imp::$method(*self, other)
63 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
64 impl const $imp<&$u> for $t {
65 type Output = <$t as $imp<$u>>::Output;
68 fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
69 $imp::$method(self, *other)
74 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
75 impl const $imp<&$u> for &$t {
76 type Output = <$t as $imp<$u>>::Output;
79 fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
80 $imp::$method(*self, *other)
84 (impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
86 impl<'a> $imp<$u> for &'a $t {
87 type Output = <$t as $imp<$u>>::Output;
90 fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
91 $imp::$method(*self, other)
96 impl $imp<&$u> for $t {
97 type Output = <$t as $imp<$u>>::Output;
100 fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
101 $imp::$method(self, *other)
106 impl $imp<&$u> for &$t {
107 type Output = <$t as $imp<$u>>::Output;
110 fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
111 $imp::$method(*self, *other)
117 // implements "T op= &U", based on "T op= U"
118 // where U is expected to be `Copy`able
119 macro_rules! forward_ref_op_assign {
120 (impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
121 forward_ref_op_assign!(impl $imp, $method for $t, $u,
122 #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]);
124 (impl const $imp:ident, $method:ident for $t:ty, $u:ty) => {
125 forward_ref_op_assign!(impl const $imp, $method for $t, $u,
126 #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]);
128 // Equivalent to the non-const version, with the addition of `rustc_const_unstable`
129 (impl const $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
131 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
132 impl const $imp<&$u> for $t {
134 fn $method(&mut self, other: &$u) {
135 $imp::$method(self, *other);
139 (impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
141 impl $imp<&$u> for $t {
143 fn $method(&mut self, other: &$u) {
144 $imp::$method(self, *other);
150 /// Create a zero-size type similar to a closure type, but named.
151 macro_rules! impl_fn_for_zst {
154 struct $Name: ident impl$( <$( $lifetime : lifetime ),+> )? Fn =
155 |$( $arg: ident: $ArgTy: ty ),*| -> $ReturnTy: ty
162 impl $( <$( $lifetime ),+> )? Fn<($( $ArgTy, )*)> for $Name {
164 extern "rust-call" fn call(&self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
169 impl $( <$( $lifetime ),+> )? FnMut<($( $ArgTy, )*)> for $Name {
171 extern "rust-call" fn call_mut(
173 ($( $arg, )*): ($( $ArgTy, )*)
175 Fn::call(&*self, ($( $arg, )*))
179 impl $( <$( $lifetime ),+> )? FnOnce<($( $ArgTy, )*)> for $Name {
180 type Output = $ReturnTy;
183 extern "rust-call" fn call_once(self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
184 Fn::call(&self, ($( $arg, )*))
191 /// A macro for defining `#[cfg]` if-else statements.
193 /// `cfg_if` is similar to the `if/elif` C preprocessor macro by allowing definition of a cascade
194 /// of `#[cfg]` cases, emitting the implementation which matches first.
196 /// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code without having to
197 /// rewrite each clause multiple times.
203 /// if #[cfg(unix)] {
204 /// fn foo() { /* unix specific functionality */ }
205 /// } else if #[cfg(target_pointer_width = "32")] {
206 /// fn foo() { /* non-unix, 32-bit functionality */ }
208 /// fn foo() { /* fallback implementation */ }
214 // This is a copy of `cfg_if!` from the `cfg_if` crate.
215 // The recursive invocations should use $crate if this is ever exported.
216 macro_rules! cfg_if {
217 // match if/else chains with a final `else`
220 if #[cfg( $i_meta:meta )] { $( $i_tokens:tt )* }
222 else { $( $e_tokens:tt )* }
227 (( $i_meta ) ( $( $i_tokens )* )) ,
229 (() ( $( $e_tokens )* )) ,
233 // match if/else chains lacking a final `else`
235 if #[cfg( $i_meta:meta )] { $( $i_tokens:tt )* }
237 else if #[cfg( $e_meta:meta )] { $( $e_tokens:tt )* }
242 (( $i_meta ) ( $( $i_tokens )* )) ,
244 (( $e_meta ) ( $( $e_tokens )* )) ,
249 // Internal and recursive macro to emit all the items
251 // Collects all the previous cfgs in a list at the beginning, so they can be
252 // negated. After the semicolon is all the remaining items.
253 (@__items ( $( $_:meta , )* ) ; ) => {};
255 @__items ( $( $no:meta , )* ) ;
256 (( $( $yes:meta )? ) ( $( $tokens:tt )* )) ,
259 // Emit all items within one block, applying an appropriate #[cfg]. The
260 // #[cfg] will require all `$yes` matchers specified and must also negate
261 // all previous matchers.
264 not(any( $( $no ),* ))
266 cfg_if! { @__identity $( $tokens )* }
268 // Recurse to emit all other items in `$rest`, and when we do so add all
269 // our `$yes` matchers to the list of `$no` matchers as future emissions
270 // will have to negate everything we just matched as well.
272 @__items ( $( $no , )* $( $yes , )? ) ;
277 // Internal macro to make __apply work out right for different match types,
278 // because of how macros match/expand stuff.
279 (@__identity $( $tokens:tt )* ) => {