native_executor/
apple.rs

1//! Apple platform implementation using Grand Central Dispatch (GCD).
2//!
3//! This module provides the native executor implementation for Apple platforms
4//! (macOS, iOS, tvOS, watchOS) by leveraging Grand Central Dispatch for optimal
5//! performance and system integration.
6
7use core::time::Duration;
8use dispatch::{Queue, QueuePriority};
9
10use crate::{PlatformExecutor, Priority};
11
12impl From<Priority> for QueuePriority {
13    fn from(val: Priority) -> Self {
14        match val {
15            Priority::Background => Self::Background,
16            Priority::Utility => Self::Low,
17            Priority::UserInitiated | Priority::UserInteractive => Self::High,
18            // Fallback to Default for any future variants
19            _ => Self::Default,
20        }
21    }
22}
23/// Apple platform executor implementation using Grand Central Dispatch.
24///
25/// This executor provides optimal performance on Apple platforms by directly
26/// leveraging GCD's system-level thread pools and scheduling primitives.
27#[derive(Debug, Clone, Copy, Default)]
28pub struct ApplePlatformExecutor;
29
30impl PlatformExecutor for ApplePlatformExecutor {
31    fn exec_main(f: impl FnOnce() + Send + 'static) {
32        let main = Queue::main();
33        main.exec_async(f);
34    }
35
36    fn exec(f: impl FnOnce() + Send + 'static, priority: Priority) {
37        let queue = Queue::global(priority.into());
38        queue.exec_async(f);
39    }
40
41    fn exec_after(delay: Duration, f: impl FnOnce() + Send + 'static, priority: Priority) {
42        let queue = Queue::global(priority.into());
43        queue.exec_after(delay, f);
44    }
45}