Skip to main content

bevy_reflect/impls/core/
panic.rs

1use 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 reflect_partial_cmp(&self, value: &dyn PartialReflect) -> Option<core::cmp::Ordering> {
92        if let Some(value) = value.try_downcast_ref::<Self>() {
93            PartialOrd::partial_cmp(self, value)
94        } else {
95            None
96        }
97    }
98
99    fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
100        if let Some(value) = value.try_downcast_ref::<Self>() {
101            self.clone_from(value);
102            Ok(())
103        } else {
104            Err(ApplyError::MismatchedTypes {
105                from_type: value.reflect_type_path().into(),
106                to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),
107            })
108        }
109    }
110}
111
112impl Reflect for &'static Location<'static> {
113    fn into_any(self: Box<Self>) -> Box<dyn Any> {
114        self
115    }
116
117    fn as_any(&self) -> &dyn Any {
118        self
119    }
120
121    fn as_any_mut(&mut self) -> &mut dyn Any {
122        self
123    }
124
125    fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
126        self
127    }
128
129    fn as_reflect(&self) -> &dyn Reflect {
130        self
131    }
132
133    fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
134        self
135    }
136
137    fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
138        *self = value.take()?;
139        Ok(())
140    }
141}
142
143impl Typed for &'static Location<'static> {
144    fn type_info() -> &'static TypeInfo {
145        static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
146        CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
147    }
148}
149
150impl GetTypeRegistration for &'static Location<'static> {
151    fn get_type_registration() -> TypeRegistration {
152        let mut registration = TypeRegistration::of::<Self>();
153        registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
154        registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
155        registration
156    }
157}
158
159impl FromReflect for &'static Location<'static> {
160    fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
161        reflect.try_downcast_ref::<Self>().copied()
162    }
163}