实现一个主事件循环,不停往主事件循环中塞入事件,事件可以描述他的名称、事件类型(不等待,等待,终止)、携带的数据、指定的事件处理。
mod event;
mod handler;
use event::{EventType, Event};
use handler::Handler;
use std::thread;
use std::time::Duration;
fn main() {
let mut event_loop: Vec<Event<i64, i64>> = Vec::new();
let mut event1 = Event::<i64, i64>::new("PlusOne", EventType::NoWait, 1);
let plus_one = Handler::<i64, i64>::new(|x: i64| x + 1);
event1.set_handler(&plus_one);
let mut event2 = Event::<i64, i64>::new("MinusOne", EventType::Wait(5), 0);
let minus_one = Handler::<i64, i64>::new(|x: i64| x - 1);
event2.set_handler(&minus_one);
event_loop.push(event1);
event_loop.push(event2);
loop {
if let Some(event) = event_loop.pop() {
match event.get_type() {
EventType::NoWait => {
println!("{:?}", event.name);
let result = event.handle();
println!("{:?}", result);
}
EventType::Wait(duration) => {
thread::sleep(Duration::from_secs(duration));
println!("{:?}", event.name);
let result = event.handle();
println!("{:?}", result);
}
EventType::Stop => {
break;
}
}
}
}
}
use crate::handler::EventHandler;
#[derive(Debug, Copy, Clone)]
#[allow(dead_code)]
pub enum EventType {
NoWait,
Wait(u64),
Stop,
}
pub struct Event<'a, T, R> {
pub name: &'static str,
r#type: EventType,
data: T,
handler: Option<&'a (dyn EventHandler<T, Output=R>)>,
}
#[allow(dead_code)]
impl<'a, T: Clone, R> Event<'a, T, R> {
pub fn new(name: &'static str, r#type: EventType, data: T) -> Self {
Self {
name,
r#type,
data,
handler: None,
}
}
pub fn set_handler(&mut self, handler: &'a (dyn EventHandler<T, Output=R>)) {
self.handler = Option::from(handler);
}
pub fn handle(&self) -> Option<R> {
if let Some(handler) = self.handler {
Option::from(handler.handle(self.data.clone()))
} else {
None
}
}
pub fn get_type(&self) -> EventType {
self.r#type
}
}
pub trait EventHandler<T> {
type Output;
fn get_func(&self) -> fn(T) -> Self::Output;
fn handle(&self, data: T) -> Self::Output {
self.get_func()(data)
}
}
#[derive(Debug)]
pub struct Handler<T, Output> {
func: fn(data: T) -> Output,
}
impl<T, Output> EventHandler<T> for Handler<T, Output> {
type Output = Output;
fn get_func(&self) -> fn(T) -> Self::Output { self.func }
}
impl<T, Output> Handler<T, Output> {
pub fn new(func: fn(T) -> Output) -> Self {
Self {
func
}
}
}
/usr/bin/cargo run --color=always
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/event-driven`
"MinusOne"
Some(-1)
"PlusOne"
Some(2)