diff --git a/flecs_ecs/examples/observer_custom_event.rs b/flecs_ecs/examples/observer_custom_event.rs index 08022d44..36d21d6b 100644 --- a/flecs_ecs/examples/observer_custom_event.rs +++ b/flecs_ecs/examples/observer_custom_event.rs @@ -5,8 +5,6 @@ use common::*; #[derive(Default, Clone, Component)] struct MyEvent; -impl EventData for MyEvent {} - fn main() { let world = World::new(); diff --git a/flecs_ecs/examples/observer_entity_event.rs b/flecs_ecs/examples/observer_entity_event.rs index d02a5a6f..0d2a0171 100644 --- a/flecs_ecs/examples/observer_entity_event.rs +++ b/flecs_ecs/examples/observer_entity_event.rs @@ -22,9 +22,6 @@ struct Resize { height: f32, } -impl EventData for Click {} -impl EventData for Resize {} - fn main() { let world = World::new(); diff --git a/flecs_ecs/src/core/entity.rs b/flecs_ecs/src/core/entity.rs index 9f269e82..afa70c22 100644 --- a/flecs_ecs/src/core/entity.rs +++ b/flecs_ecs/src/core/entity.rs @@ -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. @@ -1526,7 +1533,8 @@ impl Entity { /// /// * C++ API: `entity::get_mut` #[doc(alias = "entity::get_mut")] - pub fn get_mut(&mut self) -> &mut T::UnderlyingType { + #[allow(clippy::mut_from_ref)] + pub fn get_mut(&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); diff --git a/flecs_ecs/src/core/entity_view.rs b/flecs_ecs/src/core/entity_view.rs index 6dfee02f..8fbb397d 100644 --- a/flecs_ecs/src/core/entity_view.rs +++ b/flecs_ecs/src/core/entity_view.rs @@ -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, }; @@ -1769,7 +1769,7 @@ impl EntityView { /// /// * C++ API: `entity_view::emit` #[doc(alias = "entity_view::emit")] - pub fn emit(&self) { + pub fn emit(&self) { self.emit_id(T::get_id(self.world)); } @@ -1783,7 +1783,7 @@ impl EntityView { /// /// * C++ API: `entity_view::emit` #[doc(alias = "entity_view::emit")] - pub fn emit_payload(&self, payload: &mut T) { + pub fn emit_payload(&self, payload: &mut T) { self.get_world() .event::() .set_entity_to_emit(self.to_entity()) @@ -1818,7 +1818,7 @@ impl EntityView { /// /// * C++ API: `entity_view::enqueue` #[doc(alias = "entity_view::enqueue")] - pub fn enqueue(&self) { + pub fn enqueue(&self) { self.enqueue_id(T::get_id(self.world)); } @@ -1832,7 +1832,7 @@ impl EntityView { /// /// * C++ API: `entity_view::enqueue` #[doc(alias = "entity_view::enqueue")] - pub fn enqueue_payload(&self, payload: &mut T) { + pub fn enqueue_payload(&self, payload: &mut T) { self.get_world() .event::() .set_entity_to_emit(self.to_entity()) @@ -1859,7 +1859,7 @@ impl EntityView { #[doc(alias = "entity_builder::observe")] pub fn observe(&self, func: impl FnMut()) -> &Self where - C: EventData + ComponentId + EmptyComponent, + C: ComponentId + EmptyComponent, { self.observe_impl::(func) } @@ -1867,7 +1867,7 @@ impl EntityView { fn observe_impl(&self, func: Func) -> &Self where Func: FnMut(), - C: EventData + ComponentId, + C: ComponentId, { let new_binding_ctx = Box::::default(); let binding_ctx = Box::leak(new_binding_ctx); @@ -1904,7 +1904,7 @@ impl EntityView { #[doc(alias = "entity_builder::observe")] pub fn observe_entity(&self, func: impl FnMut(&mut Entity)) -> &Self where - C: EventData + ComponentId + EmptyComponent, + C: ComponentId + EmptyComponent, { self.observe_entity_impl::(func) } @@ -1912,7 +1912,7 @@ impl EntityView { fn observe_entity_impl(&self, func: Func) -> &Self where Func: FnMut(&mut Entity), - C: EventData + ComponentId, + C: ComponentId, { let new_binding_ctx = Box::::default(); let binding_ctx = Box::leak(new_binding_ctx); @@ -1949,7 +1949,7 @@ impl EntityView { #[doc(alias = "entity_builder::observe")] pub fn observe_payload(&self, func: impl FnMut(&mut C)) -> &Self where - C: EventData + ComponentId + NotEmptyComponent, + C: ComponentId + NotEmptyComponent, { self.observe_payload_impl::(func) } @@ -1957,7 +1957,7 @@ impl EntityView { fn observe_payload_impl(&self, func: Func) -> &Self where Func: FnMut(&mut C), - C: EventData + ComponentId, + C: ComponentId, { let new_binding_ctx = Box::::default(); let binding_ctx = Box::leak(new_binding_ctx); @@ -1994,7 +1994,7 @@ impl EntityView { #[doc(alias = "entity_builder::observe")] pub fn observe_payload_entity(&self, func: impl FnMut(&mut Entity, &mut C)) -> &Self where - C: EventData + ComponentId + NotEmptyComponent, + C: ComponentId + NotEmptyComponent, { self.observe_payload_entity_impl::(func) } @@ -2002,7 +2002,7 @@ impl EntityView { fn observe_payload_entity_impl(&self, func: Func) -> &Self where Func: FnMut(&mut Entity, &mut C), - C: EventData + ComponentId, + C: ComponentId, { let new_binding_ctx = Box::::default(); let binding_ctx = Box::leak(new_binding_ctx); diff --git a/flecs_ecs/src/core/event.rs b/flecs_ecs/src/core/event.rs index 4b7c9b87..a241df05 100644 --- a/flecs_ecs/src/core/event.rs +++ b/flecs_ecs/src/core/event.rs @@ -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; diff --git a/flecs_ecs/src/core/event_builder.rs b/flecs_ecs/src/core/event_builder.rs index 18146442..d341ffa1 100644 --- a/flecs_ecs/src/core/event_builder.rs +++ b/flecs_ecs/src/core/event_builder.rs @@ -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, }; @@ -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 @@ -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 { @@ -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; diff --git a/flecs_ecs/src/core/world.rs b/flecs_ecs/src/core/world.rs index ec6c81d3..6301d768 100644 --- a/flecs_ecs/src/core/world.rs +++ b/flecs_ecs/src/core/world.rs @@ -46,7 +46,6 @@ use super::{EmptyComponent, NotEmptyComponent}; use super::{ ecs_pair, entity::Entity, - event::EventData, event_builder::{EventBuilder, EventBuilderTyped}, id::Id, iterable::Iterable, @@ -3055,7 +3054,7 @@ impl World { /// /// * C++ API: `world::event` #[doc(alias = "world::event")] - pub fn event(&self) -> EventBuilderTyped { + pub fn event(&self) -> EventBuilderTyped { EventBuilderTyped::::new(self, T::get_id(self.raw_world)) } }