use std::time::Duration;
use crate::app::App;
use super::Event;
#[derive(Debug)]
pub struct EventHandler {
pub event_type: Event,
pub interval: Option<Duration>,
pub handler_fn: fn(&mut App), }
impl EventHandler {
pub fn new(event_type: Event, interval: Option<Duration>, handler_fn: fn(&mut App)) -> Self {
return EventHandler {
event_type,
interval,
handler_fn,
};
}
}
impl PartialEq for EventHandler {
fn eq(&self, other: &Self) -> bool {
let self_handler_fn = self.handler_fn as *const ();
let other_handler_fn = other.handler_fn as *const ();
self.event_type == other.event_type && self_handler_fn == other_handler_fn
}
}
impl Eq for EventHandler {}
impl Ord for EventHandler {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
if self.interval.is_none() {
return std::cmp::Ordering::Greater;
} else if other.interval.is_none() {
return std::cmp::Ordering::Less;
}
self.interval
.unwrap()
.cmp(&other.interval.unwrap())
.then(self.interval.unwrap().cmp(&other.interval.unwrap()))
}
}
impl PartialOrd for EventHandler {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}