Skip to content

Commit 2ccdae7

Browse files
authored
Split event.rs into a full module. (#13801)
# Objective - Split the bevy_ecs::events module so it's easier to work with ## Solution - Split the event.rs file across multiple files, made sure all tests passed, and exports from the module were the same as previous ## Testing - All automated tests pass.
1 parent d803adf commit 2ccdae7

File tree

9 files changed

+1596
-1544
lines changed

9 files changed

+1596
-1544
lines changed

crates/bevy_ecs/src/event.rs

Lines changed: 0 additions & 1544 deletions
This file was deleted.

crates/bevy_ecs/src/event/base.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#[cfg(feature = "bevy_reflect")]
2+
use bevy_reflect::Reflect;
3+
use std::{
4+
cmp::Ordering,
5+
fmt,
6+
hash::{Hash, Hasher},
7+
marker::PhantomData,
8+
};
9+
10+
/// A type that can be stored in an [`Events<E>`] resource
11+
/// You can conveniently access events using the [`EventReader`] and [`EventWriter`] system parameter.
12+
///
13+
/// Events must be thread-safe.
14+
#[diagnostic::on_unimplemented(
15+
message = "`{Self}` is not an `Event`",
16+
label = "invalid `Event`",
17+
note = "consider annotating `{Self}` with `#[derive(Event)]`"
18+
)]
19+
pub trait Event: Send + Sync + 'static {}
20+
21+
/// An `EventId` uniquely identifies an event stored in a specific [`World`].
22+
///
23+
/// An `EventId` can among other things be used to trace the flow of an event from the point it was
24+
/// sent to the point it was processed. `EventId`s increase monotonically by send order.
25+
///
26+
/// [`World`]: crate::world::World
27+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
28+
pub struct EventId<E: Event> {
29+
/// Uniquely identifies the event associated with this ID.
30+
// This value corresponds to the order in which each event was added to the world.
31+
pub id: usize,
32+
#[cfg_attr(feature = "bevy_reflect", reflect(ignore))]
33+
pub(super) _marker: PhantomData<E>,
34+
}
35+
36+
impl<E: Event> Copy for EventId<E> {}
37+
38+
impl<E: Event> Clone for EventId<E> {
39+
fn clone(&self) -> Self {
40+
*self
41+
}
42+
}
43+
44+
impl<E: Event> fmt::Display for EventId<E> {
45+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46+
<Self as fmt::Debug>::fmt(self, f)
47+
}
48+
}
49+
50+
impl<E: Event> fmt::Debug for EventId<E> {
51+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
52+
write!(
53+
f,
54+
"event<{}>#{}",
55+
std::any::type_name::<E>().split("::").last().unwrap(),
56+
self.id,
57+
)
58+
}
59+
}
60+
61+
impl<E: Event> PartialEq for EventId<E> {
62+
fn eq(&self, other: &Self) -> bool {
63+
self.id == other.id
64+
}
65+
}
66+
67+
impl<E: Event> Eq for EventId<E> {}
68+
69+
impl<E: Event> PartialOrd for EventId<E> {
70+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
71+
Some(self.cmp(other))
72+
}
73+
}
74+
75+
impl<E: Event> Ord for EventId<E> {
76+
fn cmp(&self, other: &Self) -> Ordering {
77+
self.id.cmp(&other.id)
78+
}
79+
}
80+
81+
impl<E: Event> Hash for EventId<E> {
82+
fn hash<H: Hasher>(&self, state: &mut H) {
83+
Hash::hash(&self.id, state);
84+
}
85+
}
86+
87+
#[derive(Debug)]
88+
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
89+
pub(crate) struct EventInstance<E: Event> {
90+
pub event_id: EventId<E>,
91+
pub event: E,
92+
}

0 commit comments

Comments
 (0)