bevy_reflect/impls/
std.rs

1// Temporary workaround for impl_reflect!(Option/Result false-positive
2#![allow(unused_qualifications)]
3
4use crate::{
5    self as bevy_reflect, impl_type_path, map_apply, map_partial_eq, map_try_apply,
6    prelude::ReflectDefault,
7    reflect::impl_full_reflect,
8    set_apply, set_partial_eq, set_try_apply,
9    utility::{reflect_hasher, GenericTypeInfoCell, GenericTypePathCell, NonGenericTypeInfoCell},
10    ApplyError, Array, ArrayInfo, ArrayIter, DynamicMap, DynamicSet, DynamicTypePath, FromReflect,
11    FromType, Generics, GetTypeRegistration, List, ListInfo, ListIter, Map, MapInfo, MapIter,
12    MaybeTyped, OpaqueInfo, PartialReflect, Reflect, ReflectDeserialize, ReflectFromPtr,
13    ReflectFromReflect, ReflectKind, ReflectMut, ReflectOwned, ReflectRef, ReflectSerialize, Set,
14    SetInfo, TypeInfo, TypeParamInfo, TypePath, TypeRegistration, TypeRegistry, Typed,
15};
16use alloc::{borrow::Cow, collections::VecDeque};
17use bevy_reflect_derive::{impl_reflect, impl_reflect_opaque};
18use core::{
19    any::Any,
20    fmt,
21    hash::{BuildHasher, Hash, Hasher},
22};
23use std::path::Path;
24
25impl_reflect_opaque!(bool(
26    Debug,
27    Hash,
28    PartialEq,
29    Serialize,
30    Deserialize,
31    Default
32));
33impl_reflect_opaque!(char(
34    Debug,
35    Hash,
36    PartialEq,
37    Serialize,
38    Deserialize,
39    Default
40));
41impl_reflect_opaque!(u8(Debug, Hash, PartialEq, Serialize, Deserialize, Default));
42impl_reflect_opaque!(u16(Debug, Hash, PartialEq, Serialize, Deserialize, Default));
43impl_reflect_opaque!(u32(Debug, Hash, PartialEq, Serialize, Deserialize, Default));
44impl_reflect_opaque!(u64(Debug, Hash, PartialEq, Serialize, Deserialize, Default));
45impl_reflect_opaque!(u128(
46    Debug,
47    Hash,
48    PartialEq,
49    Serialize,
50    Deserialize,
51    Default
52));
53impl_reflect_opaque!(usize(
54    Debug,
55    Hash,
56    PartialEq,
57    Serialize,
58    Deserialize,
59    Default
60));
61impl_reflect_opaque!(i8(Debug, Hash, PartialEq, Serialize, Deserialize, Default));
62impl_reflect_opaque!(i16(Debug, Hash, PartialEq, Serialize, Deserialize, Default));
63impl_reflect_opaque!(i32(Debug, Hash, PartialEq, Serialize, Deserialize, Default));
64impl_reflect_opaque!(i64(Debug, Hash, PartialEq, Serialize, Deserialize, Default));
65impl_reflect_opaque!(i128(
66    Debug,
67    Hash,
68    PartialEq,
69    Serialize,
70    Deserialize,
71    Default
72));
73impl_reflect_opaque!(isize(
74    Debug,
75    Hash,
76    PartialEq,
77    Serialize,
78    Deserialize,
79    Default
80));
81impl_reflect_opaque!(f32(Debug, PartialEq, Serialize, Deserialize, Default));
82impl_reflect_opaque!(f64(Debug, PartialEq, Serialize, Deserialize, Default));
83impl_type_path!(str);
84impl_reflect_opaque!(::alloc::string::String(
85    Debug,
86    Hash,
87    PartialEq,
88    Serialize,
89    Deserialize,
90    Default
91));
92impl_reflect_opaque!(::std::path::PathBuf(
93    Debug,
94    Hash,
95    PartialEq,
96    Serialize,
97    Deserialize,
98    Default
99));
100impl_reflect_opaque!(::core::any::TypeId(Debug, Hash, PartialEq,));
101impl_reflect_opaque!(::alloc::collections::BTreeSet<T: Ord + Eq + Clone + Send + Sync>());
102impl_reflect_opaque!(::core::ops::Range<T: Clone + Send + Sync>());
103impl_reflect_opaque!(::core::ops::RangeInclusive<T: Clone + Send + Sync>());
104impl_reflect_opaque!(::core::ops::RangeFrom<T: Clone + Send + Sync>());
105impl_reflect_opaque!(::core::ops::RangeTo<T: Clone + Send + Sync>());
106impl_reflect_opaque!(::core::ops::RangeToInclusive<T: Clone + Send + Sync>());
107impl_reflect_opaque!(::core::ops::RangeFull());
108impl_reflect_opaque!(::core::ops::Bound<T: Clone + Send + Sync>());
109impl_reflect_opaque!(::bevy_utils::Duration(
110    Debug,
111    Hash,
112    PartialEq,
113    Serialize,
114    Deserialize,
115    Default
116));
117impl_reflect_opaque!(::bevy_utils::Instant(Debug, Hash, PartialEq));
118impl_reflect_opaque!(::core::num::NonZeroI128(
119    Debug,
120    Hash,
121    PartialEq,
122    Serialize,
123    Deserialize
124));
125impl_reflect_opaque!(::core::num::NonZeroU128(
126    Debug,
127    Hash,
128    PartialEq,
129    Serialize,
130    Deserialize
131));
132impl_reflect_opaque!(::core::num::NonZeroIsize(
133    Debug,
134    Hash,
135    PartialEq,
136    Serialize,
137    Deserialize
138));
139impl_reflect_opaque!(::core::num::NonZeroUsize(
140    Debug,
141    Hash,
142    PartialEq,
143    Serialize,
144    Deserialize
145));
146impl_reflect_opaque!(::core::num::NonZeroI64(
147    Debug,
148    Hash,
149    PartialEq,
150    Serialize,
151    Deserialize
152));
153impl_reflect_opaque!(::core::num::NonZeroU64(
154    Debug,
155    Hash,
156    PartialEq,
157    Serialize,
158    Deserialize
159));
160impl_reflect_opaque!(::core::num::NonZeroU32(
161    Debug,
162    Hash,
163    PartialEq,
164    Serialize,
165    Deserialize
166));
167impl_reflect_opaque!(::core::num::NonZeroI32(
168    Debug,
169    Hash,
170    PartialEq,
171    Serialize,
172    Deserialize
173));
174impl_reflect_opaque!(::core::num::NonZeroI16(
175    Debug,
176    Hash,
177    PartialEq,
178    Serialize,
179    Deserialize
180));
181impl_reflect_opaque!(::core::num::NonZeroU16(
182    Debug,
183    Hash,
184    PartialEq,
185    Serialize,
186    Deserialize
187));
188impl_reflect_opaque!(::core::num::NonZeroU8(
189    Debug,
190    Hash,
191    PartialEq,
192    Serialize,
193    Deserialize
194));
195impl_reflect_opaque!(::core::num::NonZeroI8(
196    Debug,
197    Hash,
198    PartialEq,
199    Serialize,
200    Deserialize
201));
202impl_reflect_opaque!(::core::num::Wrapping<T: Clone + Send + Sync>());
203impl_reflect_opaque!(::core::num::Saturating<T: Clone + Send + Sync>());
204impl_reflect_opaque!(::alloc::sync::Arc<T: Send + Sync + ?Sized>);
205
206// `Serialize` and `Deserialize` only for platforms supported by serde:
207// https://github.com/serde-rs/serde/blob/3ffb86fc70efd3d329519e2dddfa306cc04f167c/serde/src/de/impls.rs#L1732
208#[cfg(any(unix, windows))]
209impl_reflect_opaque!(::std::ffi::OsString(
210    Debug,
211    Hash,
212    PartialEq,
213    Serialize,
214    Deserialize
215));
216#[cfg(not(any(unix, windows)))]
217impl_reflect_opaque!(::std::ffi::OsString(Debug, Hash, PartialEq));
218impl_reflect_opaque!(::alloc::collections::BinaryHeap<T: Clone>);
219
220macro_rules! impl_reflect_for_atomic {
221    ($ty:ty, $ordering:expr) => {
222        impl_type_path!($ty);
223
224        const _: () = {
225            #[cfg(feature = "functions")]
226            crate::func::macros::impl_function_traits!($ty);
227
228            #[allow(unused_mut)]
229            impl GetTypeRegistration for $ty
230            where
231                $ty: Any + Send + Sync,
232            {
233                fn get_type_registration() -> TypeRegistration {
234                    let mut registration = TypeRegistration::of::<Self>();
235                    registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
236                    registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
237                    registration.insert::<ReflectDefault>(FromType::<Self>::from_type());
238                    registration.insert::<ReflectSerialize>(FromType::<Self>::from_type());
239                    registration.insert::<ReflectDeserialize>(FromType::<Self>::from_type());
240                    registration
241                }
242            }
243
244            impl Typed for $ty
245            where
246                $ty: Any + Send + Sync,
247            {
248                fn type_info() -> &'static TypeInfo {
249                    static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
250                    CELL.get_or_set(|| {
251                        let info = OpaqueInfo::new::<Self>();
252                        TypeInfo::Opaque(info)
253                    })
254                }
255            }
256
257            impl PartialReflect for $ty
258            where
259                $ty: Any + Send + Sync,
260            {
261                #[inline]
262                fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
263                    Some(<Self as Typed>::type_info())
264                }
265                #[inline]
266                fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
267                    self
268                }
269                #[inline]
270                fn as_partial_reflect(&self) -> &dyn PartialReflect {
271                    self
272                }
273                #[inline]
274                fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
275                    self
276                }
277                #[inline]
278                fn try_into_reflect(
279                    self: Box<Self>,
280                ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
281                    Ok(self)
282                }
283                #[inline]
284                fn try_as_reflect(&self) -> Option<&dyn Reflect> {
285                    Some(self)
286                }
287                #[inline]
288                fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
289                    Some(self)
290                }
291                #[inline]
292                fn clone_value(&self) -> Box<dyn PartialReflect> {
293                    Box::new(<$ty>::new(self.load($ordering)))
294                }
295                #[inline]
296                fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
297                    if let Some(value) = value.try_downcast_ref::<Self>() {
298                        *self = <$ty>::new(value.load($ordering));
299                    } else {
300                        return Err(ApplyError::MismatchedTypes {
301                            from_type: Into::into(DynamicTypePath::reflect_type_path(value)),
302                            to_type: Into::into(<Self as TypePath>::type_path()),
303                        });
304                    }
305                    Ok(())
306                }
307                #[inline]
308                fn reflect_kind(&self) -> ReflectKind {
309                    ReflectKind::Opaque
310                }
311                #[inline]
312                fn reflect_ref(&self) -> ReflectRef {
313                    ReflectRef::Opaque(self)
314                }
315                #[inline]
316                fn reflect_mut(&mut self) -> ReflectMut {
317                    ReflectMut::Opaque(self)
318                }
319                #[inline]
320                fn reflect_owned(self: Box<Self>) -> ReflectOwned {
321                    ReflectOwned::Opaque(self)
322                }
323                fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
324                    fmt::Debug::fmt(self, f)
325                }
326            }
327
328            impl FromReflect for $ty
329            where
330                $ty: Any + Send + Sync,
331            {
332                fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
333                    Some(<$ty>::new(
334                        reflect.try_downcast_ref::<$ty>()?.load($ordering),
335                    ))
336                }
337            }
338        };
339
340        impl_full_reflect!(for $ty where $ty: Any + Send + Sync);
341    };
342}
343
344impl_reflect_for_atomic!(
345    ::core::sync::atomic::AtomicIsize,
346    ::core::sync::atomic::Ordering::SeqCst
347);
348impl_reflect_for_atomic!(
349    ::core::sync::atomic::AtomicUsize,
350    ::core::sync::atomic::Ordering::SeqCst
351);
352impl_reflect_for_atomic!(
353    ::core::sync::atomic::AtomicI64,
354    ::core::sync::atomic::Ordering::SeqCst
355);
356impl_reflect_for_atomic!(
357    ::core::sync::atomic::AtomicU64,
358    ::core::sync::atomic::Ordering::SeqCst
359);
360impl_reflect_for_atomic!(
361    ::core::sync::atomic::AtomicI32,
362    ::core::sync::atomic::Ordering::SeqCst
363);
364impl_reflect_for_atomic!(
365    ::core::sync::atomic::AtomicU32,
366    ::core::sync::atomic::Ordering::SeqCst
367);
368impl_reflect_for_atomic!(
369    ::core::sync::atomic::AtomicI16,
370    ::core::sync::atomic::Ordering::SeqCst
371);
372impl_reflect_for_atomic!(
373    ::core::sync::atomic::AtomicU16,
374    ::core::sync::atomic::Ordering::SeqCst
375);
376impl_reflect_for_atomic!(
377    ::core::sync::atomic::AtomicI8,
378    ::core::sync::atomic::Ordering::SeqCst
379);
380impl_reflect_for_atomic!(
381    ::core::sync::atomic::AtomicU8,
382    ::core::sync::atomic::Ordering::SeqCst
383);
384impl_reflect_for_atomic!(
385    ::core::sync::atomic::AtomicBool,
386    ::core::sync::atomic::Ordering::SeqCst
387);
388
389macro_rules! impl_reflect_for_veclike {
390    ($ty:ty, $insert:expr, $remove:expr, $push:expr, $pop:expr, $sub:ty) => {
391        impl<T: FromReflect + MaybeTyped + TypePath + GetTypeRegistration> List for $ty {
392            #[inline]
393            fn get(&self, index: usize) -> Option<&dyn PartialReflect> {
394                <$sub>::get(self, index).map(|value| value as &dyn PartialReflect)
395            }
396
397            #[inline]
398            fn get_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect> {
399                <$sub>::get_mut(self, index).map(|value| value as &mut dyn PartialReflect)
400            }
401
402            fn insert(&mut self, index: usize, value: Box<dyn PartialReflect>) {
403                let value = value.try_take::<T>().unwrap_or_else(|value| {
404                    T::from_reflect(&*value).unwrap_or_else(|| {
405                        panic!(
406                            "Attempted to insert invalid value of type {}.",
407                            value.reflect_type_path()
408                        )
409                    })
410                });
411                $insert(self, index, value);
412            }
413
414            fn remove(&mut self, index: usize) -> Box<dyn PartialReflect> {
415                Box::new($remove(self, index))
416            }
417
418            fn push(&mut self, value: Box<dyn PartialReflect>) {
419                let value = T::take_from_reflect(value).unwrap_or_else(|value| {
420                    panic!(
421                        "Attempted to push invalid value of type {}.",
422                        value.reflect_type_path()
423                    )
424                });
425                $push(self, value);
426            }
427
428            fn pop(&mut self) -> Option<Box<dyn PartialReflect>> {
429                $pop(self).map(|value| Box::new(value) as Box<dyn PartialReflect>)
430            }
431
432            #[inline]
433            fn len(&self) -> usize {
434                <$sub>::len(self)
435            }
436
437            #[inline]
438            fn iter(&self) -> ListIter {
439                ListIter::new(self)
440            }
441
442            #[inline]
443            fn drain(&mut self) -> Vec<Box<dyn PartialReflect>> {
444                self.drain(..)
445                    .map(|value| Box::new(value) as Box<dyn PartialReflect>)
446                    .collect()
447            }
448        }
449
450        impl<T: FromReflect + MaybeTyped + TypePath + GetTypeRegistration> PartialReflect for $ty {
451            #[inline]
452            fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
453                Some(<Self as Typed>::type_info())
454            }
455
456            fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
457                self
458            }
459
460            #[inline]
461            fn as_partial_reflect(&self) -> &dyn PartialReflect {
462                self
463            }
464
465            #[inline]
466            fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
467                self
468            }
469
470            fn try_into_reflect(
471                self: Box<Self>,
472            ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
473                Ok(self)
474            }
475
476            fn try_as_reflect(&self) -> Option<&dyn Reflect> {
477                Some(self)
478            }
479
480            fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
481                Some(self)
482            }
483
484            fn reflect_kind(&self) -> ReflectKind {
485                ReflectKind::List
486            }
487
488            fn reflect_ref(&self) -> ReflectRef {
489                ReflectRef::List(self)
490            }
491
492            fn reflect_mut(&mut self) -> ReflectMut {
493                ReflectMut::List(self)
494            }
495
496            fn reflect_owned(self: Box<Self>) -> ReflectOwned {
497                ReflectOwned::List(self)
498            }
499
500            fn clone_value(&self) -> Box<dyn PartialReflect> {
501                Box::new(self.clone_dynamic())
502            }
503
504            fn reflect_hash(&self) -> Option<u64> {
505                crate::list_hash(self)
506            }
507
508            fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
509                crate::list_partial_eq(self, value)
510            }
511
512            fn apply(&mut self, value: &dyn PartialReflect) {
513                crate::list_apply(self, value);
514            }
515
516            fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
517                crate::list_try_apply(self, value)
518            }
519        }
520
521        impl_full_reflect!(<T> for $ty where T: FromReflect + MaybeTyped + TypePath + GetTypeRegistration);
522
523        impl<T: FromReflect + MaybeTyped + TypePath + GetTypeRegistration> Typed for $ty {
524            fn type_info() -> &'static TypeInfo {
525                static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
526                CELL.get_or_insert::<Self, _>(|| {
527                    TypeInfo::List(
528                        ListInfo::new::<Self, T>().with_generics(Generics::from_iter([
529                            TypeParamInfo::new::<T>("T")
530                        ]))
531                    )
532                })
533            }
534        }
535
536        impl<T: FromReflect + MaybeTyped + TypePath + GetTypeRegistration> GetTypeRegistration
537            for $ty
538        {
539            fn get_type_registration() -> TypeRegistration {
540                let mut registration = TypeRegistration::of::<$ty>();
541                registration.insert::<ReflectFromPtr>(FromType::<$ty>::from_type());
542                registration
543            }
544
545            fn register_type_dependencies(registry: &mut TypeRegistry) {
546                registry.register::<T>();
547            }
548        }
549
550        impl<T: FromReflect + MaybeTyped + TypePath + GetTypeRegistration> FromReflect for $ty {
551            fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
552                let ref_list = reflect.reflect_ref().as_list().ok()?;
553
554                let mut new_list = Self::with_capacity(ref_list.len());
555
556                for field in ref_list.iter() {
557                    $push(&mut new_list, T::from_reflect(field)?);
558                }
559
560                Some(new_list)
561            }
562        }
563    };
564}
565
566impl_reflect_for_veclike!(Vec<T>, Vec::insert, Vec::remove, Vec::push, Vec::pop, [T]);
567impl_type_path!(::alloc::vec::Vec<T>);
568#[cfg(feature = "functions")]
569crate::func::macros::impl_function_traits!(Vec<T>; <T: FromReflect + MaybeTyped + TypePath + GetTypeRegistration>);
570
571impl_reflect_for_veclike!(
572    VecDeque<T>,
573    VecDeque::insert,
574    VecDeque::remove,
575    VecDeque::push_back,
576    VecDeque::pop_back,
577    VecDeque::<T>
578);
579impl_type_path!(::alloc::collections::VecDeque<T>);
580#[cfg(feature = "functions")]
581crate::func::macros::impl_function_traits!(VecDeque<T>; <T: FromReflect + MaybeTyped + TypePath + GetTypeRegistration>);
582
583macro_rules! impl_reflect_for_hashmap {
584    ($ty:path) => {
585        impl<K, V, S> Map for $ty
586        where
587            K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,
588            V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
589            S: TypePath + BuildHasher + Send + Sync,
590        {
591            fn get(&self, key: &dyn PartialReflect) -> Option<&dyn PartialReflect> {
592                key.try_downcast_ref::<K>()
593                    .and_then(|key| Self::get(self, key))
594                    .map(|value| value as &dyn PartialReflect)
595            }
596
597            fn get_mut(&mut self, key: &dyn PartialReflect) -> Option<&mut dyn PartialReflect> {
598                key.try_downcast_ref::<K>()
599                    .and_then(move |key| Self::get_mut(self, key))
600                    .map(|value| value as &mut dyn PartialReflect)
601            }
602
603            fn get_at(&self, index: usize) -> Option<(&dyn PartialReflect, &dyn PartialReflect)> {
604                self.iter()
605                    .nth(index)
606                    .map(|(key, value)| (key as &dyn PartialReflect, value as &dyn PartialReflect))
607            }
608
609            fn get_at_mut(
610                &mut self,
611                index: usize,
612            ) -> Option<(&dyn PartialReflect, &mut dyn PartialReflect)> {
613                self.iter_mut().nth(index).map(|(key, value)| {
614                    (key as &dyn PartialReflect, value as &mut dyn PartialReflect)
615                })
616            }
617
618            fn len(&self) -> usize {
619                Self::len(self)
620            }
621
622            fn iter(&self) -> MapIter {
623                MapIter::new(self)
624            }
625
626            fn drain(&mut self) -> Vec<(Box<dyn PartialReflect>, Box<dyn PartialReflect>)> {
627                self.drain()
628                    .map(|(key, value)| {
629                        (
630                            Box::new(key) as Box<dyn PartialReflect>,
631                            Box::new(value) as Box<dyn PartialReflect>,
632                        )
633                    })
634                    .collect()
635            }
636
637            fn clone_dynamic(&self) -> DynamicMap {
638                let mut dynamic_map = DynamicMap::default();
639                dynamic_map.set_represented_type(self.get_represented_type_info());
640                for (k, v) in self {
641                    let key = K::from_reflect(k).unwrap_or_else(|| {
642                        panic!(
643                            "Attempted to clone invalid key of type {}.",
644                            k.reflect_type_path()
645                        )
646                    });
647                    dynamic_map.insert_boxed(Box::new(key), v.clone_value());
648                }
649                dynamic_map
650            }
651
652            fn insert_boxed(
653                &mut self,
654                key: Box<dyn PartialReflect>,
655                value: Box<dyn PartialReflect>,
656            ) -> Option<Box<dyn PartialReflect>> {
657                let key = K::take_from_reflect(key).unwrap_or_else(|key| {
658                    panic!(
659                        "Attempted to insert invalid key of type {}.",
660                        key.reflect_type_path()
661                    )
662                });
663                let value = V::take_from_reflect(value).unwrap_or_else(|value| {
664                    panic!(
665                        "Attempted to insert invalid value of type {}.",
666                        value.reflect_type_path()
667                    )
668                });
669                self.insert(key, value)
670                    .map(|old_value| Box::new(old_value) as Box<dyn PartialReflect>)
671            }
672
673            fn remove(&mut self, key: &dyn PartialReflect) -> Option<Box<dyn PartialReflect>> {
674                let mut from_reflect = None;
675                key.try_downcast_ref::<K>()
676                    .or_else(|| {
677                        from_reflect = K::from_reflect(key);
678                        from_reflect.as_ref()
679                    })
680                    .and_then(|key| self.remove(key))
681                    .map(|value| Box::new(value) as Box<dyn PartialReflect>)
682            }
683        }
684
685        impl<K, V, S> PartialReflect for $ty
686        where
687            K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,
688            V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
689            S: TypePath + BuildHasher + Send + Sync,
690        {
691            fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
692                Some(<Self as Typed>::type_info())
693            }
694
695            #[inline]
696            fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
697                self
698            }
699
700            fn as_partial_reflect(&self) -> &dyn PartialReflect {
701                self
702            }
703
704            fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
705                self
706            }
707
708            fn try_into_reflect(
709                self: Box<Self>,
710            ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
711                Ok(self)
712            }
713
714            fn try_as_reflect(&self) -> Option<&dyn Reflect> {
715                Some(self)
716            }
717
718            fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
719                Some(self)
720            }
721
722            fn reflect_kind(&self) -> ReflectKind {
723                ReflectKind::Map
724            }
725
726            fn reflect_ref(&self) -> ReflectRef {
727                ReflectRef::Map(self)
728            }
729
730            fn reflect_mut(&mut self) -> ReflectMut {
731                ReflectMut::Map(self)
732            }
733
734            fn reflect_owned(self: Box<Self>) -> ReflectOwned {
735                ReflectOwned::Map(self)
736            }
737
738            fn clone_value(&self) -> Box<dyn PartialReflect> {
739                Box::new(self.clone_dynamic())
740            }
741
742            fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
743                map_partial_eq(self, value)
744            }
745
746            fn apply(&mut self, value: &dyn PartialReflect) {
747                map_apply(self, value);
748            }
749
750            fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
751                map_try_apply(self, value)
752            }
753        }
754
755        impl_full_reflect!(
756            <K, V, S> for $ty
757            where
758                K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,
759                V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
760                S: TypePath + BuildHasher + Send + Sync,
761        );
762
763        impl<K, V, S> Typed for $ty
764        where
765            K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,
766            V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
767            S: TypePath + BuildHasher + Send + Sync,
768        {
769            fn type_info() -> &'static TypeInfo {
770                static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
771                CELL.get_or_insert::<Self, _>(|| {
772                    TypeInfo::Map(
773                        MapInfo::new::<Self, K, V>().with_generics(Generics::from_iter([
774                            TypeParamInfo::new::<K>("K"),
775                            TypeParamInfo::new::<V>("V"),
776                        ])),
777                    )
778                })
779            }
780        }
781
782        impl<K, V, S> GetTypeRegistration for $ty
783        where
784            K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,
785            V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
786            S: TypePath + BuildHasher + Send + Sync,
787        {
788            fn get_type_registration() -> TypeRegistration {
789                let mut registration = TypeRegistration::of::<Self>();
790                registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
791                registration
792            }
793
794            fn register_type_dependencies(registry: &mut TypeRegistry) {
795                registry.register::<K>();
796                registry.register::<V>();
797            }
798        }
799
800        impl<K, V, S> FromReflect for $ty
801        where
802            K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,
803            V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
804            S: TypePath + BuildHasher + Default + Send + Sync,
805        {
806            fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
807                let ref_map = reflect.reflect_ref().as_map().ok()?;
808
809                let mut new_map = Self::with_capacity_and_hasher(ref_map.len(), S::default());
810
811                for (key, value) in ref_map.iter() {
812                    let new_key = K::from_reflect(key)?;
813                    let new_value = V::from_reflect(value)?;
814                    new_map.insert(new_key, new_value);
815                }
816
817                Some(new_map)
818            }
819        }
820    };
821}
822
823impl_reflect_for_hashmap!(::std::collections::HashMap<K, V, S>);
824impl_type_path!(::std::collections::hash_map::RandomState);
825impl_type_path!(::std::collections::HashMap<K, V, S>);
826#[cfg(feature = "functions")]
827crate::func::macros::impl_function_traits!(::std::collections::HashMap<K, V, S>;
828    <
829        K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,
830        V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
831        S: TypePath + BuildHasher + Default + Send + Sync
832    >
833);
834
835impl_reflect_for_hashmap!(bevy_utils::hashbrown::HashMap<K, V, S>);
836impl_type_path!(::bevy_utils::hashbrown::hash_map::DefaultHashBuilder);
837impl_type_path!(::bevy_utils::hashbrown::HashMap<K, V, S>);
838#[cfg(feature = "functions")]
839crate::func::macros::impl_function_traits!(::bevy_utils::hashbrown::HashMap<K, V, S>;
840    <
841        K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,
842        V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
843        S: TypePath + BuildHasher + Default + Send + Sync
844    >
845);
846
847macro_rules! impl_reflect_for_hashset {
848    ($ty:path) => {
849        impl<V, S> Set for $ty
850        where
851            V: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
852            S: TypePath + BuildHasher + Send + Sync,
853        {
854            fn get(&self, value: &dyn PartialReflect) -> Option<&dyn PartialReflect> {
855                value
856                    .try_downcast_ref::<V>()
857                    .and_then(|value| Self::get(self, value))
858                    .map(|value| value as &dyn PartialReflect)
859            }
860
861            fn len(&self) -> usize {
862                Self::len(self)
863            }
864
865            fn iter(&self) -> Box<dyn Iterator<Item = &dyn PartialReflect> + '_> {
866                let iter = self.iter().map(|v| v as &dyn PartialReflect);
867                Box::new(iter)
868            }
869
870            fn drain(&mut self) -> Vec<Box<dyn PartialReflect>> {
871                self.drain()
872                    .map(|value| Box::new(value) as Box<dyn PartialReflect>)
873                    .collect()
874            }
875
876            fn clone_dynamic(&self) -> DynamicSet {
877                let mut dynamic_set = DynamicSet::default();
878                dynamic_set.set_represented_type(self.get_represented_type_info());
879                for v in self {
880                    dynamic_set.insert_boxed(v.clone_value());
881                }
882                dynamic_set
883            }
884
885            fn insert_boxed(&mut self, value: Box<dyn PartialReflect>) -> bool {
886                let value = V::take_from_reflect(value).unwrap_or_else(|value| {
887                    panic!(
888                        "Attempted to insert invalid value of type {}.",
889                        value.reflect_type_path()
890                    )
891                });
892                self.insert(value)
893            }
894
895            fn remove(&mut self, value: &dyn PartialReflect) -> bool {
896                let mut from_reflect = None;
897                value
898                    .try_downcast_ref::<V>()
899                    .or_else(|| {
900                        from_reflect = V::from_reflect(value);
901                        from_reflect.as_ref()
902                    })
903                    .map_or(false, |value| self.remove(value))
904            }
905
906            fn contains(&self, value: &dyn PartialReflect) -> bool {
907                let mut from_reflect = None;
908                value
909                    .try_downcast_ref::<V>()
910                    .or_else(|| {
911                        from_reflect = V::from_reflect(value);
912                        from_reflect.as_ref()
913                    })
914                    .map_or(false, |value| self.contains(value))
915            }
916        }
917
918        impl<V, S> PartialReflect for $ty
919        where
920            V: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
921            S: TypePath + BuildHasher + Send + Sync,
922        {
923            fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
924                Some(<Self as Typed>::type_info())
925            }
926
927            #[inline]
928            fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
929                self
930            }
931
932            fn as_partial_reflect(&self) -> &dyn PartialReflect {
933                self
934            }
935
936            fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
937                self
938            }
939
940            #[inline]
941            fn try_into_reflect(
942                self: Box<Self>,
943            ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
944                Ok(self)
945            }
946
947            fn try_as_reflect(&self) -> Option<&dyn Reflect> {
948                Some(self)
949            }
950
951            fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
952                Some(self)
953            }
954
955            fn apply(&mut self, value: &dyn PartialReflect) {
956                set_apply(self, value);
957            }
958
959            fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
960                set_try_apply(self, value)
961            }
962
963            fn reflect_kind(&self) -> ReflectKind {
964                ReflectKind::Set
965            }
966
967            fn reflect_ref(&self) -> ReflectRef {
968                ReflectRef::Set(self)
969            }
970
971            fn reflect_mut(&mut self) -> ReflectMut {
972                ReflectMut::Set(self)
973            }
974
975            fn reflect_owned(self: Box<Self>) -> ReflectOwned {
976                ReflectOwned::Set(self)
977            }
978
979            fn clone_value(&self) -> Box<dyn PartialReflect> {
980                Box::new(self.clone_dynamic())
981            }
982
983            fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
984                set_partial_eq(self, value)
985            }
986        }
987
988        impl<V, S> Typed for $ty
989        where
990            V: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
991            S: TypePath + BuildHasher + Send + Sync,
992        {
993            fn type_info() -> &'static TypeInfo {
994                static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
995                CELL.get_or_insert::<Self, _>(|| {
996                    TypeInfo::Set(
997                        SetInfo::new::<Self, V>().with_generics(Generics::from_iter([
998                            TypeParamInfo::new::<V>("V")
999                        ]))
1000                    )
1001                })
1002            }
1003        }
1004
1005        impl<V, S> GetTypeRegistration for $ty
1006        where
1007            V: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
1008            S: TypePath + BuildHasher + Send + Sync,
1009        {
1010            fn get_type_registration() -> TypeRegistration {
1011                let mut registration = TypeRegistration::of::<Self>();
1012                registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
1013                registration
1014            }
1015
1016            fn register_type_dependencies(registry: &mut TypeRegistry) {
1017                registry.register::<V>();
1018            }
1019        }
1020
1021        impl_full_reflect!(
1022            <V, S> for $ty
1023            where
1024                V: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
1025                S: TypePath + BuildHasher + Send + Sync,
1026        );
1027
1028        impl<V, S> FromReflect for $ty
1029        where
1030            V: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
1031            S: TypePath + BuildHasher + Default + Send + Sync,
1032        {
1033            fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
1034                let ref_set = reflect.reflect_ref().as_set().ok()?;
1035
1036                let mut new_set = Self::with_capacity_and_hasher(ref_set.len(), S::default());
1037
1038                for value in ref_set.iter() {
1039                    let new_value = V::from_reflect(value)?;
1040                    new_set.insert(new_value);
1041                }
1042
1043                Some(new_set)
1044            }
1045        }
1046    };
1047}
1048
1049impl_type_path!(::bevy_utils::NoOpHash);
1050impl_type_path!(::bevy_utils::FixedState);
1051
1052impl_reflect_for_hashset!(::std::collections::HashSet<V,S>);
1053impl_type_path!(::std::collections::HashSet<V, S>);
1054#[cfg(feature = "functions")]
1055crate::func::macros::impl_function_traits!(::std::collections::HashSet<V, S>;
1056    <
1057        V: Hash + Eq + FromReflect + TypePath + GetTypeRegistration,
1058        S: TypePath + BuildHasher + Default + Send + Sync
1059    >
1060);
1061
1062impl_reflect_for_hashset!(::bevy_utils::hashbrown::HashSet<V,S>);
1063impl_type_path!(::bevy_utils::hashbrown::HashSet<V, S>);
1064#[cfg(feature = "functions")]
1065crate::func::macros::impl_function_traits!(::bevy_utils::hashbrown::HashSet<V, S>;
1066    <
1067        V: Hash + Eq + FromReflect + TypePath + GetTypeRegistration,
1068        S: TypePath + BuildHasher + Default + Send + Sync
1069    >
1070);
1071
1072impl<K, V> Map for ::alloc::collections::BTreeMap<K, V>
1073where
1074    K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1075    V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1076{
1077    fn get(&self, key: &dyn PartialReflect) -> Option<&dyn PartialReflect> {
1078        key.try_downcast_ref::<K>()
1079            .and_then(|key| Self::get(self, key))
1080            .map(|value| value as &dyn PartialReflect)
1081    }
1082
1083    fn get_mut(&mut self, key: &dyn PartialReflect) -> Option<&mut dyn PartialReflect> {
1084        key.try_downcast_ref::<K>()
1085            .and_then(move |key| Self::get_mut(self, key))
1086            .map(|value| value as &mut dyn PartialReflect)
1087    }
1088
1089    fn get_at(&self, index: usize) -> Option<(&dyn PartialReflect, &dyn PartialReflect)> {
1090        self.iter()
1091            .nth(index)
1092            .map(|(key, value)| (key as &dyn PartialReflect, value as &dyn PartialReflect))
1093    }
1094
1095    fn get_at_mut(
1096        &mut self,
1097        index: usize,
1098    ) -> Option<(&dyn PartialReflect, &mut dyn PartialReflect)> {
1099        self.iter_mut()
1100            .nth(index)
1101            .map(|(key, value)| (key as &dyn PartialReflect, value as &mut dyn PartialReflect))
1102    }
1103
1104    fn len(&self) -> usize {
1105        Self::len(self)
1106    }
1107
1108    fn iter(&self) -> MapIter {
1109        MapIter::new(self)
1110    }
1111
1112    fn drain(&mut self) -> Vec<(Box<dyn PartialReflect>, Box<dyn PartialReflect>)> {
1113        // BTreeMap doesn't have a `drain` function. See
1114        // https://github.com/rust-lang/rust/issues/81074. So we have to fake one by popping
1115        // elements off one at a time.
1116        let mut result = Vec::with_capacity(self.len());
1117        while let Some((k, v)) = self.pop_first() {
1118            result.push((
1119                Box::new(k) as Box<dyn PartialReflect>,
1120                Box::new(v) as Box<dyn PartialReflect>,
1121            ));
1122        }
1123        result
1124    }
1125
1126    fn clone_dynamic(&self) -> DynamicMap {
1127        let mut dynamic_map = DynamicMap::default();
1128        dynamic_map.set_represented_type(self.get_represented_type_info());
1129        for (k, v) in self {
1130            let key = K::from_reflect(k).unwrap_or_else(|| {
1131                panic!(
1132                    "Attempted to clone invalid key of type {}.",
1133                    k.reflect_type_path()
1134                )
1135            });
1136            dynamic_map.insert_boxed(Box::new(key), v.clone_value());
1137        }
1138        dynamic_map
1139    }
1140
1141    fn insert_boxed(
1142        &mut self,
1143        key: Box<dyn PartialReflect>,
1144        value: Box<dyn PartialReflect>,
1145    ) -> Option<Box<dyn PartialReflect>> {
1146        let key = K::take_from_reflect(key).unwrap_or_else(|key| {
1147            panic!(
1148                "Attempted to insert invalid key of type {}.",
1149                key.reflect_type_path()
1150            )
1151        });
1152        let value = V::take_from_reflect(value).unwrap_or_else(|value| {
1153            panic!(
1154                "Attempted to insert invalid value of type {}.",
1155                value.reflect_type_path()
1156            )
1157        });
1158        self.insert(key, value)
1159            .map(|old_value| Box::new(old_value) as Box<dyn PartialReflect>)
1160    }
1161
1162    fn remove(&mut self, key: &dyn PartialReflect) -> Option<Box<dyn PartialReflect>> {
1163        let mut from_reflect = None;
1164        key.try_downcast_ref::<K>()
1165            .or_else(|| {
1166                from_reflect = K::from_reflect(key);
1167                from_reflect.as_ref()
1168            })
1169            .and_then(|key| self.remove(key))
1170            .map(|value| Box::new(value) as Box<dyn PartialReflect>)
1171    }
1172}
1173
1174impl<K, V> PartialReflect for ::alloc::collections::BTreeMap<K, V>
1175where
1176    K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1177    V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1178{
1179    fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
1180        Some(<Self as Typed>::type_info())
1181    }
1182    #[inline]
1183    fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
1184        self
1185    }
1186
1187    fn as_partial_reflect(&self) -> &dyn PartialReflect {
1188        self
1189    }
1190
1191    fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
1192        self
1193    }
1194    #[inline]
1195    fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
1196        Ok(self)
1197    }
1198
1199    fn try_as_reflect(&self) -> Option<&dyn Reflect> {
1200        Some(self)
1201    }
1202
1203    fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
1204        Some(self)
1205    }
1206    fn reflect_kind(&self) -> ReflectKind {
1207        ReflectKind::Map
1208    }
1209
1210    fn reflect_ref(&self) -> ReflectRef {
1211        ReflectRef::Map(self)
1212    }
1213
1214    fn reflect_mut(&mut self) -> ReflectMut {
1215        ReflectMut::Map(self)
1216    }
1217
1218    fn reflect_owned(self: Box<Self>) -> ReflectOwned {
1219        ReflectOwned::Map(self)
1220    }
1221
1222    fn clone_value(&self) -> Box<dyn PartialReflect> {
1223        Box::new(self.clone_dynamic())
1224    }
1225
1226    fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
1227        map_partial_eq(self, value)
1228    }
1229
1230    fn apply(&mut self, value: &dyn PartialReflect) {
1231        map_apply(self, value);
1232    }
1233
1234    fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
1235        map_try_apply(self, value)
1236    }
1237}
1238
1239impl_full_reflect!(
1240    <K, V> for ::alloc::collections::BTreeMap<K, V>
1241    where
1242        K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1243        V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1244);
1245
1246impl<K, V> Typed for ::alloc::collections::BTreeMap<K, V>
1247where
1248    K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1249    V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1250{
1251    fn type_info() -> &'static TypeInfo {
1252        static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
1253        CELL.get_or_insert::<Self, _>(|| {
1254            TypeInfo::Map(
1255                MapInfo::new::<Self, K, V>().with_generics(Generics::from_iter([
1256                    TypeParamInfo::new::<K>("K"),
1257                    TypeParamInfo::new::<V>("V"),
1258                ])),
1259            )
1260        })
1261    }
1262}
1263
1264impl<K, V> GetTypeRegistration for ::alloc::collections::BTreeMap<K, V>
1265where
1266    K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1267    V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1268{
1269    fn get_type_registration() -> TypeRegistration {
1270        let mut registration = TypeRegistration::of::<Self>();
1271        registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
1272        registration
1273    }
1274}
1275
1276impl<K, V> FromReflect for ::alloc::collections::BTreeMap<K, V>
1277where
1278    K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1279    V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1280{
1281    fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
1282        let ref_map = reflect.reflect_ref().as_map().ok()?;
1283
1284        let mut new_map = Self::new();
1285
1286        for (key, value) in ref_map.iter() {
1287            let new_key = K::from_reflect(key)?;
1288            let new_value = V::from_reflect(value)?;
1289            new_map.insert(new_key, new_value);
1290        }
1291
1292        Some(new_map)
1293    }
1294}
1295
1296impl_type_path!(::alloc::collections::BTreeMap<K, V>);
1297#[cfg(feature = "functions")]
1298crate::func::macros::impl_function_traits!(::alloc::collections::BTreeMap<K, V>;
1299    <
1300        K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1301        V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration
1302    >
1303);
1304
1305impl<T: Reflect + MaybeTyped + TypePath + GetTypeRegistration, const N: usize> Array for [T; N] {
1306    #[inline]
1307    fn get(&self, index: usize) -> Option<&dyn PartialReflect> {
1308        <[T]>::get(self, index).map(|value| value as &dyn PartialReflect)
1309    }
1310
1311    #[inline]
1312    fn get_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect> {
1313        <[T]>::get_mut(self, index).map(|value| value as &mut dyn PartialReflect)
1314    }
1315
1316    #[inline]
1317    fn len(&self) -> usize {
1318        N
1319    }
1320
1321    #[inline]
1322    fn iter(&self) -> ArrayIter {
1323        ArrayIter::new(self)
1324    }
1325
1326    #[inline]
1327    fn drain(self: Box<Self>) -> Vec<Box<dyn PartialReflect>> {
1328        self.into_iter()
1329            .map(|value| Box::new(value) as Box<dyn PartialReflect>)
1330            .collect()
1331    }
1332}
1333
1334impl<T: Reflect + MaybeTyped + TypePath + GetTypeRegistration, const N: usize> PartialReflect
1335    for [T; N]
1336{
1337    fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
1338        Some(<Self as Typed>::type_info())
1339    }
1340
1341    #[inline]
1342    fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
1343        self
1344    }
1345
1346    fn as_partial_reflect(&self) -> &dyn PartialReflect {
1347        self
1348    }
1349
1350    fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
1351        self
1352    }
1353
1354    fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
1355        Ok(self)
1356    }
1357
1358    fn try_as_reflect(&self) -> Option<&dyn Reflect> {
1359        Some(self)
1360    }
1361
1362    fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
1363        Some(self)
1364    }
1365
1366    #[inline]
1367    fn reflect_kind(&self) -> ReflectKind {
1368        ReflectKind::Array
1369    }
1370
1371    #[inline]
1372    fn reflect_ref(&self) -> ReflectRef {
1373        ReflectRef::Array(self)
1374    }
1375
1376    #[inline]
1377    fn reflect_mut(&mut self) -> ReflectMut {
1378        ReflectMut::Array(self)
1379    }
1380
1381    #[inline]
1382    fn reflect_owned(self: Box<Self>) -> ReflectOwned {
1383        ReflectOwned::Array(self)
1384    }
1385
1386    #[inline]
1387    fn clone_value(&self) -> Box<dyn PartialReflect> {
1388        Box::new(self.clone_dynamic())
1389    }
1390
1391    #[inline]
1392    fn reflect_hash(&self) -> Option<u64> {
1393        crate::array_hash(self)
1394    }
1395
1396    #[inline]
1397    fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
1398        crate::array_partial_eq(self, value)
1399    }
1400
1401    fn apply(&mut self, value: &dyn PartialReflect) {
1402        crate::array_apply(self, value);
1403    }
1404
1405    #[inline]
1406    fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
1407        crate::array_try_apply(self, value)
1408    }
1409}
1410
1411impl<T: Reflect + MaybeTyped + TypePath + GetTypeRegistration, const N: usize> Reflect for [T; N] {
1412    #[inline]
1413    fn into_any(self: Box<Self>) -> Box<dyn Any> {
1414        self
1415    }
1416
1417    #[inline]
1418    fn as_any(&self) -> &dyn Any {
1419        self
1420    }
1421
1422    #[inline]
1423    fn as_any_mut(&mut self) -> &mut dyn Any {
1424        self
1425    }
1426
1427    #[inline]
1428    fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
1429        self
1430    }
1431
1432    #[inline]
1433    fn as_reflect(&self) -> &dyn Reflect {
1434        self
1435    }
1436
1437    #[inline]
1438    fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
1439        self
1440    }
1441
1442    #[inline]
1443    fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
1444        *self = value.take()?;
1445        Ok(())
1446    }
1447}
1448
1449impl<T: FromReflect + MaybeTyped + TypePath + GetTypeRegistration, const N: usize> FromReflect
1450    for [T; N]
1451{
1452    fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
1453        let ref_array = reflect.reflect_ref().as_array().ok()?;
1454
1455        let mut temp_vec = Vec::with_capacity(ref_array.len());
1456
1457        for field in ref_array.iter() {
1458            temp_vec.push(T::from_reflect(field)?);
1459        }
1460
1461        temp_vec.try_into().ok()
1462    }
1463}
1464
1465impl<T: Reflect + MaybeTyped + TypePath + GetTypeRegistration, const N: usize> Typed for [T; N] {
1466    fn type_info() -> &'static TypeInfo {
1467        static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
1468        CELL.get_or_insert::<Self, _>(|| TypeInfo::Array(ArrayInfo::new::<Self, T>(N)))
1469    }
1470}
1471
1472impl<T: TypePath, const N: usize> TypePath for [T; N] {
1473    fn type_path() -> &'static str {
1474        static CELL: GenericTypePathCell = GenericTypePathCell::new();
1475        CELL.get_or_insert::<Self, _>(|| format!("[{t}; {N}]", t = T::type_path()))
1476    }
1477
1478    fn short_type_path() -> &'static str {
1479        static CELL: GenericTypePathCell = GenericTypePathCell::new();
1480        CELL.get_or_insert::<Self, _>(|| format!("[{t}; {N}]", t = T::short_type_path()))
1481    }
1482}
1483
1484impl<T: Reflect + MaybeTyped + TypePath + GetTypeRegistration, const N: usize> GetTypeRegistration
1485    for [T; N]
1486{
1487    fn get_type_registration() -> TypeRegistration {
1488        TypeRegistration::of::<[T; N]>()
1489    }
1490
1491    fn register_type_dependencies(registry: &mut TypeRegistry) {
1492        registry.register::<T>();
1493    }
1494}
1495
1496#[cfg(feature = "functions")]
1497crate::func::macros::impl_function_traits!([T; N]; <T: Reflect + MaybeTyped + TypePath + GetTypeRegistration> [const N: usize]);
1498
1499impl_reflect! {
1500    #[type_path = "core::option"]
1501    enum Option<T> {
1502        None,
1503        Some(T),
1504    }
1505}
1506
1507impl_reflect! {
1508    #[type_path = "core::result"]
1509    enum Result<T, E> {
1510        Ok(T),
1511        Err(E),
1512    }
1513}
1514
1515impl<T: TypePath + ?Sized> TypePath for &'static T {
1516    fn type_path() -> &'static str {
1517        static CELL: GenericTypePathCell = GenericTypePathCell::new();
1518        CELL.get_or_insert::<Self, _>(|| format!("&{}", T::type_path()))
1519    }
1520
1521    fn short_type_path() -> &'static str {
1522        static CELL: GenericTypePathCell = GenericTypePathCell::new();
1523        CELL.get_or_insert::<Self, _>(|| format!("&{}", T::short_type_path()))
1524    }
1525}
1526
1527impl<T: TypePath + ?Sized> TypePath for &'static mut T {
1528    fn type_path() -> &'static str {
1529        static CELL: GenericTypePathCell = GenericTypePathCell::new();
1530        CELL.get_or_insert::<Self, _>(|| format!("&mut {}", T::type_path()))
1531    }
1532
1533    fn short_type_path() -> &'static str {
1534        static CELL: GenericTypePathCell = GenericTypePathCell::new();
1535        CELL.get_or_insert::<Self, _>(|| format!("&mut {}", T::short_type_path()))
1536    }
1537}
1538
1539impl PartialReflect for Cow<'static, str> {
1540    fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
1541        Some(<Self as Typed>::type_info())
1542    }
1543
1544    #[inline]
1545    fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
1546        self
1547    }
1548
1549    fn as_partial_reflect(&self) -> &dyn PartialReflect {
1550        self
1551    }
1552
1553    fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
1554        self
1555    }
1556
1557    fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
1558        Ok(self)
1559    }
1560
1561    fn try_as_reflect(&self) -> Option<&dyn Reflect> {
1562        Some(self)
1563    }
1564
1565    fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
1566        Some(self)
1567    }
1568
1569    fn reflect_kind(&self) -> ReflectKind {
1570        ReflectKind::Opaque
1571    }
1572
1573    fn reflect_ref(&self) -> ReflectRef {
1574        ReflectRef::Opaque(self)
1575    }
1576
1577    fn reflect_mut(&mut self) -> ReflectMut {
1578        ReflectMut::Opaque(self)
1579    }
1580
1581    fn reflect_owned(self: Box<Self>) -> ReflectOwned {
1582        ReflectOwned::Opaque(self)
1583    }
1584
1585    fn clone_value(&self) -> Box<dyn PartialReflect> {
1586        Box::new(self.clone())
1587    }
1588
1589    fn reflect_hash(&self) -> Option<u64> {
1590        let mut hasher = reflect_hasher();
1591        Hash::hash(&Any::type_id(self), &mut hasher);
1592        Hash::hash(self, &mut hasher);
1593        Some(hasher.finish())
1594    }
1595
1596    fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
1597        if let Some(value) = value.try_downcast_ref::<Self>() {
1598            Some(PartialEq::eq(self, value))
1599        } else {
1600            Some(false)
1601        }
1602    }
1603
1604    fn debug(&self, f: &mut fmt::Formatter<'_>) -> core::fmt::Result {
1605        fmt::Debug::fmt(self, f)
1606    }
1607
1608    fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
1609        if let Some(value) = value.try_downcast_ref::<Self>() {
1610            self.clone_from(value);
1611        } else {
1612            return Err(ApplyError::MismatchedTypes {
1613                from_type: value.reflect_type_path().into(),
1614                // If we invoke the reflect_type_path on self directly the borrow checker complains that the lifetime of self must outlive 'static
1615                to_type: Self::type_path().into(),
1616            });
1617        }
1618        Ok(())
1619    }
1620}
1621
1622impl_full_reflect!(for Cow<'static, str>);
1623
1624impl Typed for Cow<'static, str> {
1625    fn type_info() -> &'static TypeInfo {
1626        static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
1627        CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
1628    }
1629}
1630
1631impl GetTypeRegistration for Cow<'static, str> {
1632    fn get_type_registration() -> TypeRegistration {
1633        let mut registration = TypeRegistration::of::<Cow<'static, str>>();
1634        registration.insert::<ReflectDeserialize>(FromType::<Cow<'static, str>>::from_type());
1635        registration.insert::<ReflectFromPtr>(FromType::<Cow<'static, str>>::from_type());
1636        registration.insert::<ReflectSerialize>(FromType::<Cow<'static, str>>::from_type());
1637        registration
1638    }
1639}
1640
1641impl FromReflect for Cow<'static, str> {
1642    fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
1643        Some(reflect.try_downcast_ref::<Cow<'static, str>>()?.clone())
1644    }
1645}
1646
1647#[cfg(feature = "functions")]
1648crate::func::macros::impl_function_traits!(Cow<'static, str>);
1649
1650impl<T: TypePath> TypePath for [T]
1651where
1652    [T]: ToOwned,
1653{
1654    fn type_path() -> &'static str {
1655        static CELL: GenericTypePathCell = GenericTypePathCell::new();
1656        CELL.get_or_insert::<Self, _>(|| format!("[{}]", <T>::type_path()))
1657    }
1658
1659    fn short_type_path() -> &'static str {
1660        static CELL: GenericTypePathCell = GenericTypePathCell::new();
1661        CELL.get_or_insert::<Self, _>(|| format!("[{}]", <T>::short_type_path()))
1662    }
1663}
1664
1665impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> List
1666    for Cow<'static, [T]>
1667{
1668    fn get(&self, index: usize) -> Option<&dyn PartialReflect> {
1669        self.as_ref().get(index).map(|x| x as &dyn PartialReflect)
1670    }
1671
1672    fn get_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect> {
1673        self.to_mut()
1674            .get_mut(index)
1675            .map(|x| x as &mut dyn PartialReflect)
1676    }
1677
1678    fn insert(&mut self, index: usize, element: Box<dyn PartialReflect>) {
1679        let value = T::take_from_reflect(element).unwrap_or_else(|value| {
1680            panic!(
1681                "Attempted to insert invalid value of type {}.",
1682                value.reflect_type_path()
1683            );
1684        });
1685        self.to_mut().insert(index, value);
1686    }
1687
1688    fn remove(&mut self, index: usize) -> Box<dyn PartialReflect> {
1689        Box::new(self.to_mut().remove(index))
1690    }
1691
1692    fn push(&mut self, value: Box<dyn PartialReflect>) {
1693        let value = T::take_from_reflect(value).unwrap_or_else(|value| {
1694            panic!(
1695                "Attempted to push invalid value of type {}.",
1696                value.reflect_type_path()
1697            )
1698        });
1699        self.to_mut().push(value);
1700    }
1701
1702    fn pop(&mut self) -> Option<Box<dyn PartialReflect>> {
1703        self.to_mut()
1704            .pop()
1705            .map(|value| Box::new(value) as Box<dyn PartialReflect>)
1706    }
1707
1708    fn len(&self) -> usize {
1709        self.as_ref().len()
1710    }
1711
1712    fn iter(&self) -> ListIter {
1713        ListIter::new(self)
1714    }
1715
1716    fn drain(&mut self) -> Vec<Box<dyn PartialReflect>> {
1717        self.to_mut()
1718            .drain(..)
1719            .map(|value| Box::new(value) as Box<dyn PartialReflect>)
1720            .collect()
1721    }
1722}
1723
1724impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> PartialReflect
1725    for Cow<'static, [T]>
1726{
1727    fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
1728        Some(<Self as Typed>::type_info())
1729    }
1730
1731    #[inline]
1732    fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
1733        self
1734    }
1735
1736    fn as_partial_reflect(&self) -> &dyn PartialReflect {
1737        self
1738    }
1739
1740    fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
1741        self
1742    }
1743
1744    fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
1745        Ok(self)
1746    }
1747
1748    fn try_as_reflect(&self) -> Option<&dyn Reflect> {
1749        Some(self)
1750    }
1751
1752    fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
1753        Some(self)
1754    }
1755
1756    fn reflect_kind(&self) -> ReflectKind {
1757        ReflectKind::List
1758    }
1759
1760    fn reflect_ref(&self) -> ReflectRef {
1761        ReflectRef::List(self)
1762    }
1763
1764    fn reflect_mut(&mut self) -> ReflectMut {
1765        ReflectMut::List(self)
1766    }
1767
1768    fn reflect_owned(self: Box<Self>) -> ReflectOwned {
1769        ReflectOwned::List(self)
1770    }
1771
1772    fn clone_value(&self) -> Box<dyn PartialReflect> {
1773        Box::new(List::clone_dynamic(self))
1774    }
1775
1776    fn reflect_hash(&self) -> Option<u64> {
1777        crate::list_hash(self)
1778    }
1779
1780    fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
1781        crate::list_partial_eq(self, value)
1782    }
1783
1784    fn apply(&mut self, value: &dyn PartialReflect) {
1785        crate::list_apply(self, value);
1786    }
1787
1788    fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
1789        crate::list_try_apply(self, value)
1790    }
1791}
1792
1793impl_full_reflect!(
1794    <T> for Cow<'static, [T]>
1795    where
1796        T: FromReflect + Clone + MaybeTyped + TypePath + GetTypeRegistration,
1797);
1798
1799impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> Typed
1800    for Cow<'static, [T]>
1801{
1802    fn type_info() -> &'static TypeInfo {
1803        static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
1804        CELL.get_or_insert::<Self, _>(|| TypeInfo::List(ListInfo::new::<Self, T>()))
1805    }
1806}
1807
1808impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> GetTypeRegistration
1809    for Cow<'static, [T]>
1810{
1811    fn get_type_registration() -> TypeRegistration {
1812        TypeRegistration::of::<Cow<'static, [T]>>()
1813    }
1814
1815    fn register_type_dependencies(registry: &mut TypeRegistry) {
1816        registry.register::<T>();
1817    }
1818}
1819
1820impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> FromReflect
1821    for Cow<'static, [T]>
1822{
1823    fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
1824        let ref_list = reflect.reflect_ref().as_list().ok()?;
1825
1826        let mut temp_vec = Vec::with_capacity(ref_list.len());
1827
1828        for field in ref_list.iter() {
1829            temp_vec.push(T::from_reflect(field)?);
1830        }
1831
1832        Some(temp_vec.into())
1833    }
1834}
1835
1836#[cfg(feature = "functions")]
1837crate::func::macros::impl_function_traits!(Cow<'static, [T]>; <T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration>);
1838
1839impl PartialReflect for &'static str {
1840    fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
1841        Some(<Self as Typed>::type_info())
1842    }
1843
1844    #[inline]
1845    fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
1846        self
1847    }
1848
1849    fn as_partial_reflect(&self) -> &dyn PartialReflect {
1850        self
1851    }
1852
1853    fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
1854        self
1855    }
1856
1857    fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
1858        Ok(self)
1859    }
1860
1861    fn try_as_reflect(&self) -> Option<&dyn Reflect> {
1862        Some(self)
1863    }
1864
1865    fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
1866        Some(self)
1867    }
1868
1869    fn reflect_ref(&self) -> ReflectRef {
1870        ReflectRef::Opaque(self)
1871    }
1872
1873    fn reflect_mut(&mut self) -> ReflectMut {
1874        ReflectMut::Opaque(self)
1875    }
1876
1877    fn reflect_owned(self: Box<Self>) -> ReflectOwned {
1878        ReflectOwned::Opaque(self)
1879    }
1880
1881    fn clone_value(&self) -> Box<dyn PartialReflect> {
1882        Box::new(*self)
1883    }
1884
1885    fn reflect_hash(&self) -> Option<u64> {
1886        let mut hasher = reflect_hasher();
1887        Hash::hash(&Any::type_id(self), &mut hasher);
1888        Hash::hash(self, &mut hasher);
1889        Some(hasher.finish())
1890    }
1891
1892    fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
1893        if let Some(value) = value.try_downcast_ref::<Self>() {
1894            Some(PartialEq::eq(self, value))
1895        } else {
1896            Some(false)
1897        }
1898    }
1899
1900    fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1901        fmt::Debug::fmt(&self, f)
1902    }
1903
1904    fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
1905        if let Some(value) = value.try_downcast_ref::<Self>() {
1906            self.clone_from(value);
1907        } else {
1908            return Err(ApplyError::MismatchedTypes {
1909                from_type: value.reflect_type_path().into(),
1910                to_type: Self::type_path().into(),
1911            });
1912        }
1913        Ok(())
1914    }
1915}
1916
1917impl Reflect for &'static str {
1918    fn into_any(self: Box<Self>) -> Box<dyn Any> {
1919        self
1920    }
1921
1922    fn as_any(&self) -> &dyn Any {
1923        self
1924    }
1925
1926    fn as_any_mut(&mut self) -> &mut dyn Any {
1927        self
1928    }
1929
1930    fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
1931        self
1932    }
1933
1934    fn as_reflect(&self) -> &dyn Reflect {
1935        self
1936    }
1937
1938    fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
1939        self
1940    }
1941
1942    fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
1943        *self = value.take()?;
1944        Ok(())
1945    }
1946}
1947
1948impl Typed for &'static str {
1949    fn type_info() -> &'static TypeInfo {
1950        static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
1951        CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
1952    }
1953}
1954
1955impl GetTypeRegistration for &'static str {
1956    fn get_type_registration() -> TypeRegistration {
1957        let mut registration = TypeRegistration::of::<Self>();
1958        registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
1959        registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
1960        registration
1961    }
1962}
1963
1964impl FromReflect for &'static str {
1965    fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
1966        reflect.try_downcast_ref::<Self>().copied()
1967    }
1968}
1969
1970#[cfg(feature = "functions")]
1971crate::func::macros::impl_function_traits!(&'static str);
1972
1973impl PartialReflect for &'static Path {
1974    fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
1975        Some(<Self as Typed>::type_info())
1976    }
1977
1978    #[inline]
1979    fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
1980        self
1981    }
1982
1983    fn as_partial_reflect(&self) -> &dyn PartialReflect {
1984        self
1985    }
1986
1987    fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
1988        self
1989    }
1990
1991    fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
1992        Ok(self)
1993    }
1994
1995    fn try_as_reflect(&self) -> Option<&dyn Reflect> {
1996        Some(self)
1997    }
1998
1999    fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
2000        Some(self)
2001    }
2002
2003    fn reflect_kind(&self) -> ReflectKind {
2004        ReflectKind::Opaque
2005    }
2006
2007    fn reflect_ref(&self) -> ReflectRef {
2008        ReflectRef::Opaque(self)
2009    }
2010
2011    fn reflect_mut(&mut self) -> ReflectMut {
2012        ReflectMut::Opaque(self)
2013    }
2014
2015    fn reflect_owned(self: Box<Self>) -> ReflectOwned {
2016        ReflectOwned::Opaque(self)
2017    }
2018
2019    fn clone_value(&self) -> Box<dyn PartialReflect> {
2020        Box::new(*self)
2021    }
2022
2023    fn reflect_hash(&self) -> Option<u64> {
2024        let mut hasher = reflect_hasher();
2025        Hash::hash(&Any::type_id(self), &mut hasher);
2026        Hash::hash(self, &mut hasher);
2027        Some(hasher.finish())
2028    }
2029
2030    fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
2031        if let Some(value) = value.try_downcast_ref::<Self>() {
2032            Some(PartialEq::eq(self, value))
2033        } else {
2034            Some(false)
2035        }
2036    }
2037
2038    fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
2039        if let Some(value) = value.try_downcast_ref::<Self>() {
2040            self.clone_from(value);
2041            Ok(())
2042        } else {
2043            Err(ApplyError::MismatchedTypes {
2044                from_type: value.reflect_type_path().into(),
2045                to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),
2046            })
2047        }
2048    }
2049}
2050
2051impl Reflect for &'static Path {
2052    fn into_any(self: Box<Self>) -> Box<dyn Any> {
2053        self
2054    }
2055
2056    fn as_any(&self) -> &dyn Any {
2057        self
2058    }
2059
2060    fn as_any_mut(&mut self) -> &mut dyn Any {
2061        self
2062    }
2063
2064    fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
2065        self
2066    }
2067
2068    fn as_reflect(&self) -> &dyn Reflect {
2069        self
2070    }
2071
2072    fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
2073        self
2074    }
2075
2076    fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
2077        *self = value.take()?;
2078        Ok(())
2079    }
2080}
2081
2082impl Typed for &'static Path {
2083    fn type_info() -> &'static TypeInfo {
2084        static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
2085        CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
2086    }
2087}
2088
2089impl GetTypeRegistration for &'static Path {
2090    fn get_type_registration() -> TypeRegistration {
2091        let mut registration = TypeRegistration::of::<Self>();
2092        registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
2093        registration
2094    }
2095}
2096
2097impl FromReflect for &'static Path {
2098    fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
2099        reflect.try_downcast_ref::<Self>().copied()
2100    }
2101}
2102
2103#[cfg(feature = "functions")]
2104crate::func::macros::impl_function_traits!(&'static Path);
2105
2106impl PartialReflect for Cow<'static, Path> {
2107    fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
2108        Some(<Self as Typed>::type_info())
2109    }
2110
2111    #[inline]
2112    fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
2113        self
2114    }
2115
2116    fn as_partial_reflect(&self) -> &dyn PartialReflect {
2117        self
2118    }
2119
2120    fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
2121        self
2122    }
2123
2124    fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
2125        Ok(self)
2126    }
2127
2128    fn try_as_reflect(&self) -> Option<&dyn Reflect> {
2129        Some(self)
2130    }
2131
2132    fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
2133        Some(self)
2134    }
2135
2136    fn reflect_kind(&self) -> ReflectKind {
2137        ReflectKind::Opaque
2138    }
2139
2140    fn reflect_ref(&self) -> ReflectRef {
2141        ReflectRef::Opaque(self)
2142    }
2143
2144    fn reflect_mut(&mut self) -> ReflectMut {
2145        ReflectMut::Opaque(self)
2146    }
2147
2148    fn reflect_owned(self: Box<Self>) -> ReflectOwned {
2149        ReflectOwned::Opaque(self)
2150    }
2151
2152    fn clone_value(&self) -> Box<dyn PartialReflect> {
2153        Box::new(self.clone())
2154    }
2155
2156    fn reflect_hash(&self) -> Option<u64> {
2157        let mut hasher = reflect_hasher();
2158        Hash::hash(&Any::type_id(self), &mut hasher);
2159        Hash::hash(self, &mut hasher);
2160        Some(hasher.finish())
2161    }
2162
2163    fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
2164        if let Some(value) = value.try_downcast_ref::<Self>() {
2165            Some(PartialEq::eq(self, value))
2166        } else {
2167            Some(false)
2168        }
2169    }
2170
2171    fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2172        fmt::Debug::fmt(&self, f)
2173    }
2174
2175    fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
2176        if let Some(value) = value.try_downcast_ref::<Self>() {
2177            self.clone_from(value);
2178            Ok(())
2179        } else {
2180            Err(ApplyError::MismatchedTypes {
2181                from_type: value.reflect_type_path().into(),
2182                to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),
2183            })
2184        }
2185    }
2186}
2187
2188impl Reflect for Cow<'static, Path> {
2189    fn into_any(self: Box<Self>) -> Box<dyn Any> {
2190        self
2191    }
2192
2193    fn as_any(&self) -> &dyn Any {
2194        self
2195    }
2196
2197    fn as_any_mut(&mut self) -> &mut dyn Any {
2198        self
2199    }
2200
2201    fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
2202        self
2203    }
2204
2205    fn as_reflect(&self) -> &dyn Reflect {
2206        self
2207    }
2208
2209    fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
2210        self
2211    }
2212
2213    fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
2214        *self = value.take()?;
2215        Ok(())
2216    }
2217}
2218
2219impl Typed for Cow<'static, Path> {
2220    fn type_info() -> &'static TypeInfo {
2221        static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
2222        CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
2223    }
2224}
2225
2226impl_type_path!(::std::path::Path);
2227impl_type_path!(::alloc::borrow::Cow<'a: 'static, T: ToOwned + ?Sized>);
2228
2229impl FromReflect for Cow<'static, Path> {
2230    fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
2231        Some(reflect.try_downcast_ref::<Self>()?.clone())
2232    }
2233}
2234
2235impl GetTypeRegistration for Cow<'static, Path> {
2236    fn get_type_registration() -> TypeRegistration {
2237        let mut registration = TypeRegistration::of::<Self>();
2238        registration.insert::<ReflectDeserialize>(FromType::<Self>::from_type());
2239        registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
2240        registration.insert::<ReflectSerialize>(FromType::<Self>::from_type());
2241        registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
2242        registration
2243    }
2244}
2245
2246#[cfg(feature = "functions")]
2247crate::func::macros::impl_function_traits!(Cow<'static, Path>);
2248
2249#[cfg(test)]
2250mod tests {
2251    use crate::{
2252        self as bevy_reflect, Enum, FromReflect, PartialReflect, Reflect, ReflectSerialize,
2253        TypeInfo, TypeRegistry, Typed, VariantInfo, VariantType,
2254    };
2255    use alloc::collections::BTreeMap;
2256    use bevy_utils::{Duration, HashMap, Instant};
2257    use core::f32::consts::{PI, TAU};
2258    use static_assertions::assert_impl_all;
2259    use std::path::Path;
2260
2261    #[test]
2262    fn can_serialize_duration() {
2263        let mut type_registry = TypeRegistry::default();
2264        type_registry.register::<Duration>();
2265
2266        let reflect_serialize = type_registry
2267            .get_type_data::<ReflectSerialize>(core::any::TypeId::of::<Duration>())
2268            .unwrap();
2269        let _serializable = reflect_serialize.get_serializable(&Duration::ZERO);
2270    }
2271
2272    #[test]
2273    fn should_partial_eq_char() {
2274        let a: &dyn PartialReflect = &'x';
2275        let b: &dyn PartialReflect = &'x';
2276        let c: &dyn PartialReflect = &'o';
2277        assert!(a.reflect_partial_eq(b).unwrap_or_default());
2278        assert!(!a.reflect_partial_eq(c).unwrap_or_default());
2279    }
2280
2281    #[test]
2282    fn should_partial_eq_i32() {
2283        let a: &dyn PartialReflect = &123_i32;
2284        let b: &dyn PartialReflect = &123_i32;
2285        let c: &dyn PartialReflect = &321_i32;
2286        assert!(a.reflect_partial_eq(b).unwrap_or_default());
2287        assert!(!a.reflect_partial_eq(c).unwrap_or_default());
2288    }
2289
2290    #[test]
2291    fn should_partial_eq_f32() {
2292        let a: &dyn PartialReflect = &PI;
2293        let b: &dyn PartialReflect = &PI;
2294        let c: &dyn PartialReflect = &TAU;
2295        assert!(a.reflect_partial_eq(b).unwrap_or_default());
2296        assert!(!a.reflect_partial_eq(c).unwrap_or_default());
2297    }
2298
2299    #[test]
2300    fn should_partial_eq_string() {
2301        let a: &dyn PartialReflect = &String::from("Hello");
2302        let b: &dyn PartialReflect = &String::from("Hello");
2303        let c: &dyn PartialReflect = &String::from("World");
2304        assert!(a.reflect_partial_eq(b).unwrap_or_default());
2305        assert!(!a.reflect_partial_eq(c).unwrap_or_default());
2306    }
2307
2308    #[test]
2309    fn should_partial_eq_vec() {
2310        let a: &dyn PartialReflect = &vec![1, 2, 3];
2311        let b: &dyn PartialReflect = &vec![1, 2, 3];
2312        let c: &dyn PartialReflect = &vec![3, 2, 1];
2313        assert!(a.reflect_partial_eq(b).unwrap_or_default());
2314        assert!(!a.reflect_partial_eq(c).unwrap_or_default());
2315    }
2316
2317    #[test]
2318    fn should_partial_eq_hash_map() {
2319        let mut a = HashMap::new();
2320        a.insert(0usize, 1.23_f64);
2321        let b = a.clone();
2322        let mut c = HashMap::new();
2323        c.insert(0usize, 3.21_f64);
2324
2325        let a: &dyn PartialReflect = &a;
2326        let b: &dyn PartialReflect = &b;
2327        let c: &dyn PartialReflect = &c;
2328        assert!(a.reflect_partial_eq(b).unwrap_or_default());
2329        assert!(!a.reflect_partial_eq(c).unwrap_or_default());
2330    }
2331
2332    #[test]
2333    fn should_partial_eq_btree_map() {
2334        let mut a = BTreeMap::new();
2335        a.insert(0usize, 1.23_f64);
2336        let b = a.clone();
2337        let mut c = BTreeMap::new();
2338        c.insert(0usize, 3.21_f64);
2339
2340        let a: &dyn Reflect = &a;
2341        let b: &dyn Reflect = &b;
2342        let c: &dyn Reflect = &c;
2343        assert!(a
2344            .reflect_partial_eq(b.as_partial_reflect())
2345            .unwrap_or_default());
2346        assert!(!a
2347            .reflect_partial_eq(c.as_partial_reflect())
2348            .unwrap_or_default());
2349    }
2350
2351    #[test]
2352    fn should_partial_eq_option() {
2353        let a: &dyn PartialReflect = &Some(123);
2354        let b: &dyn PartialReflect = &Some(123);
2355        assert_eq!(Some(true), a.reflect_partial_eq(b));
2356    }
2357
2358    #[test]
2359    fn option_should_impl_enum() {
2360        assert_impl_all!(Option<()>: Enum);
2361
2362        let mut value = Some(123usize);
2363
2364        assert!(value
2365            .reflect_partial_eq(&Some(123usize))
2366            .unwrap_or_default());
2367        assert!(!value
2368            .reflect_partial_eq(&Some(321usize))
2369            .unwrap_or_default());
2370
2371        assert_eq!("Some", value.variant_name());
2372        assert_eq!("core::option::Option<usize>::Some", value.variant_path());
2373
2374        if value.is_variant(VariantType::Tuple) {
2375            if let Some(field) = value
2376                .field_at_mut(0)
2377                .and_then(|field| field.try_downcast_mut::<usize>())
2378            {
2379                *field = 321;
2380            }
2381        } else {
2382            panic!("expected `VariantType::Tuple`");
2383        }
2384
2385        assert_eq!(Some(321), value);
2386    }
2387
2388    #[test]
2389    fn option_should_from_reflect() {
2390        #[derive(Reflect, PartialEq, Debug)]
2391        struct Foo(usize);
2392
2393        let expected = Some(Foo(123));
2394        let output = <Option<Foo> as FromReflect>::from_reflect(&expected).unwrap();
2395
2396        assert_eq!(expected, output);
2397    }
2398
2399    #[test]
2400    fn option_should_apply() {
2401        #[derive(Reflect, PartialEq, Debug)]
2402        struct Foo(usize);
2403
2404        // === None on None === //
2405        let patch = None::<Foo>;
2406        let mut value = None::<Foo>;
2407        PartialReflect::apply(&mut value, &patch);
2408
2409        assert_eq!(patch, value, "None apply onto None");
2410
2411        // === Some on None === //
2412        let patch = Some(Foo(123));
2413        let mut value = None::<Foo>;
2414        PartialReflect::apply(&mut value, &patch);
2415
2416        assert_eq!(patch, value, "Some apply onto None");
2417
2418        // === None on Some === //
2419        let patch = None::<Foo>;
2420        let mut value = Some(Foo(321));
2421        PartialReflect::apply(&mut value, &patch);
2422
2423        assert_eq!(patch, value, "None apply onto Some");
2424
2425        // === Some on Some === //
2426        let patch = Some(Foo(123));
2427        let mut value = Some(Foo(321));
2428        PartialReflect::apply(&mut value, &patch);
2429
2430        assert_eq!(patch, value, "Some apply onto Some");
2431    }
2432
2433    #[test]
2434    fn option_should_impl_typed() {
2435        assert_impl_all!(Option<()>: Typed);
2436
2437        type MyOption = Option<i32>;
2438        let info = MyOption::type_info();
2439        if let TypeInfo::Enum(info) = info {
2440            assert_eq!(
2441                "None",
2442                info.variant_at(0).unwrap().name(),
2443                "Expected `None` to be variant at index `0`"
2444            );
2445            assert_eq!(
2446                "Some",
2447                info.variant_at(1).unwrap().name(),
2448                "Expected `Some` to be variant at index `1`"
2449            );
2450            assert_eq!("Some", info.variant("Some").unwrap().name());
2451            if let VariantInfo::Tuple(variant) = info.variant("Some").unwrap() {
2452                assert!(
2453                    variant.field_at(0).unwrap().is::<i32>(),
2454                    "Expected `Some` variant to contain `i32`"
2455                );
2456                assert!(
2457                    variant.field_at(1).is_none(),
2458                    "Expected `Some` variant to only contain 1 field"
2459                );
2460            } else {
2461                panic!("Expected `VariantInfo::Tuple`");
2462            }
2463        } else {
2464            panic!("Expected `TypeInfo::Enum`");
2465        }
2466    }
2467
2468    #[test]
2469    fn nonzero_usize_impl_reflect_from_reflect() {
2470        let a: &dyn PartialReflect = &core::num::NonZero::<usize>::new(42).unwrap();
2471        let b: &dyn PartialReflect = &core::num::NonZero::<usize>::new(42).unwrap();
2472        assert!(a.reflect_partial_eq(b).unwrap_or_default());
2473        let forty_two: core::num::NonZero<usize> = FromReflect::from_reflect(a).unwrap();
2474        assert_eq!(forty_two, core::num::NonZero::<usize>::new(42).unwrap());
2475    }
2476
2477    #[test]
2478    fn instant_should_from_reflect() {
2479        let expected = Instant::now();
2480        let output = <Instant as FromReflect>::from_reflect(&expected).unwrap();
2481        assert_eq!(expected, output);
2482    }
2483
2484    #[test]
2485    fn path_should_from_reflect() {
2486        let path = Path::new("hello_world.rs");
2487        let output = <&'static Path as FromReflect>::from_reflect(&path).unwrap();
2488        assert_eq!(path, output);
2489    }
2490
2491    #[test]
2492    fn type_id_should_from_reflect() {
2493        let type_id = core::any::TypeId::of::<usize>();
2494        let output = <core::any::TypeId as FromReflect>::from_reflect(&type_id).unwrap();
2495        assert_eq!(type_id, output);
2496    }
2497
2498    #[test]
2499    fn static_str_should_from_reflect() {
2500        let expected = "Hello, World!";
2501        let output = <&'static str as FromReflect>::from_reflect(&expected).unwrap();
2502        assert_eq!(expected, output);
2503    }
2504}