Allow any closure to be passed as an executor (#1686)

Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
This commit is contained in:
Thomas Eizinger
2020-09-03 19:28:15 +10:00
committed by GitHub
parent 6a1279407e
commit 3100444085
3 changed files with 27 additions and 36 deletions

View File

@ -633,18 +633,6 @@ impl NetworkConfig {
self
}
/// Shortcut for calling `executor` with an object that calls the given closure.
pub fn set_executor_fn(mut self, f: impl Fn(Pin<Box<dyn Future<Output = ()> + Send>>) + Send + 'static) -> Self {
struct SpawnImpl<F>(F);
impl<F: Fn(Pin<Box<dyn Future<Output = ()> + Send>>)> Executor for SpawnImpl<F> {
fn exec(&self, f: Pin<Box<dyn Future<Output = ()> + Send>>) {
(self.0)(f)
}
}
self.set_executor(Box::new(SpawnImpl(f)));
self
}
pub fn executor(&self) -> Option<&Box<dyn Executor + Send>> {
self.manager_config.executor.as_ref()
}
@ -692,3 +680,24 @@ impl NetworkConfig {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
struct Dummy;
impl Executor for Dummy {
fn exec(&self, _: Pin<Box<dyn Future<Output=()> + Send>>) { }
}
#[test]
fn set_executor() {
NetworkConfig::default()
.set_executor(Box::new(Dummy))
.set_executor(Box::new(|f| {
async_std::task::spawn(f);
}));
}
}