1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//! Rust trait support for Flecs ECS.
//!
//! This module provides macro support for registering Rust traits as Flecs components,
//! enabling dynamic dispatch through the ECS system. This is particularly useful when you
//! want to query components by trait implementation rather than concrete types.
//!
//! # Overview
//!
//! The `ecs_rust_trait!` macro generates a component wrapper around a Rust trait, allowing:
//! - Registration of vtables for each trait implementor
//! - Dynamic casting from entities to trait objects
//! - Querying entities that implement a specific trait
//!
//! # How it works
//!
//! 1. The macro generates a `<TraitName>Trait` component struct that stores vtable information
//! 2. Each concrete type implementing the trait registers its vtable with `register_vtable::<T>()`
//! 3. Components can be queried using the generated trait component
//! 4. Retrieved components can be cast to trait objects using `cast()` or `cast_mut()`
//!
//! # Usage Pattern
//!
//! 1. Define your trait
//! 2. Call `ecs_rust_trait!` with the trait name
//! 3. Implement the trait for your component types
//! 4. Register vtables for each implementor type
//! 5. Query components that implement the trait using the generated trait component
//! 6. Cast components to trait objects as needed
//!
//! # Example
//!
//! ```ignore
//! use flecs_ecs::prelude::*;
//!
//! // 1. Define a trait
//! pub trait Shapes {
//! fn calculate(&self) -> u64;
//! }
//!
//! // 2. Generate the trait component
//! ecs_rust_trait!(Shapes);
//!
//! // 3. Implement the trait for component types
//! #[derive(Component)]
//! pub struct Circle {
//! radius: f32,
//! }
//!
//! impl Shapes for Circle {
//! fn calculate(&self) -> u64 {
//! 1
//! }
//! }
//!
//! #[derive(Component)]
//! pub struct Square {
//! side: f32,
//! }
//!
//! impl Shapes for Square {
//! fn calculate(&self) -> u64 {
//! 2
//! }
//! }
//!
//! #[derive(Component)]
//! pub struct Triangle {
//! side: f32,
//! }
//!
//! impl Shapes for Triangle {
//! fn calculate(&self) -> u64 {
//! 3
//! }
//! }
//!
//! let world = World::new();
//!
//! // 4. Register the vtable per type that implements the trait
//! ShapesTrait::register_vtable::<Circle>(&world);
//! ShapesTrait::register_vtable::<Square>(&world);
//! ShapesTrait::register_vtable::<Triangle>(&world);
//!
//! // Create entities with the components
//! world.entity_named("circle").set(Circle { radius: 5.0 });
//! world.entity_named("square").set(Square { side: 5.0 });
//! world.entity_named("triangle").set(Triangle { side: 5.0 });
//!
//! // 5. Query entities that implement the trait
//! let query = world.query::<()>().with(ShapesTrait::id()).build();
//!
//! query.run(|mut it| {
//! while it.next() {
//! let world = it.world();
//! for i in it.iter() {
//! let e = it.get_entity(i).unwrap();
//! let id = it.id(0);
//! // 6. Cast to trait object and use it
//! let shape = ShapesTrait::cast(e, id);
//! let calc = shape.calculate();
//! println!("{} - calc: {}", e.name(), calc);
//! }
//! }
//! });
//!
//! // Output:
//! // circle - calc: 1
//! // square - calc: 2
//! // triangle - calc: 3
//! ```
//!
//! # Feature flag
//!
//! This module is only available when the `flecs_query_rust_traits` feature is enabled.
use TokenStream;
use ;
use Ident;
/// Expansion function for the `ecs_rust_trait` macro.
///
/// This function is called by the `ecs_rust_trait` proc macro in `lib.rs`.
/// It generates a component struct and implementation methods for trait object handling.
///
/// For a trait named `MyTrait`, this generates:
/// - `MyTraitTrait` struct: A component that stores vtable information
/// - `MyTraitTrait::new(vtable: usize)`: Constructor (typically not used directly)
/// - `MyTraitTrait::register_vtable::<T>(world)`: Registers the vtable for type `T`
/// - `MyTraitTrait::cast(entity, id)`: Casts entity data to `&dyn MyTrait`
/// - `MyTraitTrait::cast_mut(entity, id)`: Casts entity data to `&mut dyn MyTrait`
///
/// # Arguments
///
/// * `name` - The identifier of the trait to wrap
///
/// # Returns
///
/// A `TokenStream` containing the generated code for the trait component.
///
/// # Generated Code Structure
///
/// The generated code includes:
/// 1. A component struct with a `vtable` field
/// 2. Implementation of the `RustTrait` marker trait
/// 3. Methods for vtable registration and trait object casting
pub