1- use crate :: {
2- map_partial_eq, serde:: Serializable , DynamicMap , List , ListIter , Map , MapIter , Reflect ,
3- ReflectDeserialize , ReflectMut , ReflectRef ,
4- } ;
1+ use crate :: { DynamicMap , Enum , EnumVariant , EnumVariantMut , GetTypeRegistration , List , ListIter , Map , MapIter , Reflect , ReflectDeserialize , ReflectMut , ReflectRef , TypeRegistration , VariantInfo , VariantInfoIter , map_partial_eq, serde:: Serializable } ;
52
63use bevy_reflect_derive:: impl_reflect_value;
74use bevy_utils:: { HashMap , HashSet } ;
@@ -24,7 +21,6 @@ impl_reflect_value!(isize(Hash, PartialEq, Serialize, Deserialize));
2421impl_reflect_value ! ( f32 ( Serialize , Deserialize ) ) ;
2522impl_reflect_value ! ( f64 ( Serialize , Deserialize ) ) ;
2623impl_reflect_value ! ( String ( Hash , PartialEq , Serialize , Deserialize ) ) ;
27- impl_reflect_value ! ( Option <T : Serialize + Clone + for <' de> Deserialize <' de> + Reflect + ' static >( Serialize , Deserialize ) ) ;
2824impl_reflect_value ! ( HashSet <T : Serialize + Hash + Eq + Clone + for <' de> Deserialize <' de> + Send + Sync + ' static >( Serialize , Deserialize ) ) ;
2925impl_reflect_value ! ( Range <T : Serialize + Clone + for <' de> Deserialize <' de> + Send + Sync + ' static >( Serialize , Deserialize ) ) ;
3026
@@ -215,3 +211,115 @@ impl<K: Reflect + Clone + Eq + Hash, V: Reflect + Clone> Reflect for HashMap<K,
215211 self
216212 }
217213}
214+
215+ impl < T : Reflect + Clone + Send + Sync + ' static > GetTypeRegistration
216+ for Option < T >
217+ {
218+ fn get_type_registration ( ) -> TypeRegistration {
219+ let mut registration = TypeRegistration :: of :: < Option < T > > ( ) ;
220+ registration
221+ }
222+ }
223+ impl < T : Reflect + Clone + Send + Sync + ' static > Enum for Option < T > {
224+ fn variant ( & self ) -> EnumVariant < ' _ > {
225+ match self {
226+ Option :: Some ( new_type) => {
227+ EnumVariant :: NewType ( new_type as & dyn Reflect )
228+ }
229+ Option :: None => EnumVariant :: Unit ,
230+ }
231+ }
232+ fn variant_mut ( & mut self ) -> EnumVariantMut < ' _ > {
233+ match self {
234+ Option :: Some ( new_type) => EnumVariantMut :: NewType (
235+ new_type as & mut dyn Reflect ,
236+ ) ,
237+ Option :: None => EnumVariantMut :: Unit ,
238+ }
239+ }
240+ fn variant_info ( & self ) -> VariantInfo < ' _ > {
241+ let index = match self {
242+ Option :: Some ( _) => 0usize ,
243+ Option :: None => 1usize ,
244+ } ;
245+ VariantInfo {
246+ index,
247+ name : self . get_index_name ( index) . unwrap ( ) ,
248+ }
249+ }
250+ fn get_index_name ( & self , index : usize ) -> Option < & ' _ str > {
251+ match index {
252+ 0usize => Some ( "Option::Some" ) ,
253+ 1usize => Some ( "Option::None" ) ,
254+ _ => None ,
255+ }
256+ }
257+ fn get_index_from_name ( & self , name : & str ) -> Option < usize > {
258+ match name {
259+ "Option::Some" => Some ( 0usize ) ,
260+ "Option::None" => Some ( 1usize ) ,
261+ _ => None ,
262+ }
263+ }
264+ fn iter_variants_info ( & self ) -> VariantInfoIter < ' _ > {
265+ VariantInfoIter :: new ( self )
266+ }
267+ }
268+ impl < T : Reflect + Clone + Send + Sync + ' static > Reflect for Option < T > {
269+ #[ inline]
270+ fn type_name ( & self ) -> & str {
271+ std:: any:: type_name :: < Self > ( )
272+ }
273+ #[ inline]
274+ fn any ( & self ) -> & dyn std:: any:: Any {
275+ self
276+ }
277+ #[ inline]
278+ fn any_mut ( & mut self ) -> & mut dyn std:: any:: Any {
279+ self
280+ }
281+ #[ inline]
282+ fn clone_value ( & self ) -> Box < dyn Reflect > {
283+ Box :: new ( self . clone ( ) )
284+ }
285+ #[ inline]
286+ fn set (
287+ & mut self ,
288+ value : Box < dyn Reflect > ,
289+ ) -> Result < ( ) , Box < dyn Reflect > > {
290+ * self = value. take ( ) ?;
291+ Ok ( ( ) )
292+ }
293+ #[ inline]
294+ fn apply ( & mut self , value : & dyn Reflect ) {
295+ let value = value. any ( ) ;
296+ if let Some ( value) = value. downcast_ref :: < Self > ( ) {
297+ * self = value. clone ( ) ;
298+ } else {
299+ {
300+ panic ! ( "Enum is not {}." , & std:: any:: type_name:: <Self >( ) ) ;
301+ } ;
302+ }
303+ }
304+ fn reflect_ref ( & self ) -> ReflectRef {
305+ ReflectRef :: Enum ( self )
306+ }
307+ fn reflect_mut ( & mut self ) -> ReflectMut {
308+ ReflectMut :: Enum ( self )
309+ }
310+ fn serializable ( & self ) -> Option < Serializable > {
311+ None
312+ }
313+ fn reflect_hash ( & self ) -> Option < u64 > {
314+ None
315+ }
316+ fn reflect_partial_eq ( & self , value : & dyn Reflect ) -> Option < bool > {
317+ crate :: enum_partial_eq ( self , value)
318+ }
319+ fn as_reflect ( & self ) -> & dyn Reflect {
320+ self
321+ }
322+ fn as_reflect_mut ( & mut self ) -> & mut dyn Reflect {
323+ self
324+ }
325+ }
0 commit comments