pub trait SpawnSync {
// Required method
fn spawn_blocking<F, O>(func: F) -> impl Task<O>
where F: Fn() -> O + Send + 'static,
O: Send + 'static;
}Expand description
The SpawnSync trait provides an interface for spawning blocking (synchronous) tasks.
This trait is the synchronous version of the SpawnAsync trait,
allowing potentially long-running CPU-bound or blocking operations to be executed.
It returns a handle implementing the Task trait.
§Examples
use node_flow::context::{SpawnSync, Task};
struct MyRuntime;
struct DummyTask<T>(T);
impl<T> Future for DummyTask<T> // ...
impl<T> Task<T> for DummyTask<T> // ...
impl SpawnSync for MyRuntime {
fn spawn_blocking<F, O>(func: F) -> impl Task<O>
where
F: Fn() -> O + Send + 'static,
O: Send + 'static,
{
// Example stub (replace with actual runtime call)
DummyTask(func())
}
}Required Methods§
Sourcefn spawn_blocking<F, O>(func: F) -> impl Task<O>
fn spawn_blocking<F, O>(func: F) -> impl Task<O>
Spawns a blocking (synchronous) function in a background.
The function func is executed on a separate worker thread. The returned
task can be awaited or canceled, depending on the runtime implementation.
§Type Parameters
F: A closure or function that produces an output of typeO.O: The output type of the blocking computation.
§Returns
A task handle implementing Task<O> trait.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.