Skip to main content

bevy_reflect/impls/std/
path.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::{
9        FromType, GetTypeRegistration, ReflectDeserialize, ReflectFromPtr, ReflectSerialize,
10        TypeRegistration,
11    },
12    utility::{reflect_hasher, NonGenericTypeInfoCell},
13};
14use alloc::borrow::Cow;
15use bevy_platform::prelude::*;
16use bevy_reflect_derive::{impl_reflect_opaque, impl_type_path};
17use core::any::Any;
18use core::fmt;
19use core::hash::{Hash, Hasher};
20use std::path::Path;
21
22impl_reflect_opaque!(::std::path::PathBuf(
23    Clone,
24    Debug,
25    Hash,
26    PartialEq,
27    PartialOrd,
28    Serialize,
29    Deserialize,
30    Default
31));
32
33impl PartialReflect for &'static Path {
34    fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
35        Some(<Self as Typed>::type_info())
36    }
37
38    #[inline]
39    fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
40        self
41    }
42
43    fn as_partial_reflect(&self) -> &dyn PartialReflect {
44        self
45    }
46
47    fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
48        self
49    }
50
51    fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
52        Ok(self)
53    }
54
55    fn try_as_reflect(&self) -> Option<&dyn Reflect> {
56        Some(self)
57    }
58
59    fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
60        Some(self)
61    }
62
63    fn reflect_kind(&self) -> ReflectKind {
64        ReflectKind::Opaque
65    }
66
67    fn reflect_ref(&self) -> ReflectRef<'_> {
68        ReflectRef::Opaque(self)
69    }
70
71    fn reflect_mut(&mut self) -> ReflectMut<'_> {
72        ReflectMut::Opaque(self)
73    }
74
75    fn reflect_owned(self: Box<Self>) -> ReflectOwned {
76        ReflectOwned::Opaque(self)
77    }
78
79    fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
80        Ok(Box::new(*self))
81    }
82
83    fn reflect_hash(&self) -> Option<u64> {
84        let mut hasher = reflect_hasher();
85        Hash::hash(&Any::type_id(self), &mut hasher);
86        Hash::hash(self, &mut hasher);
87        Some(hasher.finish())
88    }
89
90    fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
91        if let Some(value) = value.try_downcast_ref::<Self>() {
92            Some(PartialEq::eq(self, value))
93        } else {
94            Some(false)
95        }
96    }
97
98    fn reflect_partial_cmp(&self, value: &dyn PartialReflect) -> Option<core::cmp::Ordering> {
99        if let Some(value) = value.try_downcast_ref::<Self>() {
100            PartialOrd::partial_cmp(self, value)
101        } else {
102            None
103        }
104    }
105
106    fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
107        if let Some(value) = value.try_downcast_ref::<Self>() {
108            self.clone_from(value);
109            Ok(())
110        } else {
111            Err(ApplyError::MismatchedTypes {
112                from_type: value.reflect_type_path().into(),
113                to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),
114            })
115        }
116    }
117}
118
119impl Reflect for &'static Path {
120    fn into_any(self: Box<Self>) -> Box<dyn Any> {
121        self
122    }
123
124    fn as_any(&self) -> &dyn Any {
125        self
126    }
127
128    fn as_any_mut(&mut self) -> &mut dyn Any {
129        self
130    }
131
132    fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
133        self
134    }
135
136    fn as_reflect(&self) -> &dyn Reflect {
137        self
138    }
139
140    fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
141        self
142    }
143
144    fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
145        *self = value.take()?;
146        Ok(())
147    }
148}
149
150impl Typed for &'static Path {
151    fn type_info() -> &'static TypeInfo {
152        static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
153        CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
154    }
155}
156
157impl GetTypeRegistration for &'static Path {
158    fn get_type_registration() -> TypeRegistration {
159        let mut registration = TypeRegistration::of::<Self>();
160        registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
161        registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
162        registration
163    }
164}
165
166impl FromReflect for &'static Path {
167    fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
168        reflect.try_downcast_ref::<Self>().copied()
169    }
170}
171
172impl PartialReflect for Cow<'static, Path> {
173    fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
174        Some(<Self as Typed>::type_info())
175    }
176
177    #[inline]
178    fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
179        self
180    }
181
182    fn as_partial_reflect(&self) -> &dyn PartialReflect {
183        self
184    }
185
186    fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
187        self
188    }
189
190    fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
191        Ok(self)
192    }
193
194    fn try_as_reflect(&self) -> Option<&dyn Reflect> {
195        Some(self)
196    }
197
198    fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
199        Some(self)
200    }
201
202    fn reflect_kind(&self) -> ReflectKind {
203        ReflectKind::Opaque
204    }
205
206    fn reflect_ref(&self) -> ReflectRef<'_> {
207        ReflectRef::Opaque(self)
208    }
209
210    fn reflect_mut(&mut self) -> ReflectMut<'_> {
211        ReflectMut::Opaque(self)
212    }
213
214    fn reflect_owned(self: Box<Self>) -> ReflectOwned {
215        ReflectOwned::Opaque(self)
216    }
217
218    fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
219        Ok(Box::new(self.clone()))
220    }
221
222    fn reflect_hash(&self) -> Option<u64> {
223        let mut hasher = reflect_hasher();
224        Hash::hash(&Any::type_id(self), &mut hasher);
225        Hash::hash(self, &mut hasher);
226        Some(hasher.finish())
227    }
228
229    fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
230        if let Some(value) = value.try_downcast_ref::<Self>() {
231            Some(PartialEq::eq(self, value))
232        } else {
233            Some(false)
234        }
235    }
236
237    fn reflect_partial_cmp(&self, value: &dyn PartialReflect) -> Option<core::cmp::Ordering> {
238        if let Some(value) = value.try_downcast_ref::<Self>() {
239            PartialOrd::partial_cmp(self, value)
240        } else {
241            None
242        }
243    }
244
245    fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
246        fmt::Debug::fmt(&self, f)
247    }
248
249    fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
250        if let Some(value) = value.try_downcast_ref::<Self>() {
251            self.clone_from(value);
252            Ok(())
253        } else {
254            Err(ApplyError::MismatchedTypes {
255                from_type: value.reflect_type_path().into(),
256                to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),
257            })
258        }
259    }
260}
261
262impl Reflect for Cow<'static, Path> {
263    fn into_any(self: Box<Self>) -> Box<dyn Any> {
264        self
265    }
266
267    fn as_any(&self) -> &dyn Any {
268        self
269    }
270
271    fn as_any_mut(&mut self) -> &mut dyn Any {
272        self
273    }
274
275    fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
276        self
277    }
278
279    fn as_reflect(&self) -> &dyn Reflect {
280        self
281    }
282
283    fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
284        self
285    }
286
287    fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
288        *self = value.take()?;
289        Ok(())
290    }
291}
292
293impl Typed for Cow<'static, Path> {
294    fn type_info() -> &'static TypeInfo {
295        static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
296        CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
297    }
298}
299
300impl_type_path!(::std::path::Path);
301
302impl FromReflect for Cow<'static, Path> {
303    fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
304        Some(reflect.try_downcast_ref::<Self>()?.clone())
305    }
306}
307
308impl GetTypeRegistration for Cow<'static, Path> {
309    fn get_type_registration() -> TypeRegistration {
310        let mut registration = TypeRegistration::of::<Self>();
311        registration.insert::<ReflectDeserialize>(FromType::<Self>::from_type());
312        registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
313        registration.insert::<ReflectSerialize>(FromType::<Self>::from_type());
314        registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
315        registration
316    }
317}
318
319#[cfg(feature = "functions")]
320crate::func::macros::impl_function_traits!(Cow<'static, Path>);