pub struct DynEvent { /* private fields */ }Expand description
Type-erased event wrapper for dynamic dispatch.
DynEvent wraps any type implementing Event and provides:
- Type-safe downcasting via
TypeId - Priority and metadata access
- Optional scope attachment for lifecycle tracking
§Type Safety
Despite being type-erased, DynEvent maintains full type safety through
TypeId-based downcasting. Incorrect type casts return None rather than
undefined behavior.
§Memory Layout
DynEvent
├── type_id: TypeId (16 bytes)
├── type_name: &'static str (16 bytes)
├── priority: u32 (4 bytes)
├── payload: Box<dyn Any + Send + Sync> (heap-allocated)
└── scope: Option<EventScope> (optional lifecycle tracking)Implementations§
Source§impl DynEvent
impl DynEvent
Sourcepub fn new<E: Event>(event: E) -> Self
pub fn new<E: Event>(event: E) -> Self
Create a new DynEvent from any type implementing Event.
§Example
use reovim_kernel::api::v1::*;
#[derive(Debug)]
struct MyEvent;
impl Event for MyEvent {}
let dyn_event = DynEvent::new(MyEvent);
assert!(dyn_event.type_name().contains("MyEvent"));Sourcepub fn with_scope(self, scope: EventScope) -> Self
pub fn with_scope(self, scope: EventScope) -> Self
Attach an EventScope for lifecycle tracking.
Returns self for method chaining.
§Example
use reovim_kernel::api::v1::*;
#[derive(Debug)]
struct MyEvent;
impl Event for MyEvent {}
let scope = EventScope::new();
let event = DynEvent::new(MyEvent).with_scope(scope);
assert!(event.scope().is_some());Sourcepub const fn type_name(&self) -> &'static str
pub const fn type_name(&self) -> &'static str
Get the type name of the wrapped event (for debugging).
Sourcepub const fn scope(&self) -> Option<&EventScope>
pub const fn scope(&self) -> Option<&EventScope>
Get a reference to the attached scope, if any.
Sourcepub fn take_scope(&mut self) -> Option<EventScope>
pub fn take_scope(&mut self) -> Option<EventScope>
Take the attached scope, leaving None in its place.
Sourcepub fn downcast_ref<E: Event>(&self) -> Option<&E>
pub fn downcast_ref<E: Event>(&self) -> Option<&E>
Attempt to downcast to a reference of type E.
Returns None if the event is not of type E.
§Example
use reovim_kernel::api::v1::*;
#[derive(Debug)]
struct MyEvent { value: i32 }
impl Event for MyEvent {}
let event = DynEvent::new(MyEvent { value: 42 });
if let Some(my_event) = event.downcast_ref::<MyEvent>() {
assert_eq!(my_event.value, 42);
}Sourcepub fn downcast_mut<E: Event>(&mut self) -> Option<&mut E>
pub fn downcast_mut<E: Event>(&mut self) -> Option<&mut E>
Attempt to downcast to a mutable reference of type E.
Returns None if the event is not of type E.
Sourcepub fn into_inner<E: Event>(self) -> Result<E, Self>
pub fn into_inner<E: Event>(self) -> Result<E, Self>
Consume the DynEvent and attempt to extract the inner event.
§Errors
Returns Err(self) if the event is not of type E.
§Panics
This function will not panic under normal operation. The internal
unwrap() is guarded by a TypeId check that ensures type safety.
§Example
use reovim_kernel::api::v1::*;
#[derive(Debug, PartialEq)]
struct MyEvent { value: i32 }
impl Event for MyEvent {}
let event = DynEvent::new(MyEvent { value: 42 });
match event.into_inner::<MyEvent>() {
Ok(my_event) => assert_eq!(my_event.value, 42),
Err(_) => panic!("unexpected type"),
}