Implement Executor for Service and SpawnHandle (#3007)

* Implement Executor for Service and SpawnHandle

* Update lib.rs

* Fix the race condition
This commit is contained in:
Pierre Krieger
2019-07-03 15:36:49 +02:00
committed by Gavin Wood
parent 81d8a5d01d
commit fa535da069
+27 -4
View File
@@ -119,10 +119,17 @@ pub struct SpawnTaskHandle {
sender: mpsc::UnboundedSender<Box<dyn Future<Item = (), Error = ()> + Send>>,
}
impl SpawnTaskHandle {
/// Spawn a task to run the given future.
pub fn spawn_task(&self, task: impl Future<Item = (), Error = ()> + Send + 'static) {
let _ = self.sender.unbounded_send(Box::new(task));
impl Executor<Box<dyn Future<Item = (), Error = ()> + Send>> for SpawnTaskHandle {
fn execute(
&self,
future: Box<dyn Future<Item = (), Error = ()> + Send>
) -> Result<(), futures::future::ExecuteError<Box<dyn Future<Item = (), Error = ()> + Send>>> {
if let Err(err) = self.sender.unbounded_send(future) {
let kind = futures::future::ExecuteErrorKind::Shutdown;
Err(futures::future::ExecuteError::new(kind, err.into_inner()))
} else {
Ok(())
}
}
}
@@ -614,6 +621,22 @@ impl<Components> Future for Service<Components> where Components: components::Co
}
}
impl<Components> Executor<Box<dyn Future<Item = (), Error = ()> + Send>>
for Service<Components> where Components: components::Components
{
fn execute(
&self,
future: Box<dyn Future<Item = (), Error = ()> + Send>
) -> Result<(), futures::future::ExecuteError<Box<dyn Future<Item = (), Error = ()> + Send>>> {
if let Err(err) = self.to_spawn_tx.unbounded_send(future) {
let kind = futures::future::ExecuteErrorKind::Shutdown;
Err(futures::future::ExecuteError::new(kind, err.into_inner()))
} else {
Ok(())
}
}
}
/// Builds a never-ending future that continuously polls the network.
///
/// The `status_sink` contain a list of senders to send a periodic network status to.