bevy_reflect/impls/core/
panic.rs1use crate::{
2 error::ReflectCloneError,
3 kind::{ReflectKind, ReflectMut, ReflectOwned, ReflectRef},
4 prelude::*,
5 reflect::ApplyError,
6 type_info::{OpaqueInfo, TypeInfo, Typed},
7 type_path::DynamicTypePath,
8 type_registry::{FromType, GetTypeRegistration, ReflectFromPtr, TypeRegistration},
9 utility::{reflect_hasher, NonGenericTypeInfoCell},
10};
11use bevy_platform::prelude::*;
12use core::any::Any;
13use core::hash::{Hash, Hasher};
14use core::panic::Location;
15
16impl TypePath for &'static Location<'static> {
17 fn type_path() -> &'static str {
18 "core::panic::Location"
19 }
20
21 fn short_type_path() -> &'static str {
22 "Location"
23 }
24}
25
26impl PartialReflect for &'static Location<'static> {
27 fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
28 Some(<Self as Typed>::type_info())
29 }
30
31 #[inline]
32 fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
33 self
34 }
35
36 fn as_partial_reflect(&self) -> &dyn PartialReflect {
37 self
38 }
39
40 fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
41 self
42 }
43
44 fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
45 Ok(self)
46 }
47
48 fn try_as_reflect(&self) -> Option<&dyn Reflect> {
49 Some(self)
50 }
51
52 fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
53 Some(self)
54 }
55
56 fn reflect_kind(&self) -> ReflectKind {
57 ReflectKind::Opaque
58 }
59
60 fn reflect_ref(&self) -> ReflectRef<'_> {
61 ReflectRef::Opaque(self)
62 }
63
64 fn reflect_mut(&mut self) -> ReflectMut<'_> {
65 ReflectMut::Opaque(self)
66 }
67
68 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
69 ReflectOwned::Opaque(self)
70 }
71
72 fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
73 Ok(Box::new(*self))
74 }
75
76 fn reflect_hash(&self) -> Option<u64> {
77 let mut hasher = reflect_hasher();
78 Hash::hash(&Any::type_id(self), &mut hasher);
79 Hash::hash(self, &mut hasher);
80 Some(hasher.finish())
81 }
82
83 fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
84 if let Some(value) = value.try_downcast_ref::<Self>() {
85 Some(PartialEq::eq(self, value))
86 } else {
87 Some(false)
88 }
89 }
90
91 fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
92 if let Some(value) = value.try_downcast_ref::<Self>() {
93 self.clone_from(value);
94 Ok(())
95 } else {
96 Err(ApplyError::MismatchedTypes {
97 from_type: value.reflect_type_path().into(),
98 to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),
99 })
100 }
101 }
102}
103
104impl Reflect for &'static Location<'static> {
105 fn into_any(self: Box<Self>) -> Box<dyn Any> {
106 self
107 }
108
109 fn as_any(&self) -> &dyn Any {
110 self
111 }
112
113 fn as_any_mut(&mut self) -> &mut dyn Any {
114 self
115 }
116
117 fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
118 self
119 }
120
121 fn as_reflect(&self) -> &dyn Reflect {
122 self
123 }
124
125 fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
126 self
127 }
128
129 fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
130 *self = value.take()?;
131 Ok(())
132 }
133}
134
135impl Typed for &'static Location<'static> {
136 fn type_info() -> &'static TypeInfo {
137 static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
138 CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
139 }
140}
141
142impl GetTypeRegistration for &'static Location<'static> {
143 fn get_type_registration() -> TypeRegistration {
144 let mut registration = TypeRegistration::of::<Self>();
145 registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
146 registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
147 registration
148 }
149}
150
151impl FromReflect for &'static Location<'static> {
152 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
153 reflect.try_downcast_ref::<Self>().copied()
154 }
155}