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.

pub(crate) mod builder;
pub(crate) mod metric_sink;
pub(crate) mod metrics;
pub(crate) mod panic_handler;
pub(crate) mod pool;
pub(crate) mod task;
pub(crate) mod worker;

pub use crate::executor::builder::PoolBuilder;
pub use crate::executor::metric_sink::{MetricSink, NoopMetricSink};
pub use crate::executor::metrics::ExecutorMetrics;
pub use crate::executor::panic_handler::{NoopPanicHandler, PanicHandler};
pub use crate::executor::pool::Pool;
pub use crate::executor::task::{BoxTask, Task};
use crate::{Error, Result};

/// Rejected executor task together with the rejection reason.
pub struct RejectedTask {
    error: Error,
    task: BoxTask,
}

impl RejectedTask {
    /// Creates a rejection that preserves ownership of the original task.
    pub fn new(error: Error, task: BoxTask) -> Self {
        Self { error, task }
    }

    /// Returns the rejection reason.
    pub fn error(&self) -> &Error {
        &self.error
    }

    /// Returns the original task.
    pub fn into_task(self) -> BoxTask {
        self.task
    }

    /// Splits the rejection into reason and original task.
    pub fn into_parts(self) -> (Error, BoxTask) {
        (self.error, self.task)
    }
}

/// Non-blocking execution boundary used by the timer scheduler.
pub trait Executor: Send + Sync + 'static {
    /// Attempts to accept a task without waiting for worker capacity.
    fn try_execute(&self, task: BoxTask) -> std::result::Result<(), RejectedTask>;

    /// Stops the executor and releases resources owned by it.
    fn shutdown(&self) -> Result<()>;

    /// Returns an immutable metrics snapshot.
    fn metrics(&self) -> ExecutorMetrics;
}