From 3cee95e59a68f1c80d80231976f4eb2444bbd29c Mon Sep 17 00:00:00 2001 From: Nathan Stocks Date: Tue, 1 Dec 2020 12:15:07 -0700 Subject: [PATCH] Rename reflect 'hash' method to 'reflect_hash' and `partial_eq` to `reflect_partial_eq` (#954) * Rename reflect 'hash' method to 'reflect_hash' to avoid colliding with std::hash::Hash::hash to resolve #943. * Rename partial_eq to reflect_partial_eq to avoid collisions with implementations of PartialEq on primitives. --- crates/bevy_reflect/README.md | 2 +- .../bevy_reflect_derive/src/lib.rs | 32 +++++++++---------- crates/bevy_reflect/src/impls/smallvec.rs | 4 +-- crates/bevy_reflect/src/impls/std.rs | 8 ++--- crates/bevy_reflect/src/lib.rs | 2 +- crates/bevy_reflect/src/list.rs | 6 ++-- crates/bevy_reflect/src/map.rs | 12 +++---- crates/bevy_reflect/src/reflect.rs | 4 +-- crates/bevy_reflect/src/struct_trait.rs | 6 ++-- crates/bevy_reflect/src/tuple_struct.rs | 6 ++-- examples/reflection/reflection.rs | 4 +-- 11 files changed, 43 insertions(+), 43 deletions(-) diff --git a/crates/bevy_reflect/README.md b/crates/bevy_reflect/README.md index b4d470f315..39c9048205 100644 --- a/crates/bevy_reflect/README.md +++ b/crates/bevy_reflect/README.md @@ -102,7 +102,7 @@ let reflect_deserializer = ReflectDeserializer::new(®istry); let value = reflect_deserializer.deserialize(&mut deserializer).unwrap(); let dynamic_struct = value.take::().unwrap(); -assert!(foo.partial_eq(&dynamic_struct).unwrap()); +assert!(foo.reflect_partial_eq(&dynamic_struct).unwrap()); ``` ### Trait "reflection" diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs index cf6655453f..22367a1d09 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs @@ -196,7 +196,7 @@ fn impl_struct( let hash_fn = reflect_attrs.get_hash_impl(&bevy_reflect_path); let serialize_fn = reflect_attrs.get_serialize_impl(&bevy_reflect_path); - let partial_eq_fn = match reflect_attrs.partial_eq { + let partial_eq_fn = match reflect_attrs.reflect_partial_eq { TraitImpl::NotImplemented => quote! { use #bevy_reflect_path::Struct; #bevy_reflect_path::struct_partial_eq(self, value) @@ -311,11 +311,11 @@ fn impl_struct( #serialize_fn } - fn hash(&self) -> Option { + fn reflect_hash(&self) -> Option { #hash_fn } - fn partial_eq(&self, value: &dyn #bevy_reflect_path::Reflect) -> Option { + fn reflect_partial_eq(&self, value: &dyn #bevy_reflect_path::Reflect) -> Option { #partial_eq_fn } } @@ -339,7 +339,7 @@ fn impl_tuple_struct( let hash_fn = reflect_attrs.get_hash_impl(&bevy_reflect_path); let serialize_fn = reflect_attrs.get_serialize_impl(&bevy_reflect_path); - let partial_eq_fn = match reflect_attrs.partial_eq { + let partial_eq_fn = match reflect_attrs.reflect_partial_eq { TraitImpl::NotImplemented => quote! { use #bevy_reflect_path::TupleStruct; #bevy_reflect_path::tuple_struct_partial_eq(self, value) @@ -430,11 +430,11 @@ fn impl_tuple_struct( #serialize_fn } - fn hash(&self) -> Option { + fn reflect_hash(&self) -> Option { #hash_fn } - fn partial_eq(&self, value: &dyn #bevy_reflect_path::Reflect) -> Option { + fn reflect_partial_eq(&self, value: &dyn #bevy_reflect_path::Reflect) -> Option { #partial_eq_fn } } @@ -501,11 +501,11 @@ fn impl_value( #bevy_reflect_path::ReflectMut::Value(self) } - fn hash(&self) -> Option { + fn reflect_hash(&self) -> Option { #hash_fn } - fn partial_eq(&self, value: &dyn #bevy_reflect_path::Reflect) -> Option { + fn reflect_partial_eq(&self, value: &dyn #bevy_reflect_path::Reflect) -> Option { #partial_eq_fn } @@ -585,8 +585,8 @@ pub fn impl_reflect_value(input: TokenStream) -> TokenStream { #[derive(Default)] struct ReflectAttrs { - hash: TraitImpl, - partial_eq: TraitImpl, + reflect_hash: TraitImpl, + reflect_partial_eq: TraitImpl, serialize: TraitImpl, data: Vec, } @@ -602,8 +602,8 @@ impl ReflectAttrs { if let Some(segment) = path.segments.iter().next() { let ident = segment.ident.to_string(); match ident.as_str() { - "PartialEq" => attrs.partial_eq = TraitImpl::Implemented, - "Hash" => attrs.hash = TraitImpl::Implemented, + "PartialEq" => attrs.reflect_partial_eq = TraitImpl::Implemented, + "Hash" => attrs.reflect_hash = TraitImpl::Implemented, "Serialize" => attrs.serialize = TraitImpl::Implemented, _ => attrs.data.push(Ident::new( &format!("Reflect{}", segment.ident), @@ -626,11 +626,11 @@ impl ReflectAttrs { if let Some(segment) = path.segments.iter().next() { match ident.as_str() { "PartialEq" => { - attrs.partial_eq = + attrs.reflect_partial_eq = TraitImpl::Custom(segment.ident.clone()) } "Hash" => { - attrs.hash = + attrs.reflect_hash = TraitImpl::Custom(segment.ident.clone()) } "Serialize" => { @@ -657,7 +657,7 @@ impl ReflectAttrs { } fn get_hash_impl(&self, path: &Path) -> proc_macro2::TokenStream { - match &self.hash { + match &self.reflect_hash { TraitImpl::Implemented => quote! { use std::hash::{Hash, Hasher}; let mut hasher = #path::ReflectHasher::default(); @@ -675,7 +675,7 @@ impl ReflectAttrs { } fn get_partial_eq_impl(&self) -> proc_macro2::TokenStream { - match &self.partial_eq { + match &self.reflect_partial_eq { TraitImpl::Implemented => quote! { let value = value.any(); if let Some(value) = value.downcast_ref::() { diff --git a/crates/bevy_reflect/src/impls/smallvec.rs b/crates/bevy_reflect/src/impls/smallvec.rs index 0aa938e2a3..c45d228e1f 100644 --- a/crates/bevy_reflect/src/impls/smallvec.rs +++ b/crates/bevy_reflect/src/impls/smallvec.rs @@ -82,11 +82,11 @@ where Box::new(self.clone_dynamic()) } - fn hash(&self) -> Option { + fn reflect_hash(&self) -> Option { None } - fn partial_eq(&self, value: &dyn Reflect) -> Option { + fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option { crate::list_partial_eq(self, value) } diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index c4a62b1196..1174b494de 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -93,11 +93,11 @@ impl Reflect for Vec { Box::new(self.clone_dynamic()) } - fn hash(&self) -> Option { + fn reflect_hash(&self) -> Option { None } - fn partial_eq(&self, value: &dyn Reflect) -> Option { + fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option { crate::list_partial_eq(self, value) } @@ -187,11 +187,11 @@ impl Reflect for HashMap Option { + fn reflect_hash(&self) -> Option { None } - fn partial_eq(&self, value: &dyn Reflect) -> Option { + fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option { map_partial_eq(self, value) } diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index f75ad00fd5..c46bdb1314 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -306,7 +306,7 @@ mod tests { let value = reflect_deserializer.deserialize(&mut deserializer).unwrap(); let dynamic_struct = value.take::().unwrap(); - assert!(foo.partial_eq(&dynamic_struct).unwrap()); + assert!(foo.reflect_partial_eq(&dynamic_struct).unwrap()); } #[test] diff --git a/crates/bevy_reflect/src/list.rs b/crates/bevy_reflect/src/list.rs index 936cd67044..aa3b0f0e98 100644 --- a/crates/bevy_reflect/src/list.rs +++ b/crates/bevy_reflect/src/list.rs @@ -111,11 +111,11 @@ impl Reflect for DynamicList { } #[inline] - fn hash(&self) -> Option { + fn reflect_hash(&self) -> Option { None } - fn partial_eq(&self, value: &dyn Reflect) -> Option { + fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option { list_partial_eq(self, value) } @@ -169,7 +169,7 @@ pub fn list_partial_eq(a: &L, b: &dyn Reflect) -> Option { } for (a_value, b_value) in a.iter().zip(list.iter()) { - if let Some(false) | None = a_value.partial_eq(b_value) { + if let Some(false) | None = a_value.reflect_partial_eq(b_value) { return Some(false); } } diff --git a/crates/bevy_reflect/src/map.rs b/crates/bevy_reflect/src/map.rs index 1931b13ae1..5ad323bcaf 100644 --- a/crates/bevy_reflect/src/map.rs +++ b/crates/bevy_reflect/src/map.rs @@ -33,7 +33,7 @@ impl DynamicMap { } pub fn insert_boxed(&mut self, key: Box, value: Box) { - match self.indices.entry(key.hash().expect(HASH_ERROR)) { + match self.indices.entry(key.reflect_hash().expect(HASH_ERROR)) { Entry::Occupied(entry) => { self.values[*entry.get()] = (key, value); } @@ -48,13 +48,13 @@ impl DynamicMap { impl Map for DynamicMap { fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> { self.indices - .get(&key.hash().expect(HASH_ERROR)) + .get(&key.reflect_hash().expect(HASH_ERROR)) .map(|index| &*self.values.get(*index).unwrap().1) } fn get_mut(&mut self, key: &dyn Reflect) -> Option<&mut dyn Reflect> { self.indices - .get(&key.hash().expect(HASH_ERROR)) + .get(&key.reflect_hash().expect(HASH_ERROR)) .cloned() .map(move |index| &mut *self.values.get_mut(index).unwrap().1) } @@ -130,11 +130,11 @@ impl Reflect for DynamicMap { Box::new(self.clone_dynamic()) } - fn hash(&self) -> Option { + fn reflect_hash(&self) -> Option { None } - fn partial_eq(&self, value: &dyn Reflect) -> Option { + fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option { map_partial_eq(self, value) } @@ -172,7 +172,7 @@ pub fn map_partial_eq(a: &M, b: &dyn Reflect) -> Option { for (key, value) in a.iter() { if let Some(map_value) = map.get(key) { - if let Some(false) | None = value.partial_eq(map_value) { + if let Some(false) | None = value.reflect_partial_eq(map_value) { return Some(false); } } else { diff --git a/crates/bevy_reflect/src/reflect.rs b/crates/bevy_reflect/src/reflect.rs index cb09703bb6..ee7a1caca2 100644 --- a/crates/bevy_reflect/src/reflect.rs +++ b/crates/bevy_reflect/src/reflect.rs @@ -30,9 +30,9 @@ pub trait Reflect: Any + Send + Sync { fn reflect_mut(&mut self) -> ReflectMut; fn clone_value(&self) -> Box; /// Returns a hash of the value (which includes the type) if hashing is supported. Otherwise `None` will be returned. - fn hash(&self) -> Option; + fn reflect_hash(&self) -> Option; /// Returns a "partial equal" comparison result if comparison is supported. Otherwise `None` will be returned. - fn partial_eq(&self, _value: &dyn Reflect) -> Option; + fn reflect_partial_eq(&self, _value: &dyn Reflect) -> Option; /// Returns a serializable value, if serialization is supported. Otherwise `None` will be returned. fn serializable(&self) -> Option; } diff --git a/crates/bevy_reflect/src/struct_trait.rs b/crates/bevy_reflect/src/struct_trait.rs index 9d38838dcc..e0ed0f18f7 100644 --- a/crates/bevy_reflect/src/struct_trait.rs +++ b/crates/bevy_reflect/src/struct_trait.rs @@ -216,11 +216,11 @@ impl Reflect for DynamicStruct { Ok(()) } - fn hash(&self) -> Option { + fn reflect_hash(&self) -> Option { None } - fn partial_eq(&self, value: &dyn Reflect) -> Option { + fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option { struct_partial_eq(self, value) } @@ -244,7 +244,7 @@ pub fn struct_partial_eq(a: &S, b: &dyn Reflect) -> Option { for (i, value) in struct_value.iter_fields().enumerate() { let name = struct_value.name_at(i).unwrap(); if let Some(field_value) = a.field(name) { - if let Some(false) | None = field_value.partial_eq(value) { + if let Some(false) | None = field_value.reflect_partial_eq(value) { return Some(false); } } else { diff --git a/crates/bevy_reflect/src/tuple_struct.rs b/crates/bevy_reflect/src/tuple_struct.rs index 4b2c338ab8..4ae2117953 100644 --- a/crates/bevy_reflect/src/tuple_struct.rs +++ b/crates/bevy_reflect/src/tuple_struct.rs @@ -171,11 +171,11 @@ impl Reflect for DynamicTupleStruct { Ok(()) } - fn hash(&self) -> Option { + fn reflect_hash(&self) -> Option { None } - fn partial_eq(&self, value: &dyn Reflect) -> Option { + fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option { tuple_struct_partial_eq(self, value) } @@ -198,7 +198,7 @@ pub fn tuple_struct_partial_eq(a: &S, b: &dyn Reflect) -> Option for (i, value) in tuple_struct.iter_fields().enumerate() { if let Some(field_value) = a.field(i) { - if let Some(false) | None = field_value.partial_eq(value) { + if let Some(false) | None = field_value.reflect_partial_eq(value) { return Some(false); } } else { diff --git a/examples/reflection/reflection.rs b/examples/reflection/reflection.rs index 98d0b1e36d..dd744ae41b 100644 --- a/examples/reflection/reflection.rs +++ b/examples/reflection/reflection.rs @@ -84,9 +84,9 @@ fn setup(type_registry: Res) { // as themselves. let _deserialized_struct = reflect_value.downcast_ref::(); - // Reflect has its own `partial_eq` implementation. This behaves like normal `partial_eq`, but it treats "dynamic" and + // Reflect has its own `partial_eq` implementation, named `reflect_partial_eq`. This behaves like normal `partial_eq`, but it treats "dynamic" and // "non-dynamic" types the same. The `Foo` struct and deserialized `DynamicStruct` are considered equal for this reason: - assert!(reflect_value.partial_eq(&value).unwrap()); + assert!(reflect_value.reflect_partial_eq(&value).unwrap()); // By "patching" `Foo` with the deserialized DynamicStruct, we can "Deserialize" Foo. // This means we can serialize and deserialize with a single `Reflect` derive!