use std::cell::RefCell;
use std::io;
use std::rc::Rc;
use mio::{Evented, Poll, PollOpt, Ready, Token};
use list::SourceList;
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<F: FnMut(Self::Event) + 'static>(
&self,
callback: F,
) -> Rc<RefCell<EventDispatcher>>;
}
pub trait EventDispatcher {
fn ready(&mut self, ready: Ready);
}
pub struct Source<E: EventSource> {
pub(crate) source: E,
pub(crate) poll: Rc<Poll>,
pub(crate) list: Rc<RefCell<SourceList>>,
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);
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<Option<Box<FnMut()>>>>,
}
impl Idle {
pub fn cancel(self) {
self.callback.borrow_mut().take();
}
}