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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/// Ghost box helper macro - trait variant. /// Place this outside your trait definition. /// /// # Example /// /// ``` /// # use ghost_actor::*; /// pub trait MyTrait { /// // trait fns here... /// /// ghost_box_trait_fns!(MyTrait); /// } /// ghost_box_trait!(MyTrait); /// /// #[derive(Clone, PartialEq, Eq, Hash)] /// pub struct MyConcrete; /// impl MyTrait for MyConcrete { /// ghost_box_trait_impl_fns!(MyTrait); /// } /// /// pub struct BoxMyTrait(pub Box<dyn MyTrait>); /// /// // BoxMyTrait will now implement `Clone + PartialEq + Eq + Hash` /// ghost_box_new_type!(BoxMyTrait); /// ``` #[macro_export] macro_rules! ghost_box_trait { ($trait:ident) => { impl ::std::clone::Clone for ::std::boxed::Box<dyn $trait> { fn clone(&self) -> Self { self.__box_clone() } } impl ::std::cmp::PartialEq for ::std::boxed::Box<dyn $trait> { fn eq(&self, o: &Self) -> bool { self.__box_eq(o) } } impl ::std::cmp::Eq for ::std::boxed::Box<dyn $trait> {} impl ::std::hash::Hash for ::std::boxed::Box<dyn $trait> { fn hash<Hasher: ::std::hash::Hasher>(&self, state: &mut Hasher) { self.__box_hash(state) } } }; } /// Ghost box helper macro - trait fn variant. /// Place this inside your trait definition. /// /// # Example /// /// ``` /// # use ghost_actor::*; /// pub trait MyTrait { /// // trait fns here... /// /// ghost_box_trait_fns!(MyTrait); /// } /// ghost_box_trait!(MyTrait); /// /// #[derive(Clone, PartialEq, Eq, Hash)] /// pub struct MyConcrete; /// impl MyTrait for MyConcrete { /// ghost_box_trait_impl_fns!(MyTrait); /// } /// /// pub struct BoxMyTrait(pub Box<dyn MyTrait>); /// /// // BoxMyTrait will now implement `Clone + PartialEq + Eq + Hash` /// ghost_box_new_type!(BoxMyTrait); /// ``` #[macro_export] macro_rules! ghost_box_trait_fns { ($trait:ident) => { /// Allows Clone from Box<dyn> trait objects. fn __box_clone(&self) -> ::std::boxed::Box<dyn $trait>; /// Allows PartialEq/Eq from Box<dyn> trait objects. fn __box_eq(&self, o: &dyn std::any::Any) -> bool; /// Allows Hash from Box<dyn> trait objects. fn __box_hash(&self, hasher: &mut dyn ::std::hash::Hasher); }; } /// Ghost box helper macro - trait impl fn variant. /// Place this inside your impl trait definition. /// /// # Example /// /// ``` /// # use ghost_actor::*; /// pub trait MyTrait { /// // trait fns here... /// /// ghost_box_trait_fns!(MyTrait); /// } /// ghost_box_trait!(MyTrait); /// /// #[derive(Clone, PartialEq, Eq, Hash)] /// pub struct MyConcrete; /// impl MyTrait for MyConcrete { /// ghost_box_trait_impl_fns!(MyTrait); /// } /// /// pub struct BoxMyTrait(pub Box<dyn MyTrait>); /// /// // BoxMyTrait will now implement `Clone + PartialEq + Eq + Hash` /// ghost_box_new_type!(BoxMyTrait); /// ``` #[macro_export] macro_rules! ghost_box_trait_impl_fns { ($trait:ident) => { #[inline] fn __box_clone(&self) -> Box<dyn $trait> { ::std::boxed::Box::new(::std::clone::Clone::clone(&*self)) } #[inline] fn __box_eq(&self, o: &dyn ::std::any::Any) -> bool { let c: &Self = match ::std::any::Any::downcast_ref(o) { None => return false, Some(c) => c, }; self == c } #[inline] fn __box_hash(&self, hasher: &mut dyn ::std::hash::Hasher) { ::std::hash::Hash::hash(self, &mut Box::new(hasher)) } }; } /// Ghost box helper macro - new type variant. /// Place this outside your new type definition. /// /// # Example /// /// ``` /// # use ghost_actor::*; /// pub trait MyTrait { /// // trait fns here... /// /// ghost_box_trait_fns!(MyTrait); /// } /// ghost_box_trait!(MyTrait); /// /// #[derive(Clone, PartialEq, Eq, Hash)] /// pub struct MyConcrete; /// impl MyTrait for MyConcrete { /// ghost_box_trait_impl_fns!(MyTrait); /// } /// /// pub struct BoxMyTrait(pub Box<dyn MyTrait>); /// /// // BoxMyTrait will now implement `Clone + PartialEq + Eq + Hash` /// ghost_box_new_type!(BoxMyTrait); /// ``` #[macro_export] macro_rules! ghost_box_new_type { ($newtype:ident) => { impl ::std::clone::Clone for $newtype { fn clone(&self) -> Self { Self(::std::clone::Clone::clone(&self.0)) } } impl ::std::cmp::PartialEq for $newtype { fn eq(&self, o: &Self) -> bool { ::std::cmp::PartialEq::eq(&self.0, &o.0) } } impl ::std::cmp::Eq for $newtype {} impl ::std::hash::Hash for $newtype { fn hash<Hasher: ::std::hash::Hasher>(&self, state: &mut Hasher) { ::std::hash::Hash::hash(&self.0, state) } } }; }