use std::cell::RefCell;
use std::io;
use std::rc::Rc;
use mio::{Evented, Poll, PollOpt, Ready, Token};
use list::ErasedList;
pub mod channel;
pub mod generic;
#[cfg(unix)]
pub mod signals;
pub mod timer;
pub trait EventSource: Evented {
type Event;
fn interest(&self) -> Ready;
fn pollopts(&self) -> PollOpt;
fn make_dispatcher<Data: 'static, F: FnMut(Self::Event, &mut Data) + 'static>(
&self,
callback: F,
) -> Rc<RefCell<EventDispatcher<Data>>>;
}
pub trait EventDispatcher<Data> {
fn ready(&mut self, ready: Ready, data: &mut Data);
}
pub struct Source<E: EventSource> {
pub(crate) source: E,
pub(crate) poll: Rc<Poll>,
pub(crate) list: Rc<RefCell<ErasedList>>,
pub(crate) token: Token,
}
impl<E: EventSource> Source<E> {
pub fn reregister(&self) -> io::Result<()> {
self.poll.reregister(
&self.source,
self.token,
self.source.interest(),
self.source.pollopts(),
)
}
pub fn remove(self) -> E {
let _ = self.poll.deregister(&self.source);
let _dispatcher = self.list.borrow_mut().del_source(self.token);
self.source
}
}
impl<E: EventSource> ::std::ops::Deref for Source<E> {
type Target = E;
fn deref(&self) -> &E {
&self.source
}
}
impl<E: EventSource> ::std::ops::DerefMut for Source<E> {
fn deref_mut(&mut self) -> &mut E {
&mut self.source
}
}
pub struct Idle {
pub(crate) callback: Rc<RefCell<ErasedIdle>>,
}
impl Idle {
pub fn cancel(self) {
self.callback.borrow_mut().cancel();
}
}
pub(crate) trait ErasedIdle {
fn cancel(&mut self);
}
impl<Data> ErasedIdle for Option<Box<FnMut(&mut Data)>> {
fn cancel(&mut self) {
self.take();
}
}