Skip to main content

DynEvent

Struct DynEvent 

Source
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

Source

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"));
Source

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());
Source

pub const fn type_id(&self) -> TypeId

Get the TypeId of the wrapped event.

Source

pub const fn type_name(&self) -> &'static str

Get the type name of the wrapped event (for debugging).

Source

pub const fn priority(&self) -> u32

Get the event’s priority.

Source

pub const fn scope(&self) -> Option<&EventScope>

Get a reference to the attached scope, if any.

Source

pub fn take_scope(&mut self) -> Option<EventScope>

Take the attached scope, leaving None in its place.

Source

pub fn is<E: Event>(&self) -> bool

Check if this event is of type E.

Source

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);
}
Source

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.

Source

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"),
}

Trait Implementations§

Source§

impl Debug for DynEvent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.