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 const $imp:ident, $method:ident for $t:ty) => {
5 forward_ref_unop!(impl const $imp, $method for $t,
6 #[stable(feature = "rust1", since = "1.0.0")]);
8 // Equivalent to the non-const version, with the addition of `rustc_const_unstable`
9 (impl const $imp:ident, $method:ident for $t:ty, #[$attr:meta]) => {
11 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
12 impl const $imp for &$t {
13 type Output = <$t as $imp>::Output;
16 fn $method(self) -> <$t as $imp>::Output {
21 (impl $imp:ident, $method:ident for $t:ty, #[$attr:meta]) => {
24 type Output = <$t as $imp>::Output;
27 fn $method(self) -> <$t as $imp>::Output {
34 // implements binary operators "&T op U", "T op &U", "&T op &U"
35 // based on "T op U" where T and U are expected to be `Copy`able
36 macro_rules! forward_ref_binop {
37 (impl const $imp:ident, $method:ident for $t:ty, $u:ty) => {
38 forward_ref_binop!(impl const $imp, $method for $t, $u,
39 #[stable(feature = "rust1", since = "1.0.0")]);
41 // Equivalent to the non-const version, with the addition of `rustc_const_unstable`
42 (impl const $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
44 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
45 impl<'a> const $imp<$u> for &'a $t {
46 type Output = <$t as $imp<$u>>::Output;
49 fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
50 $imp::$method(*self, other)
55 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
56 impl const $imp<&$u> for $t {
57 type Output = <$t as $imp<$u>>::Output;
60 fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
61 $imp::$method(self, *other)
66 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
67 impl const $imp<&$u> for &$t {
68 type Output = <$t as $imp<$u>>::Output;
71 fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
72 $imp::$method(*self, *other)
76 (impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
78 impl<'a> $imp<$u> for &'a $t {
79 type Output = <$t as $imp<$u>>::Output;
82 fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
83 $imp::$method(*self, other)
88 impl $imp<&$u> for $t {
89 type Output = <$t as $imp<$u>>::Output;
92 fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
93 $imp::$method(self, *other)
98 impl $imp<&$u> for &$t {
99 type Output = <$t as $imp<$u>>::Output;
102 fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
103 $imp::$method(*self, *other)
109 // implements "T op= &U", based on "T op= U"
110 // where U is expected to be `Copy`able
111 macro_rules! forward_ref_op_assign {
112 (impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
113 forward_ref_op_assign!(impl $imp, $method for $t, $u,
114 #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]);
116 (impl const $imp:ident, $method:ident for $t:ty, $u:ty) => {
117 forward_ref_op_assign!(impl const $imp, $method for $t, $u,
118 #[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]);
120 // Equivalent to the non-const version, with the addition of `rustc_const_unstable`
121 (impl const $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
123 #[rustc_const_unstable(feature = "const_ops", issue = "90080")]
124 impl const $imp<&$u> for $t {
126 fn $method(&mut self, other: &$u) {
127 $imp::$method(self, *other);
131 (impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
133 impl $imp<&$u> for $t {
135 fn $method(&mut self, other: &$u) {
136 $imp::$method(self, *other);
142 /// Create a zero-size type similar to a closure type, but named.
143 macro_rules! impl_fn_for_zst {
146 struct $Name: ident impl$( <$( $lifetime : lifetime ),+> )? Fn =
147 |$( $arg: ident: $ArgTy: ty ),*| -> $ReturnTy: ty
154 impl $( <$( $lifetime ),+> )? Fn<($( $ArgTy, )*)> for $Name {
156 extern "rust-call" fn call(&self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
161 impl $( <$( $lifetime ),+> )? FnMut<($( $ArgTy, )*)> for $Name {
163 extern "rust-call" fn call_mut(
165 ($( $arg, )*): ($( $ArgTy, )*)
167 Fn::call(&*self, ($( $arg, )*))
171 impl $( <$( $lifetime ),+> )? FnOnce<($( $ArgTy, )*)> for $Name {
172 type Output = $ReturnTy;
175 extern "rust-call" fn call_once(self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
176 Fn::call(&self, ($( $arg, )*))
183 /// A macro for defining `#[cfg]` if-else statements.
185 /// `cfg_if` is similar to the `if/elif` C preprocessor macro by allowing definition of a cascade
186 /// of `#[cfg]` cases, emitting the implementation which matches first.
188 /// This allows you to conveniently provide a long list `#[cfg]`'d blocks of code without having to
189 /// rewrite each clause multiple times.
193 /// ```ignore(cannot-test-this-because-non-exported-macro)
195 /// if #[cfg(unix)] {
196 /// fn foo() { /* unix specific functionality */ }
197 /// } else if #[cfg(target_pointer_width = "32")] {
198 /// fn foo() { /* non-unix, 32-bit functionality */ }
200 /// fn foo() { /* fallback implementation */ }
206 // This is a copy of `cfg_if!` from the `cfg_if` crate.
207 // The recursive invocations should use $crate if this is ever exported.
208 macro_rules! cfg_if {
209 // match if/else chains with a final `else`
212 if #[cfg( $i_meta:meta )] { $( $i_tokens:tt )* }
214 else { $( $e_tokens:tt )* }
219 (( $i_meta ) ( $( $i_tokens )* )) ,
221 (() ( $( $e_tokens )* )) ,
225 // Internal and recursive macro to emit all the items
227 // Collects all the previous cfgs in a list at the beginning, so they can be
228 // negated. After the semicolon is all the remaining items.
229 (@__items ( $( $_:meta , )* ) ; ) => {};
231 @__items ( $( $no:meta , )* ) ;
232 (( $( $yes:meta )? ) ( $( $tokens:tt )* )) ,
235 // Emit all items within one block, applying an appropriate #[cfg]. The
236 // #[cfg] will require all `$yes` matchers specified and must also negate
237 // all previous matchers.
240 not(any( $( $no ),* ))
242 cfg_if! { @__identity $( $tokens )* }
244 // Recurse to emit all other items in `$rest`, and when we do so add all
245 // our `$yes` matchers to the list of `$no` matchers as future emissions
246 // will have to negate everything we just matched as well.
248 @__items ( $( $no , )* $( $yes , )? ) ;
253 // Internal macro to make __apply work out right for different match types,
254 // because of how macros match/expand stuff.
255 (@__identity $( $tokens:tt )* ) => {