bevy/benches/benches/bevy_ecs/events/send.rs
CatThingy 89cbc78d3d
Require #[derive(Event)] on all Events (#7086)
# Objective

Be consistent with `Resource`s and `Components` and have `Event` types
be more self-documenting.
Although not susceptible to accidentally using a function instead of a
value due to `Event`s only being initialized by their type, much of the
same reasoning for removing the blanket impl on `Resource` also applies
here.

* Not immediately obvious if a type is intended to be an event
* Prevent invisible conflicts if the same third-party or primitive types
are used as events
* Allows for further extensions (e.g. opt-in warning for missed events)

## Solution

Remove the blanket impl for the `Event` trait. Add a derive macro for
it.

---

## Changelog

- `Event` is no longer implemented for all applicable types. Add the
`#[derive(Event)]` macro for events.

## Migration Guide

* Add the `#[derive(Event)]` macro for events. Third-party types used as
events should be wrapped in a newtype.
2023-06-06 14:44:32 +00:00

40 lines
904 B
Rust

use bevy_ecs::prelude::*;
#[derive(Event)]
struct BenchEvent<const SIZE: usize>([u8; SIZE]);
impl<const SIZE: usize> Default for BenchEvent<SIZE> {
fn default() -> Self {
BenchEvent([0; SIZE])
}
}
pub struct Benchmark<const SIZE: usize> {
events: Events<BenchEvent<SIZE>>,
count: usize,
}
impl<const SIZE: usize> Benchmark<SIZE> {
pub fn new(count: usize) -> Self {
let mut events = Events::default();
// Force both internal buffers to be allocated.
for _ in 0..2 {
for _ in 0..count {
events.send(BenchEvent([0u8; SIZE]));
}
events.update();
}
Self { events, count }
}
pub fn run(&mut self) {
for _ in 0..self.count {
self.events
.send(std::hint::black_box(BenchEvent([0u8; SIZE])));
}
self.events.update();
}
}