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 160 161 162 163 164 165 166 167 168
//! Macros to simplify extending operator traits over references. //! //! Adapted from the Rust core library [internal_macros.rs] to remove standard //! library internal attribute annotations and add usage documentation. //! //! [internal_macros.rs]: https://github.com/rust-lang/rust/blob/master/library/core/src/internal_macros.rs /// Extend a unary operator trait impl over refs. /// /// Given an implementation of `op T` where T is `Copy`able, implements the unary /// operator `op &T`. /// /// # Examples /** ```rust use core::ops::Neg; use forward_ref::forward_ref_unop; #[derive(Clone, Copy, Debug, PartialEq)] struct MyInt(i32); impl Neg for MyInt { type Output = Self; #[inline] fn neg(self) -> Self::Output { Self(self.0.neg()) } } forward_ref_unop!(impl Neg, neg for MyInt); // Now negation will work for references. let a = MyInt(1); assert_eq!(-a, MyInt(-1)); assert_eq!(-&a, MyInt(-1)); ``` */ #[macro_export] macro_rules! forward_ref_unop { (impl $imp:ident, $method:ident for $t:ty) => { impl $imp for &$t { type Output = <$t as $imp>::Output; #[inline] fn $method(self) -> <$t as $imp>::Output { $imp::$method(*self) } } }; } /// Extend a binary operator trait impl over refs. /// /// Given an implementation of `T op U` where T and U are `Copy`able, implements /// the binary operators: /// - `&T op U` /// - `T op &U` /// - `&T op &U` /// /// # Examples /** ```rust use core::ops::Add; use forward_ref::forward_ref_binop; #[derive(Clone, Copy, Debug, PartialEq)] struct MyInt(i32); impl Add for MyInt { type Output = Self; #[inline] fn add(self, rhs: Self) -> Self::Output { Self(self.0 + rhs.0) } } forward_ref_binop!(impl Add, add for MyInt, MyInt); // Now addition will work for any combination of references and values. let a = MyInt(1); let b = MyInt(2); assert_eq!(a + b, MyInt(3)); assert_eq!(&a + b, MyInt(3)); assert_eq!(a + &b, MyInt(3)); assert_eq!(&a + &b, MyInt(3)); ``` */ #[macro_export] macro_rules! forward_ref_binop { (impl $imp:ident, $method:ident for $t:ty, $u:ty) => { impl<'a> $imp<$u> for &'a $t { type Output = <$t as $imp<$u>>::Output; #[inline] fn $method(self, other: $u) -> <$t as $imp<$u>>::Output { $imp::$method(*self, other) } } impl $imp<&$u> for $t { type Output = <$t as $imp<$u>>::Output; #[inline] fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output { $imp::$method(self, *other) } } impl $imp<&$u> for &$t { type Output = <$t as $imp<$u>>::Output; #[inline] fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output { $imp::$method(*self, *other) } } }; } /// Extend an assignment operator trait impl over refs. /// /// Given an implementation of `T op= U` where U is `Copy`able, implements /// the binary operator `T op= &U`. /// /// # Examples /** ```rust use core::ops::AddAssign; use forward_ref::forward_ref_op_assign; #[derive(Clone, Copy, Debug, PartialEq)] struct MyInt(i32); impl AddAssign for MyInt { #[inline] fn add_assign(&mut self, rhs: Self) { self.0 += rhs.0; } } forward_ref_op_assign!(impl AddAssign, add_assign for MyInt, MyInt); // Now addition assignment will also work for references. let mut a = MyInt(1); let b = MyInt(2); a += b; assert_eq!(a, MyInt(3)); a += &b; assert_eq!(a, MyInt(5)); ``` */ #[macro_export] macro_rules! forward_ref_op_assign { (impl $imp:ident, $method:ident for $t:ty, $u:ty) => { impl $imp<&$u> for $t { #[inline] fn $method(&mut self, other: &$u) { $imp::$method(self, *other); } } }; }