forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split event.rs into a full module. (bevyengine#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.
- Loading branch information
Showing
9 changed files
with
1,596 additions
and
1,544 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#[cfg(feature = "bevy_reflect")] | ||
use bevy_reflect::Reflect; | ||
use std::{ | ||
cmp::Ordering, | ||
fmt, | ||
hash::{Hash, Hasher}, | ||
marker::PhantomData, | ||
}; | ||
|
||
/// A type that can be stored in an [`Events<E>`] resource | ||
/// You can conveniently access events using the [`EventReader`] and [`EventWriter`] system parameter. | ||
/// | ||
/// Events must be thread-safe. | ||
#[diagnostic::on_unimplemented( | ||
message = "`{Self}` is not an `Event`", | ||
label = "invalid `Event`", | ||
note = "consider annotating `{Self}` with `#[derive(Event)]`" | ||
)] | ||
pub trait Event: Send + Sync + 'static {} | ||
|
||
/// An `EventId` uniquely identifies an event stored in a specific [`World`]. | ||
/// | ||
/// An `EventId` can among other things be used to trace the flow of an event from the point it was | ||
/// sent to the point it was processed. `EventId`s increase monotonically by send order. | ||
/// | ||
/// [`World`]: crate::world::World | ||
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))] | ||
pub struct EventId<E: Event> { | ||
/// Uniquely identifies the event associated with this ID. | ||
// This value corresponds to the order in which each event was added to the world. | ||
pub id: usize, | ||
#[cfg_attr(feature = "bevy_reflect", reflect(ignore))] | ||
pub(super) _marker: PhantomData<E>, | ||
} | ||
|
||
impl<E: Event> Copy for EventId<E> {} | ||
|
||
impl<E: Event> Clone for EventId<E> { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} | ||
|
||
impl<E: Event> fmt::Display for EventId<E> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
<Self as fmt::Debug>::fmt(self, f) | ||
} | ||
} | ||
|
||
impl<E: Event> fmt::Debug for EventId<E> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!( | ||
f, | ||
"event<{}>#{}", | ||
std::any::type_name::<E>().split("::").last().unwrap(), | ||
self.id, | ||
) | ||
} | ||
} | ||
|
||
impl<E: Event> PartialEq for EventId<E> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.id == other.id | ||
} | ||
} | ||
|
||
impl<E: Event> Eq for EventId<E> {} | ||
|
||
impl<E: Event> PartialOrd for EventId<E> { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl<E: Event> Ord for EventId<E> { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
self.id.cmp(&other.id) | ||
} | ||
} | ||
|
||
impl<E: Event> Hash for EventId<E> { | ||
fn hash<H: Hasher>(&self, state: &mut H) { | ||
Hash::hash(&self.id, state); | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))] | ||
pub(crate) struct EventInstance<E: Event> { | ||
pub event_id: EventId<E>, | ||
pub event: E, | ||
} |
Oops, something went wrong.