bevy_reflect/impls/core/
sync.rs1use crate::{
2 error::ReflectCloneError,
3 kind::{ReflectKind, ReflectMut, ReflectOwned, ReflectRef},
4 prelude::*,
5 reflect::{impl_full_reflect, ApplyError},
6 type_info::{OpaqueInfo, TypeInfo, Typed},
7 type_path::DynamicTypePath,
8 type_registry::{FromType, GetTypeRegistration, ReflectFromPtr, TypeRegistration},
9 utility::NonGenericTypeInfoCell,
10};
11use bevy_platform::prelude::*;
12use bevy_reflect_derive::impl_type_path;
13use core::fmt;
14
15macro_rules! impl_reflect_for_atomic {
16 ($ty:ty, $ordering:expr) => {
17 impl_type_path!($ty);
18
19 const _: () = {
20 #[cfg(feature = "functions")]
21 crate::func::macros::impl_function_traits!($ty);
22
23 impl GetTypeRegistration for $ty {
24 fn get_type_registration() -> TypeRegistration {
25 let mut registration = TypeRegistration::of::<Self>();
26 registration.insert::<ReflectFromPtr>(FromType::<Self>::from_type());
27 registration.insert::<ReflectFromReflect>(FromType::<Self>::from_type());
28 registration.insert::<ReflectDefault>(FromType::<Self>::from_type());
29
30 #[cfg(feature = "std")]
32 {
33 registration.insert::<crate::type_registry::ReflectSerialize>(FromType::<Self>::from_type());
34 registration.insert::<crate::type_registry::ReflectDeserialize>(FromType::<Self>::from_type());
35 }
36
37 registration
38 }
39 }
40
41 impl Typed for $ty {
42 fn type_info() -> &'static TypeInfo {
43 static CELL: NonGenericTypeInfoCell = NonGenericTypeInfoCell::new();
44 CELL.get_or_set(|| {
45 let info = OpaqueInfo::new::<Self>();
46 TypeInfo::Opaque(info)
47 })
48 }
49 }
50
51 impl PartialReflect for $ty {
52 #[inline]
53 fn get_represented_type_info(&self) -> Option<&'static TypeInfo> {
54 Some(<Self as Typed>::type_info())
55 }
56 #[inline]
57 fn into_partial_reflect(self: Box<Self>) -> Box<dyn PartialReflect> {
58 self
59 }
60 #[inline]
61 fn as_partial_reflect(&self) -> &dyn PartialReflect {
62 self
63 }
64 #[inline]
65 fn as_partial_reflect_mut(&mut self) -> &mut dyn PartialReflect {
66 self
67 }
68 #[inline]
69 fn try_into_reflect(
70 self: Box<Self>,
71 ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>> {
72 Ok(self)
73 }
74 #[inline]
75 fn try_as_reflect(&self) -> Option<&dyn Reflect> {
76 Some(self)
77 }
78 #[inline]
79 fn try_as_reflect_mut(&mut self) -> Option<&mut dyn Reflect> {
80 Some(self)
81 }
82
83 #[inline]
84 fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError> {
85 Ok(Box::new(<$ty>::new(self.load($ordering))))
86 }
87
88 #[inline]
89 fn try_apply(&mut self, value: &dyn PartialReflect) -> Result<(), ApplyError> {
90 if let Some(value) = value.try_downcast_ref::<Self>() {
91 *self = <$ty>::new(value.load($ordering));
92 } else {
93 return Err(ApplyError::MismatchedTypes {
94 from_type: Into::into(DynamicTypePath::reflect_type_path(value)),
95 to_type: Into::into(<Self as TypePath>::type_path()),
96 });
97 }
98 Ok(())
99 }
100 #[inline]
101 fn reflect_kind(&self) -> ReflectKind {
102 ReflectKind::Opaque
103 }
104 #[inline]
105 fn reflect_ref(&self) -> ReflectRef<'_> {
106 ReflectRef::Opaque(self)
107 }
108 #[inline]
109 fn reflect_mut(&mut self) -> ReflectMut<'_> {
110 ReflectMut::Opaque(self)
111 }
112 #[inline]
113 fn reflect_owned(self: Box<Self>) -> ReflectOwned {
114 ReflectOwned::Opaque(self)
115 }
116 fn debug(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 fmt::Debug::fmt(self, f)
118 }
119 }
120
121 impl FromReflect for $ty {
122 fn from_reflect(reflect: &dyn PartialReflect) -> Option<Self> {
123 Some(<$ty>::new(
124 reflect.try_downcast_ref::<$ty>()?.load($ordering),
125 ))
126 }
127 }
128 };
129
130 impl_full_reflect!(for $ty);
131 };
132}
133
134impl_reflect_for_atomic!(
135 ::core::sync::atomic::AtomicIsize,
136 ::core::sync::atomic::Ordering::SeqCst
137);
138impl_reflect_for_atomic!(
139 ::core::sync::atomic::AtomicUsize,
140 ::core::sync::atomic::Ordering::SeqCst
141);
142#[cfg(target_has_atomic = "64")]
143impl_reflect_for_atomic!(
144 ::core::sync::atomic::AtomicI64,
145 ::core::sync::atomic::Ordering::SeqCst
146);
147#[cfg(target_has_atomic = "64")]
148impl_reflect_for_atomic!(
149 ::core::sync::atomic::AtomicU64,
150 ::core::sync::atomic::Ordering::SeqCst
151);
152impl_reflect_for_atomic!(
153 ::core::sync::atomic::AtomicI32,
154 ::core::sync::atomic::Ordering::SeqCst
155);
156impl_reflect_for_atomic!(
157 ::core::sync::atomic::AtomicU32,
158 ::core::sync::atomic::Ordering::SeqCst
159);
160impl_reflect_for_atomic!(
161 ::core::sync::atomic::AtomicI16,
162 ::core::sync::atomic::Ordering::SeqCst
163);
164impl_reflect_for_atomic!(
165 ::core::sync::atomic::AtomicU16,
166 ::core::sync::atomic::Ordering::SeqCst
167);
168impl_reflect_for_atomic!(
169 ::core::sync::atomic::AtomicI8,
170 ::core::sync::atomic::Ordering::SeqCst
171);
172impl_reflect_for_atomic!(
173 ::core::sync::atomic::AtomicU8,
174 ::core::sync::atomic::Ordering::SeqCst
175);
176impl_reflect_for_atomic!(
177 ::core::sync::atomic::AtomicBool,
178 ::core::sync::atomic::Ordering::SeqCst
179);