Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_reflect/src/impls/macros/list.rs
6600 views
1
macro_rules! impl_reflect_for_veclike {
2
($ty:ty, $insert:expr, $remove:expr, $push:expr, $pop:expr, $sub:ty) => {
3
const _: () = {
4
impl<T: $crate::from_reflect::FromReflect + $crate::type_info::MaybeTyped + $crate::type_path::TypePath + $crate::type_registry::GetTypeRegistration> $crate::list::List for $ty {
5
#[inline]
6
fn get(&self, index: usize) -> Option<&dyn $crate::reflect::PartialReflect> {
7
<$sub>::get(self, index).map(|value| value as &dyn $crate::reflect::PartialReflect)
8
}
9
10
#[inline]
11
fn get_mut(&mut self, index: usize) -> Option<&mut dyn $crate::reflect::PartialReflect> {
12
<$sub>::get_mut(self, index).map(|value| value as &mut dyn $crate::reflect::PartialReflect)
13
}
14
15
fn insert(&mut self, index: usize, value: bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>) {
16
let value = value.try_take::<T>().unwrap_or_else(|value| {
17
T::from_reflect(&*value).unwrap_or_else(|| {
18
panic!(
19
"Attempted to insert invalid value of type {}.",
20
value.reflect_type_path()
21
)
22
})
23
});
24
$insert(self, index, value);
25
}
26
27
fn remove(&mut self, index: usize) -> bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect> {
28
bevy_platform::prelude::Box::new($remove(self, index))
29
}
30
31
fn push(&mut self, value: bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>) {
32
let value = T::take_from_reflect(value).unwrap_or_else(|value| {
33
panic!(
34
"Attempted to push invalid value of type {}.",
35
value.reflect_type_path()
36
)
37
});
38
$push(self, value);
39
}
40
41
fn pop(&mut self) -> Option<bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>> {
42
$pop(self).map(|value| bevy_platform::prelude::Box::new(value) as bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>)
43
}
44
45
#[inline]
46
fn len(&self) -> usize {
47
<$sub>::len(self)
48
}
49
50
#[inline]
51
fn iter(&self) -> $crate::list::ListIter<'_> {
52
$crate::list::ListIter::new(self)
53
}
54
55
#[inline]
56
fn drain(&mut self) -> alloc::vec::Vec<bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>> {
57
self.drain(..)
58
.map(|value| bevy_platform::prelude::Box::new(value) as bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>)
59
.collect()
60
}
61
}
62
63
impl<T: $crate::from_reflect::FromReflect + $crate::type_info::MaybeTyped + $crate::type_path::TypePath + $crate::type_registry::GetTypeRegistration> $crate::reflect::PartialReflect for $ty {
64
#[inline]
65
fn get_represented_type_info(&self) -> Option<&'static $crate::type_info::TypeInfo> {
66
Some(<Self as $crate::type_info::Typed>::type_info())
67
}
68
69
fn into_partial_reflect(self: bevy_platform::prelude::Box<Self>) -> bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect> {
70
self
71
}
72
73
#[inline]
74
fn as_partial_reflect(&self) -> &dyn $crate::reflect::PartialReflect {
75
self
76
}
77
78
#[inline]
79
fn as_partial_reflect_mut(&mut self) -> &mut dyn $crate::reflect::PartialReflect {
80
self
81
}
82
83
fn try_into_reflect(
84
self: bevy_platform::prelude::Box<Self>,
85
) -> Result<bevy_platform::prelude::Box<dyn $crate::reflect::Reflect>, bevy_platform::prelude::Box<dyn $crate::reflect::PartialReflect>> {
86
Ok(self)
87
}
88
89
fn try_as_reflect(&self) -> Option<&dyn $crate::reflect::Reflect> {
90
Some(self)
91
}
92
93
fn try_as_reflect_mut(&mut self) -> Option<&mut dyn $crate::reflect::Reflect> {
94
Some(self)
95
}
96
97
fn reflect_kind(&self) -> $crate::kind::ReflectKind {
98
$crate::kind::ReflectKind::List
99
}
100
101
fn reflect_ref(&self) -> $crate::kind::ReflectRef<'_> {
102
$crate::kind::ReflectRef::List(self)
103
}
104
105
fn reflect_mut(&mut self) -> $crate::kind::ReflectMut<'_> {
106
$crate::kind::ReflectMut::List(self)
107
}
108
109
fn reflect_owned(self: bevy_platform::prelude::Box<Self>) -> $crate::kind::ReflectOwned {
110
$crate::kind::ReflectOwned::List(self)
111
}
112
113
fn reflect_clone(&self) -> Result<bevy_platform::prelude::Box<dyn $crate::reflect::Reflect>, $crate::error::ReflectCloneError> {
114
Ok(bevy_platform::prelude::Box::new(
115
self.iter()
116
.map(|value| value.reflect_clone_and_take())
117
.collect::<Result<Self, $crate::error::ReflectCloneError>>()?,
118
))
119
}
120
121
fn reflect_hash(&self) -> Option<u64> {
122
$crate::list::list_hash(self)
123
}
124
125
fn reflect_partial_eq(&self, value: &dyn $crate::reflect::PartialReflect) -> Option<bool> {
126
$crate::list::list_partial_eq(self, value)
127
}
128
129
fn apply(&mut self, value: &dyn $crate::reflect::PartialReflect) {
130
$crate::list::list_apply(self, value);
131
}
132
133
fn try_apply(&mut self, value: &dyn $crate::reflect::PartialReflect) -> Result<(), $crate::reflect::ApplyError> {
134
$crate::list::list_try_apply(self, value)
135
}
136
}
137
138
$crate::impl_full_reflect!(<T> for $ty where T: $crate::from_reflect::FromReflect + $crate::type_info::MaybeTyped + $crate::type_path::TypePath + $crate::type_registry::GetTypeRegistration);
139
140
impl<T: $crate::from_reflect::FromReflect + $crate::type_info::MaybeTyped + $crate::type_path::TypePath + $crate::type_registry::GetTypeRegistration> $crate::type_info::Typed for $ty {
141
fn type_info() -> &'static $crate::type_info::TypeInfo {
142
static CELL: $crate::utility::GenericTypeInfoCell = $crate::utility::GenericTypeInfoCell::new();
143
CELL.get_or_insert::<Self, _>(|| {
144
$crate::type_info::TypeInfo::List(
145
$crate::list::ListInfo::new::<Self, T>().with_generics($crate::generics::Generics::from_iter([
146
$crate::generics::TypeParamInfo::new::<T>("T")
147
]))
148
)
149
})
150
}
151
}
152
153
impl<T: $crate::from_reflect::FromReflect + $crate::type_info::MaybeTyped + $crate::type_path::TypePath + $crate::type_registry::GetTypeRegistration> $crate::type_registry::GetTypeRegistration
154
for $ty
155
{
156
fn get_type_registration() -> $crate::type_registry::TypeRegistration {
157
let mut registration = $crate::type_registry::TypeRegistration::of::<$ty>();
158
registration.insert::<$crate::type_registry::ReflectFromPtr>($crate::type_registry::FromType::<$ty>::from_type());
159
registration.insert::<$crate::from_reflect::ReflectFromReflect>($crate::type_registry::FromType::<$ty>::from_type());
160
registration
161
}
162
163
fn register_type_dependencies(registry: &mut $crate::type_registry::TypeRegistry) {
164
registry.register::<T>();
165
}
166
}
167
168
impl<T: $crate::from_reflect::FromReflect + $crate::type_info::MaybeTyped + $crate::type_path::TypePath + $crate::type_registry::GetTypeRegistration> $crate::from_reflect::FromReflect for $ty {
169
fn from_reflect(reflect: &dyn $crate::reflect::PartialReflect) -> Option<Self> {
170
let ref_list = reflect.reflect_ref().as_list().ok()?;
171
172
let mut new_list = Self::with_capacity(ref_list.len());
173
174
for field in ref_list.iter() {
175
$push(&mut new_list, T::from_reflect(field)?);
176
}
177
178
Some(new_list)
179
}
180
}
181
};
182
};
183
}
184
185
pub(crate) use impl_reflect_for_veclike;
186
187