use typeable::Typeable;
use traitobject;
use std::any::TypeId;
use std::mem;
use backend;
use super::{CallInfo};
use json::{JsonValue};
pub trait ApiHandler: Typeable {
fn api_call<'a, 'b>(&'a self, &str, &mut JsonValue, &'b mut (backend::Request + 'b), &mut CallInfo<'a>) -> backend::HandleResult<backend::Response>;
}
impl ApiHandler {
pub fn is<E: ApiHandler>(&self) -> bool { self.get_type() == TypeId::of::<E>() }
pub fn downcast<E: ApiHandler>(&self) -> Option<&E> {
if self.is::<E>() {
unsafe { Some(mem::transmute(traitobject::data(self))) }
} else {
None
}
}
#[inline]
pub fn downcast_mut<T: ApiHandler>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
unsafe {
Some(self.downcast_mut_unchecked())
}
} else {
None
}
}
#[inline]
pub unsafe fn downcast_mut_unchecked<T: ApiHandler>
(&mut self) -> &mut T {
mem::transmute(traitobject::data(self))
}
}
pub type ApiHandlers = Vec<Box<ApiHandler + Send + Sync>>;