Skip to main content

base_scheduler/
spawn.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use crate::{SpawnError, TaskError};
5use std::future::Future;
6
7/// The common spawning contract for schedulers and their handles.
8pub trait Spawn {
9    /// The runtime's awaitable task handle. Runtime adapters may use their own representation while preserving the ownership and result contract.
10    type Handle<Output>: Future<Output = Result<Output, TaskError>> + Send + 'static
11    where
12        Output: Send + 'static;
13
14    /// Register a task with this scheduler before making it runnable.
15    ///
16    /// The returned handle owns the execution lifetime. Dropping it requests destruction after any active poll returns; moving it transfers ownership. Await it explicitly for completion, or cancel cooperatively and then await. If the scheduler is closed, the future is dropped without being polled.
17    fn spawn<FutureType>(
18        &self,
19        future: FutureType,
20    ) -> Result<Self::Handle<FutureType::Output>, SpawnError>
21    where
22        FutureType: Future + Send + 'static,
23        FutureType::Output: Send + 'static;
24}