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#[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 #[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
1004#[cfg(feature = "hashbrown")]
1005impl_reflect_for_hashmap!(hashbrown::hash_map::HashMap<K, V, S>);
1006#[cfg(feature = "hashbrown")]
1007impl_type_path!(::hashbrown::hash_map::HashMap<K, V, S>);
1008#[cfg(all(feature = "functions", feature = "hashbrown"))]
1009crate::func::macros::impl_function_traits!(::hashbrown::hash_map::HashMap<K, V, S>;
1010 <
1011 K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Hash,
1012 V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1013 S: TypePath + BuildHasher + Default + Send + Sync
1014 >
1015);
1016
1017macro_rules! impl_reflect_for_hashset {
1018 ($ty:path) => {
1019 impl<V, S> Set for $ty
1020 where
1021 V: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
1022 S: TypePath + BuildHasher + Default + Send + Sync,
1023 {
1024 fn get(&self, value: &dyn PartialReflect) -> Option<&dyn PartialReflect> {
1025 value
1026 .try_downcast_ref::<V>()
1027 .and_then(|value| Self::get(self, value))
1028 .map(|value| value as &dyn PartialReflect)
1029 }
1030
1031 fn len(&self) -> usize {
1032 Self::len(self)
1033 }
1034
1035 fn iter(&self) -> Box<dyn Iterator<Item = &dyn PartialReflect> + '_> {
1036 let iter = self.iter().map(|v| v as &dyn PartialReflect);
1037 Box::new(iter)
1038 }
1039
1040 fn drain(&mut self) -> Vec<Box<dyn PartialReflect>> {
1041 self.drain()
1042 .map(|value| Box::new(value) as Box<dyn PartialReflect>)
1043 .collect()
1044 }
1045
1046 fn insert_boxed(&mut self, value: Box<dyn PartialReflect>) -> bool {
1047 let value = V::take_from_reflect(value).unwrap_or_else(|value| {
1048 panic!(
1049 "Attempted to insert invalid value of type {}.",
1050 value.reflect_type_path()
1051 )
1052 });
1053 self.insert(value)
1054 }
1055
1056 fn remove(&mut self, value: &dyn PartialReflect) -> bool {
1057 let mut from_reflect = None;
1058 value
1059 .try_downcast_ref::<V>()
1060 .or_else(|| {
1061 from_reflect = V::from_reflect(value);
1062 from_reflect.as_ref()
1063 })
1064 .is_some_and(|value| self.remove(value))
1065 }
1066
1067 fn contains(&self, value: &dyn PartialReflect) -> bool {
1068 let mut from_reflect = None;
1069 value
1070 .try_downcast_ref::<V>()
1071 .or_else(|| {
1072 from_reflect = V::from_reflect(value);
1073 from_reflect.as_ref()
1074 })
1075 .is_some_and(|value| self.contains(value))
1076 }
1077 }
1078
1079 impl<V, S> PartialReflect for $ty
1080 where
1081 V: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
1082 S: TypePath + BuildHasher + Default + Send + Sync,
1083 {
1084 fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
1085 Some(<Self as Typed>::type_info())
1086 }
1087
1088 #[inline]
1089 fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
1090 self
1091 }
1092
1093 fn as_partial_reflect(&self) -> &dyn PartialReflect {
1094 self
1095 }
1096
1097 fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
1098 self
1099 }
1100
1101 #[inline]
1102 fn try_into_reflect(
1103 self: Box<Self>,
1104 ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
1105 Ok(self)
1106 }
1107
1108 fn try_as_reflect(&self) -> Option<&dyn Reflect> {
1109 Some(self)
1110 }
1111
1112 fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
1113 Some(self)
1114 }
1115
1116 fn apply(&mut self, value: &dyn PartialReflect) {
1117 set_apply(self, value);
1118 }
1119
1120 fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
1121 set_try_apply(self, value)
1122 }
1123
1124 fn reflect_kind(&self) -> ReflectKind {
1125 ReflectKind::Set
1126 }
1127
1128 fn reflect_ref(&self) -> ReflectRef {
1129 ReflectRef::Set(self)
1130 }
1131
1132 fn reflect_mut(&mut self) -> ReflectMut {
1133 ReflectMut::Set(self)
1134 }
1135
1136 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
1137 ReflectOwned::Set(self)
1138 }
1139
1140 fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
1141 let mut set = Self::with_capacity_and_hasher(self.len(), S::default());
1142 for value in self.iter() {
1143 let value = value.reflect_clone()?.take().map_err(|_| {
1144 ReflectCloneError::FailedDowncast {
1145 expected: Cow::Borrowed(<V as TypePath>::type_path()),
1146 received: Cow::Owned(value.reflect_type_path().to_string()),
1147 }
1148 })?;
1149 set.insert(value);
1150 }
1151
1152 Ok(Box::new(set))
1153 }
1154
1155 fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
1156 set_partial_eq(self, value)
1157 }
1158 }
1159
1160 impl<V, S> Typed for $ty
1161 where
1162 V: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
1163 S: TypePath + BuildHasher + Default + Send + Sync,
1164 {
1165 fn type_info() -> &'static TypeInfo {
1166 static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
1167 CELL.get_or_insert::<Self, _>(|| {
1168 TypeInfo::Set(
1169 SetInfo::new::<Self, V>().with_generics(Generics::from_iter([
1170 TypeParamInfo::new::<V>("V")
1171 ]))
1172 )
1173 })
1174 }
1175 }
1176
1177 impl<V, S> GetTypeRegistration for $ty
1178 where
1179 V: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
1180 S: TypePath + BuildHasher + Default + Send + Sync + Default,
1181 {
1182 fn get_type_registration() -> TypeRegistration {
1183 let mut registration = TypeRegistration::of::<Self>();
1184 registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
1185 registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
1186 registration
1187 }
1188
1189 fn register_type_dependencies(registry: &mut TypeRegistry) {
1190 registry.register::<V>();
1191 }
1192 }
1193
1194 impl_full_reflect!(
1195 <V, S> for $ty
1196 where
1197 V: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
1198 S: TypePath + BuildHasher + Default + Send + Sync,
1199 );
1200
1201 impl<V, S> FromReflect for $ty
1202 where
1203 V: FromReflect + TypePath + GetTypeRegistration + Eq + Hash,
1204 S: TypePath + BuildHasher + Default + Send + Sync,
1205 {
1206 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
1207 let ref_set = reflect.reflect_ref().as_set().ok()?;
1208
1209 let mut new_set = Self::with_capacity_and_hasher(ref_set.len(), S::default());
1210
1211 for value in ref_set.iter() {
1212 let new_value = V::from_reflect(value)?;
1213 new_set.insert(new_value);
1214 }
1215
1216 Some(new_set)
1217 }
1218 }
1219 };
1220}
1221
1222impl_type_path!(::bevy_platform::hash::NoOpHash);
1223impl_type_path!(::bevy_platform::hash::FixedHasher);
1224impl_type_path!(::bevy_platform::hash::PassHash);
1225impl_reflect_opaque!(::core::net::SocketAddr(
1226 Clone,
1227 Debug,
1228 Hash,
1229 PartialEq,
1230 Serialize,
1231 Deserialize
1232));
1233
1234#[cfg(feature = "std")]
1235impl_reflect_for_hashset!(::std::collections::HashSet<V,S>);
1236#[cfg(feature = "std")]
1237impl_type_path!(::std::collections::HashSet<V, S>);
1238#[cfg(all(feature = "functions", feature = "std"))]
1239crate::func::macros::impl_function_traits!(::std::collections::HashSet<V, S>;
1240 <
1241 V: Hash + Eq + FromReflect + TypePath + GetTypeRegistration,
1242 S: TypePath + BuildHasher + Default + Send + Sync
1243 >
1244);
1245
1246impl_reflect_for_hashset!(::bevy_platform::collections::HashSet<V,S>);
1247impl_type_path!(::bevy_platform::collections::HashSet<V, S>);
1248#[cfg(feature = "functions")]
1249crate::func::macros::impl_function_traits!(::bevy_platform::collections::HashSet<V, S>;
1250 <
1251 V: Hash + Eq + FromReflect + TypePath + GetTypeRegistration,
1252 S: TypePath + BuildHasher + Default + Send + Sync
1253 >
1254);
1255
1256#[cfg(feature = "hashbrown")]
1257impl_reflect_for_hashset!(::hashbrown::hash_set::HashSet<V,S>);
1258#[cfg(feature = "hashbrown")]
1259impl_type_path!(::hashbrown::hash_set::HashSet<V, S>);
1260#[cfg(all(feature = "functions", feature = "hashbrown"))]
1261crate::func::macros::impl_function_traits!(::hashbrown::hash_set::HashSet<V, S>;
1262 <
1263 V: Hash + Eq + FromReflect + TypePath + GetTypeRegistration,
1264 S: TypePath + BuildHasher + Default + Send + Sync
1265 >
1266);
1267
1268impl<K, V> Map for ::alloc::collections::BTreeMap<K, V>
1269where
1270 K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1271 V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1272{
1273 fn get(&self, key: &dyn PartialReflect) -> Option<&dyn PartialReflect> {
1274 key.try_downcast_ref::<K>()
1275 .and_then(|key| Self::get(self, key))
1276 .map(|value| value as &dyn PartialReflect)
1277 }
1278
1279 fn get_mut(&mut self, key: &dyn PartialReflect) -> Option<&mut dyn PartialReflect> {
1280 key.try_downcast_ref::<K>()
1281 .and_then(move |key| Self::get_mut(self, key))
1282 .map(|value| value as &mut dyn PartialReflect)
1283 }
1284
1285 fn get_at(&self, index: usize) -> Option<(&dyn PartialReflect, &dyn PartialReflect)> {
1286 self.iter()
1287 .nth(index)
1288 .map(|(key, value)| (key as &dyn PartialReflect, value as &dyn PartialReflect))
1289 }
1290
1291 fn get_at_mut(
1292 &mut self,
1293 index: usize,
1294 ) -> Option<(&dyn PartialReflect, &mut dyn PartialReflect)> {
1295 self.iter_mut()
1296 .nth(index)
1297 .map(|(key, value)| (key as &dyn PartialReflect, value as &mut dyn PartialReflect))
1298 }
1299
1300 fn len(&self) -> usize {
1301 Self::len(self)
1302 }
1303
1304 fn iter(&self) -> MapIter {
1305 MapIter::new(self)
1306 }
1307
1308 fn drain(&mut self) -> Vec<(Box<dyn PartialReflect>, Box<dyn PartialReflect>)> {
1309 let mut result = Vec::with_capacity(self.len());
1313 while let Some((k, v)) = self.pop_first() {
1314 result.push((
1315 Box::new(k) as Box<dyn PartialReflect>,
1316 Box::new(v) as Box<dyn PartialReflect>,
1317 ));
1318 }
1319 result
1320 }
1321
1322 fn clone_dynamic(&self) -> DynamicMap {
1323 let mut dynamic_map = DynamicMap::default();
1324 dynamic_map.set_represented_type(self.get_represented_type_info());
1325 for (k, v) in self {
1326 let key = K::from_reflect(k).unwrap_or_else(|| {
1327 panic!(
1328 "Attempted to clone invalid key of type {}.",
1329 k.reflect_type_path()
1330 )
1331 });
1332 dynamic_map.insert_boxed(Box::new(key), v.to_dynamic());
1333 }
1334 dynamic_map
1335 }
1336
1337 fn insert_boxed(
1338 &mut self,
1339 key: Box<dyn PartialReflect>,
1340 value: Box<dyn PartialReflect>,
1341 ) -> Option<Box<dyn PartialReflect>> {
1342 let key = K::take_from_reflect(key).unwrap_or_else(|key| {
1343 panic!(
1344 "Attempted to insert invalid key of type {}.",
1345 key.reflect_type_path()
1346 )
1347 });
1348 let value = V::take_from_reflect(value).unwrap_or_else(|value| {
1349 panic!(
1350 "Attempted to insert invalid value of type {}.",
1351 value.reflect_type_path()
1352 )
1353 });
1354 self.insert(key, value)
1355 .map(|old_value| Box::new(old_value) as Box<dyn PartialReflect>)
1356 }
1357
1358 fn remove(&mut self, key: &dyn PartialReflect) -> Option<Box<dyn PartialReflect>> {
1359 let mut from_reflect = None;
1360 key.try_downcast_ref::<K>()
1361 .or_else(|| {
1362 from_reflect = K::from_reflect(key);
1363 from_reflect.as_ref()
1364 })
1365 .and_then(|key| self.remove(key))
1366 .map(|value| Box::new(value) as Box<dyn PartialReflect>)
1367 }
1368}
1369
1370impl<K, V> PartialReflect for ::alloc::collections::BTreeMap<K, V>
1371where
1372 K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1373 V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1374{
1375 fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
1376 Some(<Self as Typed>::type_info())
1377 }
1378 #[inline]
1379 fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
1380 self
1381 }
1382
1383 fn as_partial_reflect(&self) -> &dyn PartialReflect {
1384 self
1385 }
1386
1387 fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
1388 self
1389 }
1390 #[inline]
1391 fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
1392 Ok(self)
1393 }
1394
1395 fn try_as_reflect(&self) -> Option<&dyn Reflect> {
1396 Some(self)
1397 }
1398
1399 fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
1400 Some(self)
1401 }
1402 fn reflect_kind(&self) -> ReflectKind {
1403 ReflectKind::Map
1404 }
1405
1406 fn reflect_ref(&self) -> ReflectRef {
1407 ReflectRef::Map(self)
1408 }
1409
1410 fn reflect_mut(&mut self) -> ReflectMut {
1411 ReflectMut::Map(self)
1412 }
1413
1414 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
1415 ReflectOwned::Map(self)
1416 }
1417
1418 fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
1419 let mut map = Self::new();
1420 for (key, value) in self.iter() {
1421 let key =
1422 key.reflect_clone()?
1423 .take()
1424 .map_err(|_| ReflectCloneError::FailedDowncast {
1425 expected: Cow::Borrowed(<K as TypePath>::type_path()),
1426 received: Cow::Owned(key.reflect_type_path().to_string()),
1427 })?;
1428 let value =
1429 value
1430 .reflect_clone()?
1431 .take()
1432 .map_err(|_| ReflectCloneError::FailedDowncast {
1433 expected: Cow::Borrowed(<V as TypePath>::type_path()),
1434 received: Cow::Owned(value.reflect_type_path().to_string()),
1435 })?;
1436 map.insert(key, value);
1437 }
1438
1439 Ok(Box::new(map))
1440 }
1441
1442 fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
1443 map_partial_eq(self, value)
1444 }
1445
1446 fn apply(&mut self, value: &dyn PartialReflect) {
1447 map_apply(self, value);
1448 }
1449
1450 fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
1451 map_try_apply(self, value)
1452 }
1453}
1454
1455impl_full_reflect!(
1456 <K, V> for ::alloc::collections::BTreeMap<K, V>
1457 where
1458 K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1459 V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1460);
1461
1462impl<K, V> Typed for ::alloc::collections::BTreeMap<K, V>
1463where
1464 K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1465 V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1466{
1467 fn type_info() -> &'static TypeInfo {
1468 static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
1469 CELL.get_or_insert::<Self, _>(|| {
1470 TypeInfo::Map(
1471 MapInfo::new::<Self, K, V>().with_generics(Generics::from_iter([
1472 TypeParamInfo::new::<K>("K"),
1473 TypeParamInfo::new::<V>("V"),
1474 ])),
1475 )
1476 })
1477 }
1478}
1479
1480impl<K, V> GetTypeRegistration for ::alloc::collections::BTreeMap<K, V>
1481where
1482 K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1483 V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1484{
1485 fn get_type_registration() -> TypeRegistration {
1486 let mut registration = TypeRegistration::of::<Self>();
1487 registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
1488 registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
1489 registration
1490 }
1491}
1492
1493impl<K, V> FromReflect for ::alloc::collections::BTreeMap<K, V>
1494where
1495 K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1496 V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration,
1497{
1498 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
1499 let ref_map = reflect.reflect_ref().as_map().ok()?;
1500
1501 let mut new_map = Self::new();
1502
1503 for (key, value) in ref_map.iter() {
1504 let new_key = K::from_reflect(key)?;
1505 let new_value = V::from_reflect(value)?;
1506 new_map.insert(new_key, new_value);
1507 }
1508
1509 Some(new_map)
1510 }
1511}
1512
1513impl_type_path!(::alloc::collections::BTreeMap<K, V>);
1514#[cfg(feature = "functions")]
1515crate::func::macros::impl_function_traits!(::alloc::collections::BTreeMap<K, V>;
1516 <
1517 K: FromReflect + MaybeTyped + TypePath + GetTypeRegistration + Eq + Ord,
1518 V: FromReflect + MaybeTyped + TypePath + GetTypeRegistration
1519 >
1520);
1521
1522impl<T: Reflect + MaybeTyped + TypePath + GetTypeRegistration, const N: usize> Array for [T; N] {
1523 #[inline]
1524 fn get(&self, index: usize) -> Option<&dyn PartialReflect> {
1525 <[T]>::get(self, index).map(|value| value as &dyn PartialReflect)
1526 }
1527
1528 #[inline]
1529 fn get_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect> {
1530 <[T]>::get_mut(self, index).map(|value| value as &mut dyn PartialReflect)
1531 }
1532
1533 #[inline]
1534 fn len(&self) -> usize {
1535 N
1536 }
1537
1538 #[inline]
1539 fn iter(&self) -> ArrayIter {
1540 ArrayIter::new(self)
1541 }
1542
1543 #[inline]
1544 fn drain(self: Box<Self>) -> Vec<Box<dyn PartialReflect>> {
1545 self.into_iter()
1546 .map(|value| Box::new(value) as Box<dyn PartialReflect>)
1547 .collect()
1548 }
1549}
1550
1551impl<T: Reflect + MaybeTyped + TypePath + GetTypeRegistration, const N: usize> PartialReflect
1552 for [T; N]
1553{
1554 fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
1555 Some(<Self as Typed>::type_info())
1556 }
1557
1558 #[inline]
1559 fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
1560 self
1561 }
1562
1563 fn as_partial_reflect(&self) -> &dyn PartialReflect {
1564 self
1565 }
1566
1567 fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
1568 self
1569 }
1570
1571 fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
1572 Ok(self)
1573 }
1574
1575 fn try_as_reflect(&self) -> Option<&dyn Reflect> {
1576 Some(self)
1577 }
1578
1579 fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
1580 Some(self)
1581 }
1582
1583 #[inline]
1584 fn reflect_kind(&self) -> ReflectKind {
1585 ReflectKind::Array
1586 }
1587
1588 #[inline]
1589 fn reflect_ref(&self) -> ReflectRef {
1590 ReflectRef::Array(self)
1591 }
1592
1593 #[inline]
1594 fn reflect_mut(&mut self) -> ReflectMut {
1595 ReflectMut::Array(self)
1596 }
1597
1598 #[inline]
1599 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
1600 ReflectOwned::Array(self)
1601 }
1602
1603 #[inline]
1604 fn reflect_hash(&self) -> Option<u64> {
1605 crate::array_hash(self)
1606 }
1607
1608 #[inline]
1609 fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
1610 crate::array_partial_eq(self, value)
1611 }
1612
1613 fn apply(&mut self, value: &dyn PartialReflect) {
1614 crate::array_apply(self, value);
1615 }
1616
1617 #[inline]
1618 fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
1619 crate::array_try_apply(self, value)
1620 }
1621}
1622
1623impl<T: Reflect + MaybeTyped + TypePath + GetTypeRegistration, const N: usize> Reflect for [T; N] {
1624 #[inline]
1625 fn into_any(self: Box<Self>) -> Box<dyn Any> {
1626 self
1627 }
1628
1629 #[inline]
1630 fn as_any(&self) -> &dyn Any {
1631 self
1632 }
1633
1634 #[inline]
1635 fn as_any_mut(&mut self) -> &mut dyn Any {
1636 self
1637 }
1638
1639 #[inline]
1640 fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
1641 self
1642 }
1643
1644 #[inline]
1645 fn as_reflect(&self) -> &dyn Reflect {
1646 self
1647 }
1648
1649 #[inline]
1650 fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
1651 self
1652 }
1653
1654 #[inline]
1655 fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
1656 *self = value.take()?;
1657 Ok(())
1658 }
1659}
1660
1661impl<T: FromReflect + MaybeTyped + TypePath + GetTypeRegistration, const N: usize> FromReflect
1662 for [T; N]
1663{
1664 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
1665 let ref_array = reflect.reflect_ref().as_array().ok()?;
1666
1667 let mut temp_vec = Vec::with_capacity(ref_array.len());
1668
1669 for field in ref_array.iter() {
1670 temp_vec.push(T::from_reflect(field)?);
1671 }
1672
1673 temp_vec.try_into().ok()
1674 }
1675}
1676
1677impl<T: Reflect + MaybeTyped + TypePath + GetTypeRegistration, const N: usize> Typed for [T; N] {
1678 fn type_info() -> &'static TypeInfo {
1679 static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
1680 CELL.get_or_insert::<Self, _>(|| TypeInfo::Array(ArrayInfo::new::<Self, T>(N)))
1681 }
1682}
1683
1684impl<T: TypePath, const N: usize> TypePath for [T; N] {
1685 fn type_path() -> &'static str {
1686 static CELL: GenericTypePathCell = GenericTypePathCell::new();
1687 CELL.get_or_insert::<Self, _>(|| format!("[{t}; {N}]", t = T::type_path()))
1688 }
1689
1690 fn short_type_path() -> &'static str {
1691 static CELL: GenericTypePathCell = GenericTypePathCell::new();
1692 CELL.get_or_insert::<Self, _>(|| format!("[{t}; {N}]", t = T::short_type_path()))
1693 }
1694}
1695
1696impl<T: Reflect + MaybeTyped + TypePath + GetTypeRegistration, const N: usize> GetTypeRegistration
1697 for [T; N]
1698{
1699 fn get_type_registration() -> TypeRegistration {
1700 TypeRegistration::of::<[T; N]>()
1701 }
1702
1703 fn register_type_dependencies(registry: &mut TypeRegistry) {
1704 registry.register::<T>();
1705 }
1706}
1707
1708#[cfg(feature = "functions")]
1709crate::func::macros::impl_function_traits!([T; N]; <T: Reflect + MaybeTyped + TypePath + GetTypeRegistration> [const N: usize]);
1710
1711impl_reflect! {
1712 #[type_path = "core::option"]
1713 enum Option<T> {
1714 None,
1715 Some(T),
1716 }
1717}
1718
1719impl_reflect! {
1720 #[type_path = "core::result"]
1721 enum Result<T, E> {
1722 Ok(T),
1723 Err(E),
1724 }
1725}
1726
1727impl<T: TypePath + ?Sized> TypePath for &'static T {
1728 fn type_path() -> &'static str {
1729 static CELL: GenericTypePathCell = GenericTypePathCell::new();
1730 CELL.get_or_insert::<Self, _>(|| format!("&{}", T::type_path()))
1731 }
1732
1733 fn short_type_path() -> &'static str {
1734 static CELL: GenericTypePathCell = GenericTypePathCell::new();
1735 CELL.get_or_insert::<Self, _>(|| format!("&{}", T::short_type_path()))
1736 }
1737}
1738
1739impl<T: TypePath + ?Sized> TypePath for &'static mut T {
1740 fn type_path() -> &'static str {
1741 static CELL: GenericTypePathCell = GenericTypePathCell::new();
1742 CELL.get_or_insert::<Self, _>(|| format!("&mut {}", T::type_path()))
1743 }
1744
1745 fn short_type_path() -> &'static str {
1746 static CELL: GenericTypePathCell = GenericTypePathCell::new();
1747 CELL.get_or_insert::<Self, _>(|| format!("&mut {}", T::short_type_path()))
1748 }
1749}
1750
1751impl PartialReflect for Cow<'static, str> {
1752 fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
1753 Some(<Self as Typed>::type_info())
1754 }
1755
1756 #[inline]
1757 fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
1758 self
1759 }
1760
1761 fn as_partial_reflect(&self) -> &dyn PartialReflect {
1762 self
1763 }
1764
1765 fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
1766 self
1767 }
1768
1769 fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
1770 Ok(self)
1771 }
1772
1773 fn try_as_reflect(&self) -> Option<&dyn Reflect> {
1774 Some(self)
1775 }
1776
1777 fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
1778 Some(self)
1779 }
1780
1781 fn reflect_kind(&self) -> ReflectKind {
1782 ReflectKind::Opaque
1783 }
1784
1785 fn reflect_ref(&self) -> ReflectRef {
1786 ReflectRef::Opaque(self)
1787 }
1788
1789 fn reflect_mut(&mut self) -> ReflectMut {
1790 ReflectMut::Opaque(self)
1791 }
1792
1793 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
1794 ReflectOwned::Opaque(self)
1795 }
1796
1797 fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
1798 Ok(Box::new(self.clone()))
1799 }
1800
1801 fn reflect_hash(&self) -> Option<u64> {
1802 let mut hasher = reflect_hasher();
1803 Hash::hash(&Any::type_id(self), &mut hasher);
1804 Hash::hash(self, &mut hasher);
1805 Some(hasher.finish())
1806 }
1807
1808 fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
1809 if let Some(value) = value.try_downcast_ref::<Self>() {
1810 Some(PartialEq::eq(self, value))
1811 } else {
1812 Some(false)
1813 }
1814 }
1815
1816 fn debug(&self, f: &mut fmt::Formatter<'_>) -> core::fmt::Result {
1817 fmt::Debug::fmt(self, f)
1818 }
1819
1820 fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
1821 if let Some(value) = value.try_downcast_ref::<Self>() {
1822 self.clone_from(value);
1823 } else {
1824 return Err(ApplyError::MismatchedTypes {
1825 from_type: value.reflect_type_path().into(),
1826 to_type: Self::type_path().into(),
1828 });
1829 }
1830 Ok(())
1831 }
1832}
1833
1834impl_full_reflect!(for Cow<'static, str>);
1835
1836impl Typed for Cow<'static, str> {
1837 fn type_info() -> &'static TypeInfo {
1838 static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
1839 CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
1840 }
1841}
1842
1843impl GetTypeRegistration for Cow<'static, str> {
1844 fn get_type_registration() -> TypeRegistration {
1845 let mut registration = TypeRegistration::of::<Cow<'static, str>>();
1846 registration.insert::<ReflectDeserialize>(FromType::<Cow<'static, str>>::from_type());
1847 registration.insert::<ReflectFromPtr>(FromType::<Cow<'static, str>>::from_type());
1848 registration.insert::<ReflectFromReflect>(FromType::<Cow<'static, str>>::from_type());
1849 registration.insert::<ReflectSerialize>(FromType::<Cow<'static, str>>::from_type());
1850 registration
1851 }
1852}
1853
1854impl FromReflect for Cow<'static, str> {
1855 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
1856 Some(reflect.try_downcast_ref::<Cow<'static, str>>()?.clone())
1857 }
1858}
1859
1860#[cfg(feature = "functions")]
1861crate::func::macros::impl_function_traits!(Cow<'static, str>);
1862
1863impl<T: TypePath> TypePath for [T]
1864where
1865 [T]: ToOwned,
1866{
1867 fn type_path() -> &'static str {
1868 static CELL: GenericTypePathCell = GenericTypePathCell::new();
1869 CELL.get_or_insert::<Self, _>(|| format!("[{}]", <T>::type_path()))
1870 }
1871
1872 fn short_type_path() -> &'static str {
1873 static CELL: GenericTypePathCell = GenericTypePathCell::new();
1874 CELL.get_or_insert::<Self, _>(|| format!("[{}]", <T>::short_type_path()))
1875 }
1876}
1877
1878impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> List
1879 for Cow<'static, [T]>
1880{
1881 fn get(&self, index: usize) -> Option<&dyn PartialReflect> {
1882 self.as_ref().get(index).map(|x| x as &dyn PartialReflect)
1883 }
1884
1885 fn get_mut(&mut self, index: usize) -> Option<&mut dyn PartialReflect> {
1886 self.to_mut()
1887 .get_mut(index)
1888 .map(|x| x as &mut dyn PartialReflect)
1889 }
1890
1891 fn insert(&mut self, index: usize, element: Box<dyn PartialReflect>) {
1892 let value = T::take_from_reflect(element).unwrap_or_else(|value| {
1893 panic!(
1894 "Attempted to insert invalid value of type {}.",
1895 value.reflect_type_path()
1896 );
1897 });
1898 self.to_mut().insert(index, value);
1899 }
1900
1901 fn remove(&mut self, index: usize) -> Box<dyn PartialReflect> {
1902 Box::new(self.to_mut().remove(index))
1903 }
1904
1905 fn push(&mut self, value: Box<dyn PartialReflect>) {
1906 let value = T::take_from_reflect(value).unwrap_or_else(|value| {
1907 panic!(
1908 "Attempted to push invalid value of type {}.",
1909 value.reflect_type_path()
1910 )
1911 });
1912 self.to_mut().push(value);
1913 }
1914
1915 fn pop(&mut self) -> Option<Box<dyn PartialReflect>> {
1916 self.to_mut()
1917 .pop()
1918 .map(|value| Box::new(value) as Box<dyn PartialReflect>)
1919 }
1920
1921 fn len(&self) -> usize {
1922 self.as_ref().len()
1923 }
1924
1925 fn iter(&self) -> ListIter {
1926 ListIter::new(self)
1927 }
1928
1929 fn drain(&mut self) -> Vec<Box<dyn PartialReflect>> {
1930 self.to_mut()
1931 .drain(..)
1932 .map(|value| Box::new(value) as Box<dyn PartialReflect>)
1933 .collect()
1934 }
1935}
1936
1937impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> PartialReflect
1938 for Cow<'static, [T]>
1939{
1940 fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
1941 Some(<Self as Typed>::type_info())
1942 }
1943
1944 #[inline]
1945 fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
1946 self
1947 }
1948
1949 fn as_partial_reflect(&self) -> &dyn PartialReflect {
1950 self
1951 }
1952
1953 fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
1954 self
1955 }
1956
1957 fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
1958 Ok(self)
1959 }
1960
1961 fn try_as_reflect(&self) -> Option<&dyn Reflect> {
1962 Some(self)
1963 }
1964
1965 fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
1966 Some(self)
1967 }
1968
1969 fn reflect_kind(&self) -> ReflectKind {
1970 ReflectKind::List
1971 }
1972
1973 fn reflect_ref(&self) -> ReflectRef {
1974 ReflectRef::List(self)
1975 }
1976
1977 fn reflect_mut(&mut self) -> ReflectMut {
1978 ReflectMut::List(self)
1979 }
1980
1981 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
1982 ReflectOwned::List(self)
1983 }
1984
1985 fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
1986 Ok(Box::new(self.clone()))
1987 }
1988
1989 fn reflect_hash(&self) -> Option<u64> {
1990 crate::list_hash(self)
1991 }
1992
1993 fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
1994 crate::list_partial_eq(self, value)
1995 }
1996
1997 fn apply(&mut self, value: &dyn PartialReflect) {
1998 crate::list_apply(self, value);
1999 }
2000
2001 fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
2002 crate::list_try_apply(self, value)
2003 }
2004}
2005
2006impl_full_reflect!(
2007 <T> for Cow<'static, [T]>
2008 where
2009 T: FromReflect + Clone + MaybeTyped + TypePath + GetTypeRegistration,
2010);
2011
2012impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> Typed
2013 for Cow<'static, [T]>
2014{
2015 fn type_info() -> &'static TypeInfo {
2016 static CELL: GenericTypeInfoCell = GenericTypeInfoCell::new();
2017 CELL.get_or_insert::<Self, _>(|| TypeInfo::List(ListInfo::new::<Self, T>()))
2018 }
2019}
2020
2021impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> GetTypeRegistration
2022 for Cow<'static, [T]>
2023{
2024 fn get_type_registration() -> TypeRegistration {
2025 TypeRegistration::of::<Cow<'static, [T]>>()
2026 }
2027
2028 fn register_type_dependencies(registry: &mut TypeRegistry) {
2029 registry.register::<T>();
2030 }
2031}
2032
2033impl<T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration> FromReflect
2034 for Cow<'static, [T]>
2035{
2036 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
2037 let ref_list = reflect.reflect_ref().as_list().ok()?;
2038
2039 let mut temp_vec = Vec::with_capacity(ref_list.len());
2040
2041 for field in ref_list.iter() {
2042 temp_vec.push(T::from_reflect(field)?);
2043 }
2044
2045 Some(temp_vec.into())
2046 }
2047}
2048
2049#[cfg(feature = "functions")]
2050crate::func::macros::impl_function_traits!(Cow<'static, [T]>; <T: FromReflect + MaybeTyped + Clone + TypePath + GetTypeRegistration>);
2051
2052impl PartialReflect for &'static str {
2053 fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
2054 Some(<Self as Typed>::type_info())
2055 }
2056
2057 #[inline]
2058 fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
2059 self
2060 }
2061
2062 fn as_partial_reflect(&self) -> &dyn PartialReflect {
2063 self
2064 }
2065
2066 fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
2067 self
2068 }
2069
2070 fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
2071 Ok(self)
2072 }
2073
2074 fn try_as_reflect(&self) -> Option<&dyn Reflect> {
2075 Some(self)
2076 }
2077
2078 fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
2079 Some(self)
2080 }
2081
2082 fn reflect_ref(&self) -> ReflectRef {
2083 ReflectRef::Opaque(self)
2084 }
2085
2086 fn reflect_mut(&mut self) -> ReflectMut {
2087 ReflectMut::Opaque(self)
2088 }
2089
2090 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
2091 ReflectOwned::Opaque(self)
2092 }
2093
2094 fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
2095 Ok(Box::new(*self))
2096 }
2097
2098 fn reflect_hash(&self) -> Option<u64> {
2099 let mut hasher = reflect_hasher();
2100 Hash::hash(&Any::type_id(self), &mut hasher);
2101 Hash::hash(self, &mut hasher);
2102 Some(hasher.finish())
2103 }
2104
2105 fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
2106 if let Some(value) = value.try_downcast_ref::<Self>() {
2107 Some(PartialEq::eq(self, value))
2108 } else {
2109 Some(false)
2110 }
2111 }
2112
2113 fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2114 fmt::Debug::fmt(&self, f)
2115 }
2116
2117 fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
2118 if let Some(value) = value.try_downcast_ref::<Self>() {
2119 self.clone_from(value);
2120 } else {
2121 return Err(ApplyError::MismatchedTypes {
2122 from_type: value.reflect_type_path().into(),
2123 to_type: Self::type_path().into(),
2124 });
2125 }
2126 Ok(())
2127 }
2128}
2129
2130impl Reflect for &'static str {
2131 fn into_any(self: Box<Self>) -> Box<dyn Any> {
2132 self
2133 }
2134
2135 fn as_any(&self) -> &dyn Any {
2136 self
2137 }
2138
2139 fn as_any_mut(&mut self) -> &mut dyn Any {
2140 self
2141 }
2142
2143 fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
2144 self
2145 }
2146
2147 fn as_reflect(&self) -> &dyn Reflect {
2148 self
2149 }
2150
2151 fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
2152 self
2153 }
2154
2155 fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
2156 *self = value.take()?;
2157 Ok(())
2158 }
2159}
2160
2161impl Typed for &'static str {
2162 fn type_info() -> &'static TypeInfo {
2163 static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
2164 CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
2165 }
2166}
2167
2168impl GetTypeRegistration for &'static str {
2169 fn get_type_registration() -> TypeRegistration {
2170 let mut registration = TypeRegistration::of::<Self>();
2171 registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
2172 registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
2173 registration
2174 }
2175}
2176
2177impl FromReflect for &'static str {
2178 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
2179 reflect.try_downcast_ref::<Self>().copied()
2180 }
2181}
2182
2183#[cfg(feature = "functions")]
2184crate::func::macros::impl_function_traits!(&'static str);
2185
2186#[cfg(feature = "std")]
2187impl PartialReflect for &'static Path {
2188 fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
2189 Some(<Self as Typed>::type_info())
2190 }
2191
2192 #[inline]
2193 fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
2194 self
2195 }
2196
2197 fn as_partial_reflect(&self) -> &dyn PartialReflect {
2198 self
2199 }
2200
2201 fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
2202 self
2203 }
2204
2205 fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
2206 Ok(self)
2207 }
2208
2209 fn try_as_reflect(&self) -> Option<&dyn Reflect> {
2210 Some(self)
2211 }
2212
2213 fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
2214 Some(self)
2215 }
2216
2217 fn reflect_kind(&self) -> ReflectKind {
2218 ReflectKind::Opaque
2219 }
2220
2221 fn reflect_ref(&self) -> ReflectRef {
2222 ReflectRef::Opaque(self)
2223 }
2224
2225 fn reflect_mut(&mut self) -> ReflectMut {
2226 ReflectMut::Opaque(self)
2227 }
2228
2229 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
2230 ReflectOwned::Opaque(self)
2231 }
2232
2233 fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
2234 Ok(Box::new(*self))
2235 }
2236
2237 fn reflect_hash(&self) -> Option<u64> {
2238 let mut hasher = reflect_hasher();
2239 Hash::hash(&Any::type_id(self), &mut hasher);
2240 Hash::hash(self, &mut hasher);
2241 Some(hasher.finish())
2242 }
2243
2244 fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
2245 if let Some(value) = value.try_downcast_ref::<Self>() {
2246 Some(PartialEq::eq(self, value))
2247 } else {
2248 Some(false)
2249 }
2250 }
2251
2252 fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
2253 if let Some(value) = value.try_downcast_ref::<Self>() {
2254 self.clone_from(value);
2255 Ok(())
2256 } else {
2257 Err(ApplyError::MismatchedTypes {
2258 from_type: value.reflect_type_path().into(),
2259 to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),
2260 })
2261 }
2262 }
2263}
2264
2265#[cfg(feature = "std")]
2266impl Reflect for &'static Path {
2267 fn into_any(self: Box<Self>) -> Box<dyn Any> {
2268 self
2269 }
2270
2271 fn as_any(&self) -> &dyn Any {
2272 self
2273 }
2274
2275 fn as_any_mut(&mut self) -> &mut dyn Any {
2276 self
2277 }
2278
2279 fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
2280 self
2281 }
2282
2283 fn as_reflect(&self) -> &dyn Reflect {
2284 self
2285 }
2286
2287 fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
2288 self
2289 }
2290
2291 fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
2292 *self = value.take()?;
2293 Ok(())
2294 }
2295}
2296
2297#[cfg(feature = "std")]
2298impl Typed for &'static Path {
2299 fn type_info() -> &'static TypeInfo {
2300 static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
2301 CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
2302 }
2303}
2304
2305#[cfg(feature = "std")]
2306impl GetTypeRegistration for &'static Path {
2307 fn get_type_registration() -> TypeRegistration {
2308 let mut registration = TypeRegistration::of::<Self>();
2309 registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
2310 registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
2311 registration
2312 }
2313}
2314
2315#[cfg(feature = "std")]
2316impl FromReflect for &'static Path {
2317 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
2318 reflect.try_downcast_ref::<Self>().copied()
2319 }
2320}
2321
2322#[cfg(all(feature = "functions", feature = "std"))]
2323crate::func::macros::impl_function_traits!(&'static Path);
2324
2325#[cfg(feature = "std")]
2326impl PartialReflect for Cow<'static, Path> {
2327 fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
2328 Some(<Self as Typed>::type_info())
2329 }
2330
2331 #[inline]
2332 fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
2333 self
2334 }
2335
2336 fn as_partial_reflect(&self) -> &dyn PartialReflect {
2337 self
2338 }
2339
2340 fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
2341 self
2342 }
2343
2344 fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
2345 Ok(self)
2346 }
2347
2348 fn try_as_reflect(&self) -> Option<&dyn Reflect> {
2349 Some(self)
2350 }
2351
2352 fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
2353 Some(self)
2354 }
2355
2356 fn reflect_kind(&self) -> ReflectKind {
2357 ReflectKind::Opaque
2358 }
2359
2360 fn reflect_ref(&self) -> ReflectRef {
2361 ReflectRef::Opaque(self)
2362 }
2363
2364 fn reflect_mut(&mut self) -> ReflectMut {
2365 ReflectMut::Opaque(self)
2366 }
2367
2368 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
2369 ReflectOwned::Opaque(self)
2370 }
2371
2372 fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
2373 Ok(Box::new(self.clone()))
2374 }
2375
2376 fn reflect_hash(&self) -> Option<u64> {
2377 let mut hasher = reflect_hasher();
2378 Hash::hash(&Any::type_id(self), &mut hasher);
2379 Hash::hash(self, &mut hasher);
2380 Some(hasher.finish())
2381 }
2382
2383 fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
2384 if let Some(value) = value.try_downcast_ref::<Self>() {
2385 Some(PartialEq::eq(self, value))
2386 } else {
2387 Some(false)
2388 }
2389 }
2390
2391 fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2392 fmt::Debug::fmt(&self, f)
2393 }
2394
2395 fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
2396 if let Some(value) = value.try_downcast_ref::<Self>() {
2397 self.clone_from(value);
2398 Ok(())
2399 } else {
2400 Err(ApplyError::MismatchedTypes {
2401 from_type: value.reflect_type_path().into(),
2402 to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),
2403 })
2404 }
2405 }
2406}
2407
2408#[cfg(feature = "std")]
2409impl Reflect for Cow<'static, Path> {
2410 fn into_any(self: Box<Self>) -> Box<dyn Any> {
2411 self
2412 }
2413
2414 fn as_any(&self) -> &dyn Any {
2415 self
2416 }
2417
2418 fn as_any_mut(&mut self) -> &mut dyn Any {
2419 self
2420 }
2421
2422 fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
2423 self
2424 }
2425
2426 fn as_reflect(&self) -> &dyn Reflect {
2427 self
2428 }
2429
2430 fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
2431 self
2432 }
2433
2434 fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
2435 *self = value.take()?;
2436 Ok(())
2437 }
2438}
2439
2440#[cfg(feature = "std")]
2441impl Typed for Cow<'static, Path> {
2442 fn type_info() -> &'static TypeInfo {
2443 static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
2444 CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
2445 }
2446}
2447
2448#[cfg(feature = "std")]
2449impl_type_path!(::std::path::Path);
2450impl_type_path!(::alloc::borrow::Cow<'a: 'static, T: ToOwned + ?Sized>);
2451
2452#[cfg(feature = "std")]
2453impl FromReflect for Cow<'static, Path> {
2454 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
2455 Some(reflect.try_downcast_ref::<Self>()?.clone())
2456 }
2457}
2458
2459#[cfg(feature = "std")]
2460impl GetTypeRegistration for Cow<'static, Path> {
2461 fn get_type_registration() -> TypeRegistration {
2462 let mut registration = TypeRegistration::of::<Self>();
2463 registration.insert::<ReflectDeserialize>(FromType::<Self>::from_type());
2464 registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
2465 registration.insert::<ReflectSerialize>(FromType::<Self>::from_type());
2466 registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
2467 registration
2468 }
2469}
2470
2471#[cfg(all(feature = "functions", feature = "std"))]
2472crate::func::macros::impl_function_traits!(Cow<'static, Path>);
2473
2474impl TypePath for &'static Location<'static> {
2475 fn type_path() -> &'static str {
2476 "core::panic::Location"
2477 }
2478
2479 fn short_type_path() -> &'static str {
2480 "Location"
2481 }
2482}
2483
2484impl PartialReflect for &'static Location<'static> {
2485 fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
2486 Some(<Self as Typed>::type_info())
2487 }
2488
2489 #[inline]
2490 fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
2491 self
2492 }
2493
2494 fn as_partial_reflect(&self) -> &dyn PartialReflect {
2495 self
2496 }
2497
2498 fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
2499 self
2500 }
2501
2502 fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
2503 Ok(self)
2504 }
2505
2506 fn try_as_reflect(&self) -> Option<&dyn Reflect> {
2507 Some(self)
2508 }
2509
2510 fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
2511 Some(self)
2512 }
2513
2514 fn reflect_kind(&self) -> ReflectKind {
2515 ReflectKind::Opaque
2516 }
2517
2518 fn reflect_ref(&self) -> ReflectRef {
2519 ReflectRef::Opaque(self)
2520 }
2521
2522 fn reflect_mut(&mut self) -> ReflectMut {
2523 ReflectMut::Opaque(self)
2524 }
2525
2526 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
2527 ReflectOwned::Opaque(self)
2528 }
2529
2530 fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
2531 Ok(Box::new(*self))
2532 }
2533
2534 fn reflect_hash(&self) -> Option<u64> {
2535 let mut hasher = reflect_hasher();
2536 Hash::hash(&Any::type_id(self), &mut hasher);
2537 Hash::hash(self, &mut hasher);
2538 Some(hasher.finish())
2539 }
2540
2541 fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
2542 if let Some(value) = value.try_downcast_ref::<Self>() {
2543 Some(PartialEq::eq(self, value))
2544 } else {
2545 Some(false)
2546 }
2547 }
2548
2549 fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
2550 if let Some(value) = value.try_downcast_ref::<Self>() {
2551 self.clone_from(value);
2552 Ok(())
2553 } else {
2554 Err(ApplyError::MismatchedTypes {
2555 from_type: value.reflect_type_path().into(),
2556 to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),
2557 })
2558 }
2559 }
2560}
2561
2562impl Reflect for &'static Location<'static> {
2563 fn into_any(self: Box<Self>) -> Box<dyn Any> {
2564 self
2565 }
2566
2567 fn as_any(&self) -> &dyn Any {
2568 self
2569 }
2570
2571 fn as_any_mut(&mut self) -> &mut dyn Any {
2572 self
2573 }
2574
2575 fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
2576 self
2577 }
2578
2579 fn as_reflect(&self) -> &dyn Reflect {
2580 self
2581 }
2582
2583 fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
2584 self
2585 }
2586
2587 fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
2588 *self = value.take()?;
2589 Ok(())
2590 }
2591}
2592
2593impl Typed for &'static Location<'static> {
2594 fn type_info() -> &'static TypeInfo {
2595 static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
2596 CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
2597 }
2598}
2599
2600impl GetTypeRegistration for &'static Location<'static> {
2601 fn get_type_registration() -> TypeRegistration {
2602 let mut registration = TypeRegistration::of::<Self>();
2603 registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
2604 registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
2605 registration
2606 }
2607}
2608
2609impl FromReflect for &'static Location<'static> {
2610 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
2611 reflect.try_downcast_ref::<Self>().copied()
2612 }
2613}
2614
2615#[cfg(all(feature = "functions", feature = "std"))]
2616crate::func::macros::impl_function_traits!(&'static Location<'static>);
2617
2618#[cfg(test)]
2619mod tests {
2620 use crate::{
2621 Enum, FromReflect, PartialReflect, Reflect, ReflectSerialize, TypeInfo, TypeRegistry,
2622 Typed, VariantInfo, VariantType,
2623 };
2624 use alloc::{collections::BTreeMap, string::String, vec};
2625 use bevy_platform::collections::HashMap;
2626 use bevy_platform::time::Instant;
2627 use core::{
2628 f32::consts::{PI, TAU},
2629 time::Duration,
2630 };
2631 use static_assertions::assert_impl_all;
2632 use std::path::Path;
2633
2634 #[test]
2635 fn can_serialize_duration() {
2636 let mut type_registry = TypeRegistry::default();
2637 type_registry.register::<Duration>();
2638
2639 let reflect_serialize = type_registry
2640 .get_type_data::<ReflectSerialize>(core::any::TypeId::of::<Duration>())
2641 .unwrap();
2642 let _serializable = reflect_serialize.get_serializable(&Duration::ZERO);
2643 }
2644
2645 #[test]
2646 fn should_partial_eq_char() {
2647 let a: &dyn PartialReflect = &'x';
2648 let b: &dyn PartialReflect = &'x';
2649 let c: &dyn PartialReflect = &'o';
2650 assert!(a.reflect_partial_eq(b).unwrap_or_default());
2651 assert!(!a.reflect_partial_eq(c).unwrap_or_default());
2652 }
2653
2654 #[test]
2655 fn should_partial_eq_i32() {
2656 let a: &dyn PartialReflect = &123_i32;
2657 let b: &dyn PartialReflect = &123_i32;
2658 let c: &dyn PartialReflect = &321_i32;
2659 assert!(a.reflect_partial_eq(b).unwrap_or_default());
2660 assert!(!a.reflect_partial_eq(c).unwrap_or_default());
2661 }
2662
2663 #[test]
2664 fn should_partial_eq_f32() {
2665 let a: &dyn PartialReflect = &PI;
2666 let b: &dyn PartialReflect = &PI;
2667 let c: &dyn PartialReflect = &TAU;
2668 assert!(a.reflect_partial_eq(b).unwrap_or_default());
2669 assert!(!a.reflect_partial_eq(c).unwrap_or_default());
2670 }
2671
2672 #[test]
2673 fn should_partial_eq_string() {
2674 let a: &dyn PartialReflect = &String::from("Hello");
2675 let b: &dyn PartialReflect = &String::from("Hello");
2676 let c: &dyn PartialReflect = &String::from("World");
2677 assert!(a.reflect_partial_eq(b).unwrap_or_default());
2678 assert!(!a.reflect_partial_eq(c).unwrap_or_default());
2679 }
2680
2681 #[test]
2682 fn should_partial_eq_vec() {
2683 let a: &dyn PartialReflect = &vec![1, 2, 3];
2684 let b: &dyn PartialReflect = &vec![1, 2, 3];
2685 let c: &dyn PartialReflect = &vec![3, 2, 1];
2686 assert!(a.reflect_partial_eq(b).unwrap_or_default());
2687 assert!(!a.reflect_partial_eq(c).unwrap_or_default());
2688 }
2689
2690 #[test]
2691 fn should_partial_eq_hash_map() {
2692 let mut a = <HashMap<_, _>>::default();
2693 a.insert(0usize, 1.23_f64);
2694 let b = a.clone();
2695 let mut c = <HashMap<_, _>>::default();
2696 c.insert(0usize, 3.21_f64);
2697
2698 let a: &dyn PartialReflect = &a;
2699 let b: &dyn PartialReflect = &b;
2700 let c: &dyn PartialReflect = &c;
2701 assert!(a.reflect_partial_eq(b).unwrap_or_default());
2702 assert!(!a.reflect_partial_eq(c).unwrap_or_default());
2703 }
2704
2705 #[test]
2706 fn should_partial_eq_btree_map() {
2707 let mut a = BTreeMap::new();
2708 a.insert(0usize, 1.23_f64);
2709 let b = a.clone();
2710 let mut c = BTreeMap::new();
2711 c.insert(0usize, 3.21_f64);
2712
2713 let a: &dyn Reflect = &a;
2714 let b: &dyn Reflect = &b;
2715 let c: &dyn Reflect = &c;
2716 assert!(a
2717 .reflect_partial_eq(b.as_partial_reflect())
2718 .unwrap_or_default());
2719 assert!(!a
2720 .reflect_partial_eq(c.as_partial_reflect())
2721 .unwrap_or_default());
2722 }
2723
2724 #[test]
2725 fn should_partial_eq_option() {
2726 let a: &dyn PartialReflect = &Some(123);
2727 let b: &dyn PartialReflect = &Some(123);
2728 assert_eq!(Some(true), a.reflect_partial_eq(b));
2729 }
2730
2731 #[test]
2732 fn option_should_impl_enum() {
2733 assert_impl_all!(Option<()>: Enum);
2734
2735 let mut value = Some(123usize);
2736
2737 assert!(value
2738 .reflect_partial_eq(&Some(123usize))
2739 .unwrap_or_default());
2740 assert!(!value
2741 .reflect_partial_eq(&Some(321usize))
2742 .unwrap_or_default());
2743
2744 assert_eq!("Some", value.variant_name());
2745 assert_eq!("core::option::Option<usize>::Some", value.variant_path());
2746
2747 if value.is_variant(VariantType::Tuple) {
2748 if let Some(field) = value
2749 .field_at_mut(0)
2750 .and_then(|field| field.try_downcast_mut::<usize>())
2751 {
2752 *field = 321;
2753 }
2754 } else {
2755 panic!("expected `VariantType::Tuple`");
2756 }
2757
2758 assert_eq!(Some(321), value);
2759 }
2760
2761 #[test]
2762 fn option_should_from_reflect() {
2763 #[derive(Reflect, PartialEq, Debug)]
2764 struct Foo(usize);
2765
2766 let expected = Some(Foo(123));
2767 let output = <Option<Foo> as FromReflect>::from_reflect(&expected).unwrap();
2768
2769 assert_eq!(expected, output);
2770 }
2771
2772 #[test]
2773 fn option_should_apply() {
2774 #[derive(Reflect, PartialEq, Debug)]
2775 struct Foo(usize);
2776
2777 let patch = None::<Foo>;
2779 let mut value = None::<Foo>;
2780 PartialReflect::apply(&mut value, &patch);
2781
2782 assert_eq!(patch, value, "None apply onto None");
2783
2784 let patch = Some(Foo(123));
2786 let mut value = None::<Foo>;
2787 PartialReflect::apply(&mut value, &patch);
2788
2789 assert_eq!(patch, value, "Some apply onto None");
2790
2791 let patch = None::<Foo>;
2793 let mut value = Some(Foo(321));
2794 PartialReflect::apply(&mut value, &patch);
2795
2796 assert_eq!(patch, value, "None apply onto Some");
2797
2798 let patch = Some(Foo(123));
2800 let mut value = Some(Foo(321));
2801 PartialReflect::apply(&mut value, &patch);
2802
2803 assert_eq!(patch, value, "Some apply onto Some");
2804 }
2805
2806 #[test]
2807 fn option_should_impl_typed() {
2808 assert_impl_all!(Option<()>: Typed);
2809
2810 type MyOption = Option<i32>;
2811 let info = MyOption::type_info();
2812 if let TypeInfo::Enum(info) = info {
2813 assert_eq!(
2814 "None",
2815 info.variant_at(0).unwrap().name(),
2816 "Expected `None` to be variant at index `0`"
2817 );
2818 assert_eq!(
2819 "Some",
2820 info.variant_at(1).unwrap().name(),
2821 "Expected `Some` to be variant at index `1`"
2822 );
2823 assert_eq!("Some", info.variant("Some").unwrap().name());
2824 if let VariantInfo::Tuple(variant) = info.variant("Some").unwrap() {
2825 assert!(
2826 variant.field_at(0).unwrap().is::<i32>(),
2827 "Expected `Some` variant to contain `i32`"
2828 );
2829 assert!(
2830 variant.field_at(1).is_none(),
2831 "Expected `Some` variant to only contain 1 field"
2832 );
2833 } else {
2834 panic!("Expected `VariantInfo::Tuple`");
2835 }
2836 } else {
2837 panic!("Expected `TypeInfo::Enum`");
2838 }
2839 }
2840
2841 #[test]
2842 fn nonzero_usize_impl_reflect_from_reflect() {
2843 let a: &dyn PartialReflect = &core::num::NonZero::<usize>::new(42).unwrap();
2844 let b: &dyn PartialReflect = &core::num::NonZero::<usize>::new(42).unwrap();
2845 assert!(a.reflect_partial_eq(b).unwrap_or_default());
2846 let forty_two: core::num::NonZero<usize> = FromReflect::from_reflect(a).unwrap();
2847 assert_eq!(forty_two, core::num::NonZero::<usize>::new(42).unwrap());
2848 }
2849
2850 #[test]
2851 fn instant_should_from_reflect() {
2852 let expected = Instant::now();
2853 let output = <Instant as FromReflect>::from_reflect(&expected).unwrap();
2854 assert_eq!(expected, output);
2855 }
2856
2857 #[test]
2858 fn path_should_from_reflect() {
2859 let path = Path::new("hello_world.rs");
2860 let output = <&'static Path as FromReflect>::from_reflect(&path).unwrap();
2861 assert_eq!(path, output);
2862 }
2863
2864 #[test]
2865 fn type_id_should_from_reflect() {
2866 let type_id = core::any::TypeId::of::<usize>();
2867 let output = <core::any::TypeId as FromReflect>::from_reflect(&type_id).unwrap();
2868 assert_eq!(type_id, output);
2869 }
2870
2871 #[test]
2872 fn static_str_should_from_reflect() {
2873 let expected = "Hello, World!";
2874 let output = <&'static str as FromReflect>::from_reflect(&expected).unwrap();
2875 assert_eq!(expected, output);
2876 }
2877
2878 #[test]
2879 fn should_reflect_hashmaps() {
2880 assert_impl_all!(std::collections::HashMap<u32, f32>: Reflect);
2881 assert_impl_all!(bevy_platform::collections::HashMap<u32, f32>: Reflect);
2882
2883 #[cfg(feature = "hashbrown")]
2886 assert_impl_all!(hashbrown::HashMap<u32, f32, foldhash::fast::RandomState>: Reflect);
2887 }
2888}