okee-wheel-timer 0.1.0

Deterministic hashed wheel timer with keyed deduplication.
Documentation
/// Unique identifier of an event inside a wheel instance.
///
/// IDs are assigned by [`HashedWheelTimer::schedule`](crate::HashedWheelTimer::schedule)
/// and preserved by update/reschedule operations.
pub type EventId = u64;

/// A scheduled event stored in the wheel.
///
/// `Event` values are returned by read/pop/remove operations.
/// They carry metadata used by the wheel scheduler:
/// - [`tick`](Self::tick): absolute tick when the event can be processed.
/// - [`delta_tick`](Self::delta_tick): wave index within the same `tick`.
///
/// # Example
/// ```
/// use okee_wheel_timer::HashedWheelTimer;
///
/// let mut wheel = HashedWheelTimer::new(8);
/// let id = wheel.schedule(0, "payload").id;
///
/// let events = wheel.pop_events();
/// assert_eq!(events[0].id(), id);
/// assert_eq!(events[0].tick(), 0);
/// assert_eq!(events[0].data(), &"payload");
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Event<T> {
    id: EventId,
    tick: u64,
    delta_tick: u64,
    data: T,
}

impl<T> Event<T> {
    pub(crate) fn new(id: EventId, tick: u64, delta_tick: u64, data: T) -> Self {
        Self {
            id,
            tick,
            delta_tick,
            data,
        }
    }

    /// Returns the unique ID of this event.
    pub fn id(&self) -> EventId {
        self.id
    }

    /// Returns the absolute tick when this event becomes available.
    pub fn tick(&self) -> u64 {
        self.tick
    }

    /// Returns the wave number inside [`tick`](Self::tick).
    ///
    /// Events with the same `(tick, delta_tick)` are popped together.
    pub fn delta_tick(&self) -> u64 {
        self.delta_tick
    }

    /// Returns a shared reference to the payload.
    pub fn data(&self) -> &T {
        &self.data
    }

    /// Consumes the event and returns its payload.
    pub fn into_data(self) -> T {
        self.data
    }
}