Skip to content

Commit

Permalink
Remove EventData trait and drop the requirement. Components can now a…
Browse files Browse the repository at this point in the history
…lways be events.
  • Loading branch information
Indra-db committed Apr 5, 2024
1 parent 5977c1d commit 84dee53
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 35 deletions.
2 changes: 0 additions & 2 deletions flecs_ecs/examples/observer_custom_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use common::*;
#[derive(Default, Clone, Component)]
struct MyEvent;

impl EventData for MyEvent {}

fn main() {
let world = World::new();

Expand Down
3 changes: 0 additions & 3 deletions flecs_ecs/examples/observer_entity_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ struct Resize {
height: f32,
}

impl EventData for Click {}
impl EventData for Resize {}

fn main() {
let world = World::new();

Expand Down
14 changes: 11 additions & 3 deletions flecs_ecs/src/core/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,15 @@ impl Entity {
///
/// * C++ API: `entity::null`
#[doc(alias = "entity::null")]
pub fn new_null() -> Entity {
Entity::default()
pub const fn new_null() -> Entity {
Entity {
entity_view: EntityView {
id: super::Id {
raw_id: 0,
world: std::ptr::null_mut(),
},
},
}
}

/// Add an id to an entity.
Expand Down Expand Up @@ -1526,7 +1533,8 @@ impl Entity {
///
/// * C++ API: `entity::get_mut`
#[doc(alias = "entity::get_mut")]
pub fn get_mut<T: ComponentId>(&mut self) -> &mut T::UnderlyingType {
#[allow(clippy::mut_from_ref)]
pub fn get_mut<T: ComponentId>(&self) -> &mut T::UnderlyingType {
// This branch will be removed in release mode since this can be determined at compile time.
if !T::IS_ENUM {
let component_id = T::get_id(self.world);
Expand Down
26 changes: 13 additions & 13 deletions flecs_ecs/src/core/entity_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use super::{
table::{Table, TableRange},
try_register_component,
world::World,
CachedEnumData, EmptyComponent, EventBuilderImpl, EventData, IntoComponentId, IntoEntityId,
CachedEnumData, EmptyComponent, EventBuilderImpl, IntoComponentId, IntoEntityId,
IntoEntityIdExt, IntoWorld, IterT, NotEmptyComponent, ObserverEntityBindingCtx, ECS_ANY,
ECS_CHILD_OF, ECS_WILDCARD,
};
Expand Down Expand Up @@ -1769,7 +1769,7 @@ impl EntityView {
///
/// * C++ API: `entity_view::emit`
#[doc(alias = "entity_view::emit")]
pub fn emit<T: EventData + EmptyComponent + ComponentId>(&self) {
pub fn emit<T: EmptyComponent + ComponentId>(&self) {
self.emit_id(T::get_id(self.world));
}

Expand All @@ -1783,7 +1783,7 @@ impl EntityView {
///
/// * C++ API: `entity_view::emit`
#[doc(alias = "entity_view::emit")]
pub fn emit_payload<T: EventData + NotEmptyComponent + ComponentId>(&self, payload: &mut T) {
pub fn emit_payload<T: NotEmptyComponent + ComponentId>(&self, payload: &mut T) {
self.get_world()
.event::<T>()
.set_entity_to_emit(self.to_entity())
Expand Down Expand Up @@ -1818,7 +1818,7 @@ impl EntityView {
///
/// * C++ API: `entity_view::enqueue`
#[doc(alias = "entity_view::enqueue")]
pub fn enqueue<T: EventData + EmptyComponent + ComponentId>(&self) {
pub fn enqueue<T: EmptyComponent + ComponentId>(&self) {
self.enqueue_id(T::get_id(self.world));
}

Expand All @@ -1832,7 +1832,7 @@ impl EntityView {
///
/// * C++ API: `entity_view::enqueue`
#[doc(alias = "entity_view::enqueue")]
pub fn enqueue_payload<T: EventData + NotEmptyComponent + ComponentId>(&self, payload: &mut T) {
pub fn enqueue_payload<T: NotEmptyComponent + ComponentId>(&self, payload: &mut T) {
self.get_world()
.event::<T>()
.set_entity_to_emit(self.to_entity())
Expand All @@ -1859,15 +1859,15 @@ impl EntityView {
#[doc(alias = "entity_builder::observe")]
pub fn observe<C>(&self, func: impl FnMut()) -> &Self
where
C: EventData + ComponentId + EmptyComponent,
C: ComponentId + EmptyComponent,
{
self.observe_impl::<C, _>(func)
}

fn observe_impl<C, Func>(&self, func: Func) -> &Self
where
Func: FnMut(),
C: EventData + ComponentId,
C: ComponentId,
{
let new_binding_ctx = Box::<super::ObserverEntityBindingCtx>::default();
let binding_ctx = Box::leak(new_binding_ctx);
Expand Down Expand Up @@ -1904,15 +1904,15 @@ impl EntityView {
#[doc(alias = "entity_builder::observe")]
pub fn observe_entity<C>(&self, func: impl FnMut(&mut Entity)) -> &Self
where
C: EventData + ComponentId + EmptyComponent,
C: ComponentId + EmptyComponent,
{
self.observe_entity_impl::<C, _>(func)
}

fn observe_entity_impl<C, Func>(&self, func: Func) -> &Self
where
Func: FnMut(&mut Entity),
C: EventData + ComponentId,
C: ComponentId,
{
let new_binding_ctx = Box::<super::ObserverEntityBindingCtx>::default();
let binding_ctx = Box::leak(new_binding_ctx);
Expand Down Expand Up @@ -1949,15 +1949,15 @@ impl EntityView {
#[doc(alias = "entity_builder::observe")]
pub fn observe_payload<C>(&self, func: impl FnMut(&mut C)) -> &Self
where
C: EventData + ComponentId + NotEmptyComponent,
C: ComponentId + NotEmptyComponent,
{
self.observe_payload_impl::<C, _>(func)
}

fn observe_payload_impl<C, Func>(&self, func: Func) -> &Self
where
Func: FnMut(&mut C),
C: EventData + ComponentId,
C: ComponentId,
{
let new_binding_ctx = Box::<super::ObserverEntityBindingCtx>::default();
let binding_ctx = Box::leak(new_binding_ctx);
Expand Down Expand Up @@ -1994,15 +1994,15 @@ impl EntityView {
#[doc(alias = "entity_builder::observe")]
pub fn observe_payload_entity<C>(&self, func: impl FnMut(&mut Entity, &mut C)) -> &Self
where
C: EventData + ComponentId + NotEmptyComponent,
C: ComponentId + NotEmptyComponent,
{
self.observe_payload_entity_impl::<C, _>(func)
}

fn observe_payload_entity_impl<C, Func>(&self, func: Func) -> &Self
where
Func: FnMut(&mut Entity, &mut C),
C: EventData + ComponentId,
C: ComponentId,
{
let new_binding_ctx = Box::<super::ObserverEntityBindingCtx>::default();
let binding_ctx = Box::leak(new_binding_ctx);
Expand Down
5 changes: 0 additions & 5 deletions flecs_ecs/src/core/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ use super::{
ecs_pair, ComponentId, EventBuilder, IntoComponentId, IntoEntityId, IntoEntityIdExt, IntoTable,
};

/// Trait to mark component structs as `EventData` to be used in `EventBuilderTyped`.
/// This is used to set the event data for the event to be emitted
/// this is to ensure that the event data is of the correct type and the component is meant to be used with `EventBuilderTyped`
pub trait EventData {}

/// Event builder trait to implement '`set_event_data`' for untyped and typed `EventBuilder`
pub trait EventBuilderImpl {
type BuiltType;
Expand Down
14 changes: 7 additions & 7 deletions flecs_ecs/src/core/event_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::sys::{ecs_event_desc_t, FLECS_EVENT_DESC_MAX};
use super::{
c_types::{IdT, TypeT},
component_registration::ComponentId,
event::{EventBuilderImpl, EventData},
event::EventBuilderImpl,
world::World,
IntoEntityId,
};
Expand Down Expand Up @@ -93,12 +93,12 @@ impl EventBuilderImpl for EventBuilder {
/// Ensures the use of appropriate data types for events, enhancing type safety and data integrity.
/// This design aims to prevent the utilization of incompatible components as event data,
/// thereby ensuring greater explicitness and correctness in event handling.
pub struct EventBuilderTyped<'a, T: EventData + ComponentId> {
pub struct EventBuilderTyped<'a, T: ComponentId> {
pub(crate) builder: EventBuilder,
_phantom: std::marker::PhantomData<&'a T>,
}

impl<'a, T: EventData + ComponentId> EventBuilderTyped<'a, T> {
impl<'a, T: ComponentId> EventBuilderTyped<'a, T> {
/// Create a new typed `EventBuilder`
///
/// # Arguments
Expand All @@ -121,7 +121,7 @@ impl<'a, T: EventData + ComponentId> EventBuilderTyped<'a, T> {
/// The `Deref` trait is implemented to allow `EventBuilderTyped` instances to be treated as
/// references to `EventBuilder`. This enables the use of `EventBuilder` methods directly on
/// `EventBuilderTyped` instances.
impl<'a, T: EventData + ComponentId> Deref for EventBuilderTyped<'a, T> {
impl<'a, T: ComponentId> Deref for EventBuilderTyped<'a, T> {
type Target = EventBuilder;

fn deref(&self) -> &Self::Target {
Expand All @@ -130,15 +130,15 @@ impl<'a, T: EventData + ComponentId> Deref for EventBuilderTyped<'a, T> {
}

/// See `Deref` trait for more information.
impl<'a, T: EventData + ComponentId> DerefMut for EventBuilderTyped<'a, T> {
impl<'a, T: ComponentId> DerefMut for EventBuilderTyped<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.builder
}
}

impl<'a, T: EventData + ComponentId> EventBuilderImpl for EventBuilderTyped<'a, T>
impl<'a, T: ComponentId> EventBuilderImpl for EventBuilderTyped<'a, T>
where
T: EventData + ComponentId,
T: ComponentId,
{
type BuiltType = &'a mut T;
type ConstBuiltType = &'a T;
Expand Down
3 changes: 1 addition & 2 deletions flecs_ecs/src/core/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ use super::{EmptyComponent, NotEmptyComponent};
use super::{
ecs_pair,
entity::Entity,
event::EventData,
event_builder::{EventBuilder, EventBuilderTyped},
id::Id,
iterable::Iterable,
Expand Down Expand Up @@ -3055,7 +3054,7 @@ impl World {
///
/// * C++ API: `world::event`
#[doc(alias = "world::event")]
pub fn event<T: EventData + ComponentId>(&self) -> EventBuilderTyped<T> {
pub fn event<T: ComponentId>(&self) -> EventBuilderTyped<T> {
EventBuilderTyped::<T>::new(self, T::get_id(self.raw_world))
}
}
Expand Down

0 comments on commit 84dee53

Please sign in to comment.