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    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 try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
98        if let Some(value) = value.try_downcast_ref::<Self>() {
99            self.clone_from(value);
100            Ok(())
101        } else {
102            Err(ApplyError::MismatchedTypes {
103                from_type: value.reflect_type_path().into(),
104                to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),
105            })
106        }
107    }
108}
109
110impl Reflect for &'static Path {
111    fn into_any(self: Box<Self>) -> Box<dyn Any> {
112        self
113    }
114
115    fn as_any(&self) -> &dyn Any {
116        self
117    }
118
119    fn as_any_mut(&mut self) -> &mut dyn Any {
120        self
121    }
122
123    fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
124        self
125    }
126
127    fn as_reflect(&self) -> &dyn Reflect {
128        self
129    }
130
131    fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
132        self
133    }
134
135    fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
136        *self = value.take()?;
137        Ok(())
138    }
139}
140
141impl Typed for &'static Path {
142    fn type_info() -> &'static TypeInfo {
143        static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
144        CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
145    }
146}
147
148impl GetTypeRegistration for &'static Path {
149    fn get_type_registration() -> TypeRegistration {
150        let mut registration = TypeRegistration::of::<Self>();
151        registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
152        registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
153        registration
154    }
155}
156
157impl FromReflect for &'static Path {
158    fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
159        reflect.try_downcast_ref::<Self>().copied()
160    }
161}
162
163impl PartialReflect for Cow<'static, Path> {
164    fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
165        Some(<Self as Typed>::type_info())
166    }
167
168    #[inline]
169    fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
170        self
171    }
172
173    fn as_partial_reflect(&self) -> &dyn PartialReflect {
174        self
175    }
176
177    fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
178        self
179    }
180
181    fn try_into_reflect(self: Box<Self>) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
182        Ok(self)
183    }
184
185    fn try_as_reflect(&self) -> Option<&dyn Reflect> {
186        Some(self)
187    }
188
189    fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
190        Some(self)
191    }
192
193    fn reflect_kind(&self) -> ReflectKind {
194        ReflectKind::Opaque
195    }
196
197    fn reflect_ref(&self) -> ReflectRef<'_> {
198        ReflectRef::Opaque(self)
199    }
200
201    fn reflect_mut(&mut self) -> ReflectMut<'_> {
202        ReflectMut::Opaque(self)
203    }
204
205    fn reflect_owned(self: Box<Self>) -> ReflectOwned {
206        ReflectOwned::Opaque(self)
207    }
208
209    fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
210        Ok(Box::new(self.clone()))
211    }
212
213    fn reflect_hash(&self) -> Option<u64> {
214        let mut hasher = reflect_hasher();
215        Hash::hash(&Any::type_id(self), &mut hasher);
216        Hash::hash(self, &mut hasher);
217        Some(hasher.finish())
218    }
219
220    fn reflect_partial_eq(&self, value: &dyn PartialReflect) -> Option<bool> {
221        if let Some(value) = value.try_downcast_ref::<Self>() {
222            Some(PartialEq::eq(self, value))
223        } else {
224            Some(false)
225        }
226    }
227
228    fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
229        fmt::Debug::fmt(&self, f)
230    }
231
232    fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
233        if let Some(value) = value.try_downcast_ref::<Self>() {
234            self.clone_from(value);
235            Ok(())
236        } else {
237            Err(ApplyError::MismatchedTypes {
238                from_type: value.reflect_type_path().into(),
239                to_type: <Self as DynamicTypePath>::reflect_type_path(self).into(),
240            })
241        }
242    }
243}
244
245impl Reflect for Cow<'static, Path> {
246    fn into_any(self: Box<Self>) -> Box<dyn Any> {
247        self
248    }
249
250    fn as_any(&self) -> &dyn Any {
251        self
252    }
253
254    fn as_any_mut(&mut self) -> &mut dyn Any {
255        self
256    }
257
258    fn into_reflect(self: Box<Self>) -> Box<dyn Reflect> {
259        self
260    }
261
262    fn as_reflect(&self) -> &dyn Reflect {
263        self
264    }
265
266    fn as_reflect_mut(&mut self) -> &mut dyn Reflect {
267        self
268    }
269
270    fn set(&mut self, value: Box<dyn Reflect>) -> Result<(), Box<dyn Reflect>> {
271        *self = value.take()?;
272        Ok(())
273    }
274}
275
276impl Typed for Cow<'static, Path> {
277    fn type_info() -> &'static TypeInfo {
278        static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
279        CELL.get_or_set(|| TypeInfo::Opaque(OpaqueInfo::new::<Self>()))
280    }
281}
282
283impl_type_path!(::std::path::Path);
284
285impl FromReflect for Cow<'static, Path> {
286    fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
287        Some(reflect.try_downcast_ref::<Self>()?.clone())
288    }
289}
290
291impl GetTypeRegistration for Cow<'static, Path> {
292    fn get_type_registration() -> TypeRegistration {
293        let mut registration = TypeRegistration::of::<Self>();
294        registration.insert::<ReflectDeserialize>(FromType::<Self>::from_type());
295        registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
296        registration.insert::<ReflectSerialize>(FromType::<Self>::from_type());
297        registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
298        registration
299    }
300}
301
302#[cfg(feature = "functions")]
303crate::func::macros::impl_function_traits!(Cow<'static, Path>);