From 7b0590e4308c3072b96a82ad47504af0ba326e0c Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 18 Jun 2020 12:08:26 +0200 Subject: [PATCH] impl From for TaskExecutor --- client/cli/src/runner.rs | 28 ++++++++++++---------------- client/service/src/config.rs | 21 ++++++--------------- client/service/test/src/lib.rs | 10 ++++------ utils/browser/src/lib.rs | 4 ++-- 4 files changed, 24 insertions(+), 39 deletions(-) diff --git a/client/cli/src/runner.rs b/client/cli/src/runner.rs index 7aa5a99a7bb42..2aa649ff9746a 100644 --- a/client/cli/src/runner.rs +++ b/client/cli/src/runner.rs @@ -25,9 +25,7 @@ use futures::pin_mut; use futures::select; use futures::{future, future::FutureExt, Future}; use log::info; -use sc_service::{ - AbstractService, Configuration, Role, ServiceBuilderCommand, TaskType, config::TaskExecutor, -}; +use sc_service::{AbstractService, Configuration, Role, ServiceBuilderCommand, TaskType}; use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; use sp_utils::metrics::{TOKIO_THREADS_ALIVE, TOKIO_THREADS_TOTAL}; use sp_version::RuntimeVersion; @@ -121,23 +119,21 @@ impl Runner { let tokio_runtime = build_runtime()?; let runtime_handle = tokio_runtime.handle().clone(); - let task_executor = TaskExecutor::from_fn( - move |fut, task_type| { - match task_type { - TaskType::Async => { runtime_handle.spawn(fut); } - TaskType::Blocking => { - runtime_handle.spawn( async move { - // `spawn_blocking` is looking for the current runtime, and as such has to be called - // from within `spawn`. - tokio::task::spawn_blocking(move || futures::executor::block_on(fut)) - }); - } + let task_executor = move |fut, task_type| { + match task_type { + TaskType::Async => { runtime_handle.spawn(fut); } + TaskType::Blocking => { + runtime_handle.spawn( async move { + // `spawn_blocking` is looking for the current runtime, and as such has to + // be called from within `spawn`. + tokio::task::spawn_blocking(move || futures::executor::block_on(fut)) + }); } } - ); + }; Ok(Runner { - config: command.create_configuration(cli, task_executor)?, + config: command.create_configuration(cli, task_executor.into())?, tokio_runtime, phantom: PhantomData, }) diff --git a/client/service/src/config.rs b/client/service/src/config.rs index 0eb62c55a3e74..2f54f84f717d4 100644 --- a/client/service/src/config.rs +++ b/client/service/src/config.rs @@ -266,14 +266,14 @@ impl std::fmt::Debug for TaskExecutor { } } -/* -impl std::convert::From for TaskExecutor { - fn from(x: TaskExecutorInner) - -> Self { - Self(x) +impl std::convert::From for TaskExecutor +where + F: Fn(Pin + Send>>, TaskType) + Send + Sync + 'static, +{ + fn from(x: F) -> Self { + Self(Arc::new(x)) } } -*/ impl std::ops::Deref for TaskExecutor { type Target = TaskExecutorInner; @@ -282,12 +282,3 @@ impl std::ops::Deref for TaskExecutor { &self.0 } } - -impl TaskExecutor { - /// Create a `TaskExecutor` from a function - pub fn from_fn( - f: impl Fn(Pin + Send>>, TaskType) + Send + Sync + 'static, - ) -> Self { - Self(Arc::new(f)) - } -} diff --git a/client/service/test/src/lib.rs b/client/service/test/src/lib.rs index 0f1dd0f2d64de..932de27c0ed15 100644 --- a/client/service/test/src/lib.rs +++ b/client/service/test/src/lib.rs @@ -255,13 +255,11 @@ impl TestNet where authorities: impl Iterator Result<(F, U), Error>)> ) { let executor = self.runtime.executor(); - let task_executor = { + let task_executor: TaskExecutor = { let executor = executor.clone(); - TaskExecutor::from_fn( - move |fut: Pin + Send>>, _| { - executor.spawn(fut.unit_error().compat()); - }, - ) + (move |fut: Pin + Send>>, _| { + executor.spawn(fut.unit_error().compat()); + }).into() }; for (key, authority) in authorities { diff --git a/utils/browser/src/lib.rs b/utils/browser/src/lib.rs index 8776033acacd9..3e66321a22c76 100644 --- a/utils/browser/src/lib.rs +++ b/utils/browser/src/lib.rs @@ -20,7 +20,7 @@ use log::{debug, info}; use sc_network::config::TransportConfig; use sc_service::{ AbstractService, RpcSession, Role, Configuration, - config::{DatabaseConfig, KeystoreConfig, NetworkConfiguration, TaskExecutor}, + config::{DatabaseConfig, KeystoreConfig, NetworkConfiguration}, GenericChainSpec, RuntimeGenesis }; use wasm_bindgen::prelude::*; @@ -63,7 +63,7 @@ where network, telemetry_endpoints: chain_spec.telemetry_endpoints().clone(), chain_spec: Box::new(chain_spec), - task_executor: TaskExecutor::from_fn(|fut, _| wasm_bindgen_futures::spawn_local(fut)), + task_executor: (|fut, _| wasm_bindgen_futures::spawn_local(fut)).into(), telemetry_external_transport: Some(transport), role: Role::Light, database: {