diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs index e34a1f84e91f4..ef23f22d499f5 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs @@ -665,12 +665,8 @@ fn impl_enum( Ident::new("unused", Span::call_site()) }; let wrapper_name = match &variant.fields { - Fields::Named(struct_fields) => { - quote!(#struct_fields).to_string() - } - Fields::Unnamed(tuple_fields) => { - quote!(#tuple_fields).to_string() - } + Fields::Named(struct_fields) => quote!(#struct_fields).to_string(), + Fields::Unnamed(tuple_fields) => quote!(#tuple_fields).to_string(), Fields::Unit => "unused".to_string(), }; let reflect_variant = { @@ -880,7 +876,7 @@ fn impl_enum( wrapper_ident, wrapper_name, variant_index, - variant_name, + _variant_name, _variant_ident, variant_and_fields_ident, fields, @@ -1030,7 +1026,7 @@ fn impl_enum( wrapper_ident, wrapper_name, variant_index, - variant_name, + _variant_name, _variant_ident, variant_and_fields_ident, fields, diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index 454f64c5dc718..a97df087b8593 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -1,6 +1,7 @@ use crate::{ - map_partial_eq, serde::Serializable, DynamicMap, List, ListIter, Map, MapIter, Reflect, - ReflectDeserialize, ReflectMut, ReflectRef, + map_partial_eq, serde::Serializable, DynamicMap, Enum, EnumVariant, EnumVariantMut, + GetTypeRegistration, List, ListIter, Map, MapIter, Reflect, ReflectDeserialize, ReflectMut, + ReflectRef, TypeRegistration, VariantInfo, VariantInfoIter, }; use bevy_reflect_derive::impl_reflect_value; @@ -29,7 +30,6 @@ impl_reflect_value!(isize(Hash, PartialEq, Serialize, Deserialize)); impl_reflect_value!(f32(Serialize, Deserialize)); impl_reflect_value!(f64(Serialize, Deserialize)); impl_reflect_value!(String(Hash, PartialEq, Serialize, Deserialize)); -impl_reflect_value!(Option Deserialize<'de> + Reflect + 'static>(Serialize, Deserialize)); impl_reflect_value!(HashSet Deserialize<'de> + Send + Sync + 'static>(Serialize, Deserialize)); impl_reflect_value!(Range Deserialize<'de> + Send + Sync + 'static>(Serialize, Deserialize)); @@ -279,4 +279,131 @@ impl Reflect for Cow<'static, str> { fn serializable(&self) -> Option { Some(Serializable::Borrowed(self)) } + + fn as_reflect(&self) -> &dyn Reflect { + self + } + + fn as_reflect_mut(&mut self) -> &mut dyn Reflect { + self + } +} + +impl GetTypeRegistration for Option { + fn get_type_registration() -> TypeRegistration { + TypeRegistration::of::>() + } +} +impl Enum for Option { + fn variant(&self) -> EnumVariant<'_> { + match self { + Option::Some(new_type) => EnumVariant::NewType(new_type as &dyn Reflect), + Option::None => EnumVariant::Unit, + } + } + + fn variant_mut(&mut self) -> EnumVariantMut<'_> { + match self { + Option::Some(new_type) => EnumVariantMut::NewType(new_type as &mut dyn Reflect), + Option::None => EnumVariantMut::Unit, + } + } + + fn variant_info(&self) -> VariantInfo<'_> { + let index = match self { + Option::Some(_) => 0usize, + Option::None => 1usize, + }; + VariantInfo { + index, + name: self.get_index_name(index).unwrap(), + } + } + + fn get_index_name(&self, index: usize) -> Option<&'_ str> { + match index { + 0usize => Some("Option::Some"), + 1usize => Some("Option::None"), + _ => None, + } + } + + fn get_index_from_name(&self, name: &str) -> Option { + match name { + "Option::Some" => Some(0usize), + "Option::None" => Some(1usize), + _ => None, + } + } + + fn iter_variants_info(&self) -> VariantInfoIter<'_> { + VariantInfoIter::new(self) + } +} +impl Reflect for Option { + #[inline] + fn type_name(&self) -> &str { + std::any::type_name::() + } + + #[inline] + fn any(&self) -> &dyn std::any::Any { + self + } + + #[inline] + fn any_mut(&mut self) -> &mut dyn std::any::Any { + self + } + + #[inline] + fn clone_value(&self) -> Box { + Box::new(self.clone()) + } + + #[inline] + fn set(&mut self, value: Box) -> Result<(), Box> { + *self = value.take()?; + Ok(()) + } + + #[inline] + fn apply(&mut self, value: &dyn Reflect) { + let value = value.any(); + if let Some(value) = value.downcast_ref::() { + *self = value.clone(); + } else { + { + panic!("Enum is not {}.", &std::any::type_name::()); + }; + } + } + + fn reflect_ref(&self) -> ReflectRef { + ReflectRef::Enum(self) + } + + fn reflect_mut(&mut self) -> ReflectMut { + ReflectMut::Enum(self) + } + + fn serializable(&self) -> Option { + None + } + + fn reflect_hash(&self) -> Option { + None + } + + fn reflect_partial_eq(&self, value: &dyn Reflect) -> Option { + crate::enum_partial_eq(self, value) + } + + fn as_reflect(&self) -> &dyn Reflect { + self + } + + fn as_reflect_mut(&mut self) -> &mut dyn Reflect { + self + } }