some_global_executor 0.1.6

Reference thread-per-core executor for the some_executor crate.
Documentation
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Platform selection, and the scheduling order both platforms share.
//!
//! The two backends — `stdlib` for native targets and `wasm` for wasm32 — are
//! selected by `cfg` and re-exported from here, so the rest of the crate names
//! one set of types regardless of target.
//!
//! [`ScheduledTask`] is the part that is *not* platform-specific: it orders
//! delayed tasks for a `BinaryHeap`. Both keys are reversed in the `Ord` impl
//! because `BinaryHeap` is a max-heap and what a scheduler wants is the
//! earliest deadline; the submission counter breaks ties so that two tasks due
//! at the same instant run in the order they were spawned rather than in
//! whatever order the heap happens to produce.

#[derive(Debug)]
struct ScheduledTask {
    deadline: some_executor::Instant,
    order: usize,
    task: crate::SpawnedTask,
}

impl ScheduledTask {
    fn new(task: crate::SpawnedTask, order: usize) -> Self {
        Self {
            deadline: task.imp.poll_after(),
            order,
            task,
        }
    }

    fn deadline(&self) -> some_executor::Instant {
        self.deadline
    }

    fn into_task(self) -> crate::SpawnedTask {
        self.task
    }
}

impl PartialEq for ScheduledTask {
    fn eq(&self, other: &Self) -> bool {
        self.deadline == other.deadline && self.order == other.order
    }
}

impl Eq for ScheduledTask {}

impl PartialOrd for ScheduledTask {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ScheduledTask {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // BinaryHeap is a max-heap, so reverse both keys to return the
        // earliest deadline and preserve submission order for equal deadlines.
        other
            .deadline
            .cmp(&self.deadline)
            .then_with(|| other.order.cmp(&self.order))
    }
}

#[cfg(not(target_arch = "wasm32"))]
mod stdlib;
#[cfg(not(target_arch = "wasm32"))]
pub use stdlib::*;

#[cfg(target_arch = "wasm32")]
mod wasm;
#[cfg(target_arch = "wasm32")]
pub use wasm::*;