Skip to main content

bevy_reflect/impls/std/
path.rs

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