hirun 0.1.1

rust异步运行框架
Documentation
use super::Scheduler;
use core::cell::Cell;
use core::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
use core::ptr;
use core::time::Duration;
use hicollections::{rbtree, RbTree, RbTreeNode};

#[repr(C)]
pub struct Timer {
    pub(crate) node: RbTreeNode,
    pub(crate) timeout: Cell<Duration>,
    handle: fn(&Timer, &mut Scheduler),
}

pub fn timer_rbtree_new() -> RbTree<Timer> {
    rbtree!(Timer, node)
}

impl PartialOrd<Timer> for Timer {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        let cmp = self.timeout.partial_cmp(&other.timeout);
        match cmp {
            Some(Ordering::Equal) => (self as *const Self).partial_cmp(&(other as *const Self)),
            _ => cmp,
        }
    }
}

impl PartialEq<Timer> for Timer {
    fn eq(&self, other: &Self) -> bool {
        ptr::eq(self, other)
    }
}

impl Eq for Timer {}

impl Ord for Timer {
    fn cmp(&self, other: &Self) -> Ordering {
        (self as *const Self).cmp(&(other as *const Self))
    }
}

impl Timer {
    pub fn new(handle: fn(&Timer, &mut Scheduler)) -> Self {
        Self {
            node: RbTreeNode::new(),
            timeout: Cell::new(Duration::new(0, 0)),
            handle,
        }
    }
    pub fn handle(&self, sched: &mut Scheduler) {
        (self.handle)(self, sched)
    }
}