#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ControlFlow {
Continue,
Break,
}
pub trait EventsLoop<Event> {
fn poll_events(&mut self, callback: &mut dyn FnMut(Event));
fn run(&mut self, callback: &mut dyn FnMut(Event) -> ControlFlow);
fn create_proxy(&self) -> Box<dyn EventsLoopProxy>;
}
pub trait EventsLoopProxy : Send {
fn wakeup(&self) -> Result<(), EventsLoopClosed>;
fn clone(&self) -> Box<dyn EventsLoopProxy>;
}
impl Clone for Box<dyn EventsLoopProxy> {
fn clone(&self) -> Box<dyn EventsLoopProxy> {
use std::ops::Deref;
self.deref().clone()
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct EventsLoopClosed;
impl std::fmt::Display for EventsLoopClosed {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self.to_string())
}
}
impl std::error::Error for EventsLoopClosed {
fn description(&self) -> &str {
"Tried to wake up a closed `EventsLoop`"
}
}
#[cfg(test)]
mod tests {
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
enum Events {
A, B, C
}
struct Loop;
impl ::EventsLoop<Events> for Loop {
fn poll_events(&mut self, callback: &mut dyn FnMut(Events)) {
callback(Events::A);
callback(Events::B);
}
fn run(&mut self, callback: &mut dyn FnMut(Events) -> ::ControlFlow){
while callback(Events::C) == ::ControlFlow::Continue {
}
}
fn create_proxy(&self) -> Box<dyn (::EventsLoopProxy)> {
unimplemented!();
}
}
#[test]
fn it_works() {
use ::EventsLoop;
let mut l = Loop{};
l.poll_events(&mut |e| println!("{:?}", e));
l.run(&mut |e| {println!("{:?}", e); ::ControlFlow::Break});
}
}