-
-
Notifications
You must be signed in to change notification settings - Fork 4k
ObserverTrigger: Add wrapper type around ComponentId
#19755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5d97f7f
fa06283
5a6005c
ea87a3f
abc49da
84eb8df
5b7a30b
4ec47a1
227934e
5e3f691
aec5026
0addb40
2b12300
fcecaa0
eaad910
d292ed1
96e9810
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -94,23 +94,23 @@ use core::{ | |||||||||||||||||||||||||||||||
note = "consider annotating `{Self}` with `#[derive(Event)]`" | ||||||||||||||||||||||||||||||||
)] | ||||||||||||||||||||||||||||||||
pub trait Event: Send + Sync + 'static { | ||||||||||||||||||||||||||||||||
/// Generates the [`ComponentId`] for this event type. | ||||||||||||||||||||||||||||||||
/// Generates the [`EventKey`] for this event type. | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// If this type has already been registered, | ||||||||||||||||||||||||||||||||
/// this will return the existing [`ComponentId`]. | ||||||||||||||||||||||||||||||||
/// this will return the existing [`EventKey`]. | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// This is used by various dynamically typed observer APIs, | ||||||||||||||||||||||||||||||||
/// such as [`World::trigger_targets_dynamic`]. | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// # Warning | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// This method should not be overridden by implementers, | ||||||||||||||||||||||||||||||||
/// and should always correspond to the implementation of [`component_id`](Event::component_id). | ||||||||||||||||||||||||||||||||
fn register_component_id(world: &mut World) -> ComponentId { | ||||||||||||||||||||||||||||||||
world.register_component::<EventWrapperComponent<Self>>() | ||||||||||||||||||||||||||||||||
/// and should always correspond to the implementation of [`event_type`](Event::event_type). | ||||||||||||||||||||||||||||||||
fn register_event_type(world: &mut World) -> EventKey { | ||||||||||||||||||||||||||||||||
EventKey(world.register_component::<EventWrapperComponent<Self>>()) | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
/// Fetches the [`ComponentId`] for this event type, | ||||||||||||||||||||||||||||||||
/// Fetches the [`EventKey`] for this event type, | ||||||||||||||||||||||||||||||||
/// if it has already been generated. | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// This is used by various dynamically typed observer APIs, | ||||||||||||||||||||||||||||||||
|
@@ -119,9 +119,12 @@ pub trait Event: Send + Sync + 'static { | |||||||||||||||||||||||||||||||
/// # Warning | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// This method should not be overridden by implementers, | ||||||||||||||||||||||||||||||||
/// and should always correspond to the implementation of [`register_component_id`](Event::register_component_id). | ||||||||||||||||||||||||||||||||
fn component_id(world: &World) -> Option<ComponentId> { | ||||||||||||||||||||||||||||||||
world.component_id::<EventWrapperComponent<Self>>() | ||||||||||||||||||||||||||||||||
/// and should always correspond to the implementation of | ||||||||||||||||||||||||||||||||
/// [`register_event_type`](Event::register_event_type). | ||||||||||||||||||||||||||||||||
fn event_type(world: &World) -> Option<EventKey> { | ||||||||||||||||||||||||||||||||
world | ||||||||||||||||||||||||||||||||
.component_id::<EventWrapperComponent<Self>>() | ||||||||||||||||||||||||||||||||
.map(EventKey) | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
|
@@ -421,3 +424,28 @@ pub(crate) struct EventInstance<E: BufferedEvent> { | |||||||||||||||||||||||||||||||
pub event_id: EventId<E>, | ||||||||||||||||||||||||||||||||
pub event: E, | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
/// # Warning | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// This struct should only be instantiated internally to this crate. | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// [`EventKey`]s are created in: | ||||||||||||||||||||||||||||||||
/// - `crate::observer::trigger_dynamic_ref_with_caller` | ||||||||||||||||||||||||||||||||
/// - `crate::observer::trigger_dynamic_ref_with_caller` | ||||||||||||||||||||||||||||||||
/// - `crate::observer::observer_multiple_events` | ||||||||||||||||||||||||||||||||
/// - `crate::observer::observer_dynamic_trigger` | ||||||||||||||||||||||||||||||||
/// - `crate::observer::runner::hook_on_add` | ||||||||||||||||||||||||||||||||
Comment on lines
+428
to
+437
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We don't need to give a warning about non-public construction: Rust makes that impossible with correct usage of the visibility system. But we should give some docs about what this type is for and how to get it. |
||||||||||||||||||||||||||||||||
#[derive(Debug, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)] | ||||||||||||||||||||||||||||||||
pub struct EventKey(pub(crate) ComponentId); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
impl EventKey { | ||||||||||||||||||||||||||||||||
/// Returns id of the underlying [`Event`]. | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||
/// Used internally in: | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This list will be really hard to maintain. Better to leave this off, and allow users to use their IDEs go-to-uses instead. |
||||||||||||||||||||||||||||||||
/// - [`crate::event::register_event`] | ||||||||||||||||||||||||||||||||
/// - [`crate::event::run_updates`] | ||||||||||||||||||||||||||||||||
/// - [`crate::event::deregister_events`] | ||||||||||||||||||||||||||||||||
#[inline] | ||||||||||||||||||||||||||||||||
pub(crate) fn component_id(&self) -> ComponentId { | ||||||||||||||||||||||||||||||||
self.0 | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that these names would be clearer and more consistent as
register_event_key
/event_key
.