]> git.lizzy.rs Git - rust.git/blob - src/libcore/internal_macros.rs
Clean up E0207 explanation
[rust.git] / src / libcore / internal_macros.rs
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")]);
7     };
8     (impl $imp:ident, $method:ident for $t:ty, #[$attr:meta]) => {
9         #[$attr]
10         impl $imp for &$t {
11             type Output = <$t as $imp>::Output;
12
13             #[inline]
14             fn $method(self) -> <$t as $imp>::Output {
15                 $imp::$method(*self)
16             }
17         }
18     }
19 }
20
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")]);
27     };
28     (impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
29         #[$attr]
30         impl<'a> $imp<$u> for &'a $t {
31             type Output = <$t as $imp<$u>>::Output;
32
33             #[inline]
34             fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
35                 $imp::$method(*self, other)
36             }
37         }
38
39         #[$attr]
40         impl $imp<&$u> for $t {
41             type Output = <$t as $imp<$u>>::Output;
42
43             #[inline]
44             fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
45                 $imp::$method(self, *other)
46             }
47         }
48
49         #[$attr]
50         impl $imp<&$u> for &$t {
51             type Output = <$t as $imp<$u>>::Output;
52
53             #[inline]
54             fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
55                 $imp::$method(*self, *other)
56             }
57         }
58     }
59 }
60
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")]);
67     };
68     (impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
69         #[$attr]
70         impl $imp<&$u> for $t {
71             #[inline]
72             fn $method(&mut self, other: &$u) {
73                 $imp::$method(self, *other);
74             }
75         }
76     }
77 }
78
79 /// Create a zero-size type similar to a closure type, but named.
80 #[unstable(feature = "std_internals", issue = "none")]
81 macro_rules! impl_fn_for_zst {
82     ($(
83         $( #[$attr: meta] )*
84         struct $Name: ident impl$( <$( $lifetime : lifetime ),+> )? Fn =
85             |$( $arg: ident: $ArgTy: ty ),*| -> $ReturnTy: ty
86             $body: block;
87     )+) => {
88         $(
89             $( #[$attr] )*
90             struct $Name;
91
92             impl $( <$( $lifetime ),+> )? Fn<($( $ArgTy, )*)> for $Name {
93                 #[inline]
94                 extern "rust-call" fn call(&self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
95                     $body
96                 }
97             }
98
99             impl $( <$( $lifetime ),+> )? FnMut<($( $ArgTy, )*)> for $Name {
100                 #[inline]
101                 extern "rust-call" fn call_mut(
102                     &mut self,
103                     ($( $arg, )*): ($( $ArgTy, )*)
104                 ) -> $ReturnTy {
105                     Fn::call(&*self, ($( $arg, )*))
106                 }
107             }
108
109             impl $( <$( $lifetime ),+> )? FnOnce<($( $ArgTy, )*)> for $Name {
110                 type Output = $ReturnTy;
111
112                 #[inline]
113                 extern "rust-call" fn call_once(self, ($( $arg, )*): ($( $ArgTy, )*)) -> $ReturnTy {
114                     Fn::call(&self, ($( $arg, )*))
115                 }
116             }
117         )+
118     }
119 }