bevy_reflect/impls/
std.rs

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