timerwheel 0.1.0

Hierarchical timer wheel for delayed task scheduling with pluggable executors.
Documentation
// Copyright © 2026-present The Timerwheel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

use crate::timer::metrics::TimerMetricsInner;
use crate::timing_wheel::state::{EntryState, TimeoutState};

#[derive(Clone, Debug)]
/// Handle returned for a scheduled timeout.
pub struct Timeout {
    state: Arc<TimeoutState>,
    metrics: Arc<TimerMetricsInner>,
    permits: Arc<AtomicUsize>,
}

impl Timeout {
    pub(crate) fn new(
        state: Arc<TimeoutState>,
        metrics: Arc<TimerMetricsInner>,
        permits: Arc<AtomicUsize>,
    ) -> Self {
        Self {
            state,
            metrics,
            permits,
        }
    }

    /// Attempts to cancel the timeout before it expires.
    pub fn cancel(&self) -> bool {
        if self.state.cancel() {
            self.metrics.cancelled();
            self.permits.fetch_sub(1, Ordering::AcqRel);
            return true;
        }
        false
    }

    /// Returns true when the timeout was cancelled.
    pub fn is_cancelled(&self) -> bool {
        self.state.load() == EntryState::Cancelled
    }

    /// Returns true when the timeout has been accepted for execution.
    pub fn is_expired(&self) -> bool {
        self.state.load() == EntryState::Expired
    }
}