use std::intrinsics::TypeId;
use GenericEvent;
use ptr::Ptr;
use UpdateArgs;
pub trait UpdateEvent {
fn from_update_args(args: &UpdateArgs) -> Option<Self>;
fn from_dt(dt: f64) -> Option<Self>;
fn update<U>(&self, f: |&UpdateArgs| -> U) -> Option<U>;
}
impl<T: GenericEvent> UpdateEvent for T {
#[inline(always)]
fn from_update_args(args: &UpdateArgs) -> Option<T> {
let id = TypeId::of::<Box<UpdateEvent>>();
Ptr::with_ref::<UpdateArgs, Option<T>>(args, |ptr| {
GenericEvent::from_event(id, ptr)
})
}
#[inline(always)]
fn from_dt(dt: f64) -> Option<T> {
UpdateEvent::from_update_args(&UpdateArgs { dt: dt })
}
#[inline(always)]
fn update<U>(&self, f: |&UpdateArgs| -> U) -> Option<U> {
let id = TypeId::of::<Box<UpdateEvent>>();
self.with_event(id, |ptr| {
f(ptr.expect::<UpdateArgs>())
})
}
}