use chrono::{DateTime, Utc};
use std::cmp::Ordering;
fn get_current_time() -> DateTime<Utc> {
Utc::now()
}
pub struct Event<F>
where
F: FnOnce(DateTime<Utc>),
{
id: usize,
time: DateTime<Utc>,
func: F,
}
impl<F> Event<F>
where
F: FnOnce(DateTime<Utc>),
{
pub fn new(id: usize, time: DateTime<Utc>, func: F) -> Self {
Event { id, time, func }
}
pub(crate) fn has_expired(&self) -> bool {
get_current_time() >= self.time
}
pub(crate) fn execute(self) {
(self.func)(get_current_time());
}
}
impl<F> PartialEq for Event<F>
where
F: FnOnce(DateTime<Utc>),
{
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl<F> Eq for Event<F> where F: FnOnce(DateTime<Utc>) {}
impl<F> PartialOrd for Event<F>
where
F: FnOnce(DateTime<Utc>),
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<F> Ord for Event<F>
where
F: FnOnce(DateTime<Utc>),
{
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}