Native Executor
Platform-native async task executor that leverages OS event loops (GCD, GDK) for optimal performance.
Features
- Platform-native scheduling: Direct GCD integration on Apple platforms
- Structured concurrency: Tasks are tied to their handles; dropping an un-awaited handle cancels the task unless it was detached
- Priority-aware execution: Background vs default task prioritization
- Thread-local safety: Non-Send future execution with compile-time guarantees
- Mailbox-based messaging: Share state via serialized cross-thread queues
- Zero-cost abstractions: Direct OS API usage, no additional runtime
Quick Start
use ;
use Duration;
// Spawn a task with default priority
let handle = spawn_local;
// Keep the task alive: awaiting is structured; detach for fire-and-forget.
handle.detach;
// Keep the main thread alive to allow tasks to complete
sleep;
Structured Concurrency
All spawn* functions return AsyncTask handles that own the task lifecycle. Dropping the
handle without calling .await or .detach() cancels the task immediately. Awaiting the
handle gives structured shutdown and propagates panics; detach() opts out and lets the task
run to completion in the background when you truly need fire-and-forget behavior.
Core Components
Task Spawning
use ;
spawn;
spawn_local;
spawn_main;
spawn_with_priority;
Timers
use ;
use Duration;
async ;
Mailbox Messaging
use Mailbox;
use ;
let mailbox = main;
// Send fire-and-forget updates
mailbox.handle;
// Cross-thread with main-thread execution
let main_val = new;
async ;
Platform Support
Current: Apple platforms (macOS, iOS, tvOS, watchOS) via Grand Central Dispatch, Android (native worker queues)
Planned: Linux (GDK)
Unsupported platforms fail at compile-time with clear error messages.
Polyfill Feature
The optional polyfill feature (enabled by default) provides a simulated
executor for targets without a native implementation. Its behavior is as
follows:
- On Apple, Android, and
wasm32targets the feature is a no-op – the native executors and timers always take precedence. - On other targets the crate will not build unless the
polyfillfeature is enabled. Disabling it makes the lack of a native executor a hard error. - The polyfill spins up its own worker threads and exposes a synthetic
"main thread". Call
native_executor::polyfill::start_main_executor()on a dedicated thread before usingspawn_mainorspawn_local. - Because this main thread is not provided by the OS event loop, code that depends on true main-thread semantics (UI frameworks, platform APIs, etc.) may behave differently. The feature exists only as a portability fallback.
Example setup for unsupported targets:
spawn;
Examples
License
This project is licensed under the MIT License.