bevy-trait-query
An implementation of trait queries for the bevy game engine.
Before using this crate, you should be familiar with bevy: https://bevyengine.org/.
Bevy Version | Crate Version |
---|---|
0.12 | 0.4 |
0.11 | 0.3 |
0.10 | 0.2 |
0.9 | 0.1 |
0.8 | 0.0.3 |
Preview | Main branch |
Note on reliability
While this crate has seen some use in the world with no issues yet, it is still quite new and experimental. Use with caution (and miri!).
If you find a bug, please open an issue.
Overview
Let's say you have a trait that you want to implement for some of your components.
/// Components that display a message when hovered.
In order to be useful within bevy, you'll want to be able to query for this trait.
// Just add this attribute...
// ...and now you can use your trait in queries.
Since Rust unfortunately lacks any kind of reflection, it is necessary to register each component with the trait when the app gets built.
;
;
/* ...trait implementations omitted for brevity... */
;
Unlike queries for concrete types, it's possible for an entity to have multiple components that match a trait query.
Alternatively, if you expect to only have component implementing the trait for each entity,
you can use the filter One
. This has significantly better performance than iterating
over all trait impls.
use One;
Performance
The performance of trait queries is quite competitive. Here are some benchmarks for simple cases:
Concrete type | One | All | |
---|---|---|---|
1 match | 16.135 µs | 31.441 µs | 63.273 µs |
2 matches | 17.501 µs | - | 102.83 µs |
1-2 matches | - | 16.959 µs | 82.179 µs |
License
MIT or APACHE-2.0