use std::io;
use std::sync::Arc;
use std::marker::PhantomData;
use error::ErrCode;
use super::{IoObject, IoService, Callback};
pub trait Handler<R> : Sized + Send + 'static {
type Output;
fn callback(self, io: &IoService, res: io::Result<R>);
#[doc(hidden)]
fn wrap<G>(self, callback: G) -> Callback
where G: FnOnce(&IoService, ErrCode, Self) + Send + 'static;
#[doc(hidden)]
type AsyncResult : AsyncResult<Self::Output>;
#[doc(hidden)]
fn async_result(&self) -> Self::AsyncResult;
}
pub trait AsyncResult<R> {
fn get(self, io: &IoService) -> R;
}
pub struct NoAsyncResult;
impl AsyncResult<()> for NoAsyncResult {
fn get(self, _io: &IoService) {
}
}
pub struct ArcHandler<T, F, R> {
data: Arc<T>,
handler: F,
_marker: PhantomData<R>,
}
impl<T, F, R> Handler<R> for ArcHandler<T, F, R>
where T: IoObject + Send + Sync + 'static,
F: FnOnce(Arc<T>, io::Result<R>) + Send + 'static,
R: Send + 'static,
{
type Output = ();
fn callback(self, _: &IoService, res: io::Result<R>) {
let ArcHandler { data, handler, _marker } = self;
handler(data, res)
}
fn wrap<G>(self, callback: G) -> Callback
where G: FnOnce(&IoService, ErrCode, Self) + Send + 'static
{
Box::new(move |io: *const IoService, ec| {
callback(unsafe { &*io }, ec, self)
})
}
type AsyncResult = NoAsyncResult;
fn async_result(&self) -> Self::AsyncResult {
NoAsyncResult
}
}
pub fn wrap<T, F, R>(handler: F, data: &Arc<T>) -> ArcHandler<T, F, R>
where T: IoObject,
{
ArcHandler {
data: data.clone(),
handler: handler,
_marker: PhantomData,
}
}