diff --git a/Cargo.toml b/Cargo.toml index 91520cc..e42777e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,12 +14,14 @@ readme = "README.md" [features] default = ["async-io", "blocking"] +executor-trait = ["async-trait", "executor-trait-crate", "async-task/executor-trait", "blocking"] tokio02 = ["tokio02-crate"] tokio03 = ["tokio03-crate"] [dependencies] async-channel = "^1.5" async-executor = "^1.4" +async-task = "^4.0" async-mutex = "^1.4" futures-lite = "^1.0" num_cpus = "^1.13" @@ -29,10 +31,19 @@ once_cell = "^1.4" version = "^1.2" optional = true +[dependencies.async-trait] +version = "^0.1.42" +optional = true + [dependencies.blocking] version = "^1.0" optional = true +[dependencies.executor-trait-crate] +package = "executor-trait" +version = "^0.5" +optional = true + [dependencies.tokio02-crate] package = "tokio" version = "^0.2" @@ -49,3 +60,6 @@ features = ["rt", "rt-multi-thread"] [dev-dependencies] doc-comment = "^0.3" + +[patch.crates-io] +async-task = { git="https://github.com/Keruspe/async-task", branch="executor-trait" } diff --git a/src/executor_trait.rs b/src/executor_trait.rs new file mode 100644 index 0000000..9fdb529 --- /dev/null +++ b/src/executor_trait.rs @@ -0,0 +1,28 @@ +use async_trait::async_trait; +use executor_trait_crate::{Executor, Task}; +use std::{future::Future, pin::Pin}; + +/// Dummy object implementing executor-trait common interfaces on top of async-global-executor +#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] +pub struct AsyncGlobalExecutor; + +#[async_trait] +impl Executor for AsyncGlobalExecutor { + fn spawn( + &self, + f: Pin + Send>>, + ) -> Box> { + Box::new(crate::spawn(f)) + } + + fn spawn_local(&self, f: Pin>>) -> Box> { + Box::new(crate::spawn_local(f)) + } + + async fn spawn_blocking T + Send + 'static, T: Send + 'static>( + &self, + f: F, + ) -> T { + crate::spawn_blocking(f).await + } +} diff --git a/src/lib.rs b/src/lib.rs index 088d085..7dbcf59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,12 +37,17 @@ pub use executor::{block_on, spawn, spawn_blocking, spawn_local}; pub use init::{init, init_with_config}; pub use threading::{spawn_more_threads, stop_current_thread, stop_thread}; +#[cfg(feature = "executor-trait")] +pub use executor_trait::AsyncGlobalExecutor; + mod config; mod executor; mod init; mod reactor; mod threading; +#[cfg(feature = "executor-trait")] +mod executor_trait; #[cfg(feature = "tokio02")] mod tokio02; #[cfg(feature = "tokio03")]