executor: add TypedExecutor (#993)

Adds a `TypedExecutor` trait that describes how to spawn futures of a specific
type. This is useful for implementing functions that are generic over an executor
and wish to support both `Send` and `!Send` cases.
This commit is contained in:
Carl Lerche
2019-03-21 14:30:18 -07:00
committed by GitHub
parent cdde2e7a27
commit b1172f8074
19 changed files with 499 additions and 214 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ tokio-codec = { version = "0.1.0", optional = true }
tokio-current-thread = { version = "0.1.3", optional = true }
tokio-fs = { version = "0.1.6", optional = true }
tokio-io = { version = "0.1.6", optional = true }
tokio-executor = { version = "0.1.5", optional = true }
tokio-executor = { version = "0.1.5", optional = true, path = "../tokio-executor" }
tokio-reactor = { version = "0.1.1", optional = true }
tokio-sync = { version = "0.1.3", optional = true }
tokio-threadpool = { version = "0.1.8", optional = true }
+1 -1
View File
@@ -60,7 +60,7 @@ pub mod thread_pool {
};
}
pub use tokio_executor::{Executor, DefaultExecutor, SpawnError};
pub use tokio_executor::{Executor, TypedExecutor, DefaultExecutor, SpawnError};
use futures::{Future, IntoFuture};
use futures::future::{self, FutureResult};
@@ -76,6 +76,15 @@ where T: Future<Item = (), Error = ()> + Send + 'static,
}
}
impl<T> ::executor::TypedExecutor<T> for Handle
where
T: Future<Item = (), Error = ()> + Send + 'static,
{
fn spawn(&mut self, future: T) -> Result<(), ::executor::SpawnError> {
Handle::spawn(self, future)
}
}
/// Error returned by the `run` function.
#[derive(Debug)]
pub struct RunError {
@@ -73,3 +73,12 @@ impl ::executor::Executor for TaskExecutor {
self.inner.spawn(future)
}
}
impl<T> ::executor::TypedExecutor<T> for TaskExecutor
where
T: Future<Item = (), Error = ()> + Send + 'static,
{
fn spawn(&mut self, future: T) -> Result<(), ::executor::SpawnError> {
::executor::Executor::spawn(self, Box::new(future))
}
}