|
| 1 | +//! Provides a fundamental executor primitive appropriate for the target platform |
| 2 | +//! and feature set selected. |
| 3 | +//! By default, the `async_executor` feature will be enabled, which will rely on |
| 4 | +//! [`async-executor`] for the underlying implementation. This requires `std`, |
| 5 | +//! so is not suitable for `no_std` contexts. Instead, you must use `edge_executor`, |
| 6 | +//! which relies on the alternate [`edge-executor`] backend. |
| 7 | +//! |
| 8 | +//! [`async-executor`]: https://crates.io/crates/async-executor |
| 9 | +//! [`edge-executor`]: https://crates.io/crates/edge-executor |
| 10 | +
|
| 11 | +pub use async_task::Task; |
| 12 | +use core::{ |
| 13 | + fmt, |
| 14 | + panic::{RefUnwindSafe, UnwindSafe}, |
| 15 | +}; |
| 16 | +use derive_more::{Deref, DerefMut}; |
| 17 | + |
| 18 | +#[cfg(feature = "multi_threaded")] |
| 19 | +pub use async_task::FallibleTask; |
| 20 | + |
| 21 | +#[cfg(feature = "async_executor")] |
| 22 | +type ExecutorInner<'a> = async_executor::Executor<'a>; |
| 23 | + |
| 24 | +#[cfg(feature = "async_executor")] |
| 25 | +type LocalExecutorInner<'a> = async_executor::LocalExecutor<'a>; |
| 26 | + |
| 27 | +#[cfg(all(not(feature = "async_executor"), feature = "edge_executor"))] |
| 28 | +type ExecutorInner<'a> = edge_executor::Executor<'a, 64>; |
| 29 | + |
| 30 | +#[cfg(all(not(feature = "async_executor"), feature = "edge_executor"))] |
| 31 | +type LocalExecutorInner<'a> = edge_executor::LocalExecutor<'a, 64>; |
| 32 | + |
| 33 | +/// Wrapper around a multi-threading-aware async executor. |
| 34 | +/// Spawning will generally require tasks to be `Send` and `Sync` to allow multiple |
| 35 | +/// threads to send/receive/advance tasks. |
| 36 | +/// |
| 37 | +/// If you require an executor _without_ the `Send` and `Sync` requirements, consider |
| 38 | +/// using [`LocalExecutor`] instead. |
| 39 | +#[derive(Deref, DerefMut, Default)] |
| 40 | +pub struct Executor<'a>(ExecutorInner<'a>); |
| 41 | + |
| 42 | +/// Wrapper around a single-threaded async executor. |
| 43 | +/// Spawning wont generally require tasks to be `Send` and `Sync`, at the cost of |
| 44 | +/// this executor itself not being `Send` or `Sync`. This makes it unsuitable for |
| 45 | +/// global statics. |
| 46 | +/// |
| 47 | +/// If need to store an executor in a global static, or send across threads, |
| 48 | +/// consider using [`Executor`] instead. |
| 49 | +#[derive(Deref, DerefMut, Default)] |
| 50 | +pub struct LocalExecutor<'a>(LocalExecutorInner<'a>); |
| 51 | + |
| 52 | +impl Executor<'_> { |
| 53 | + /// Construct a new [`Executor`] |
| 54 | + #[allow(dead_code, reason = "not all feature flags require this function")] |
| 55 | + pub const fn new() -> Self { |
| 56 | + Self(ExecutorInner::new()) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl LocalExecutor<'_> { |
| 61 | + /// Construct a new [`LocalExecutor`] |
| 62 | + #[allow(dead_code, reason = "not all feature flags require this function")] |
| 63 | + pub const fn new() -> Self { |
| 64 | + Self(LocalExecutorInner::new()) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl UnwindSafe for Executor<'_> {} |
| 69 | +impl RefUnwindSafe for Executor<'_> {} |
| 70 | + |
| 71 | +impl UnwindSafe for LocalExecutor<'_> {} |
| 72 | +impl RefUnwindSafe for LocalExecutor<'_> {} |
| 73 | + |
| 74 | +impl fmt::Debug for Executor<'_> { |
| 75 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 76 | + f.debug_struct("Executor").finish() |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl fmt::Debug for LocalExecutor<'_> { |
| 81 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 82 | + f.debug_struct("LocalExecutor").finish() |
| 83 | + } |
| 84 | +} |
0 commit comments