mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-27 16:51:34 +00:00
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:
@ -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);
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user