Add Application::Executor associated type

This commit is contained in:
Héctor Ramón Jiménez 2020-01-20 04:47:36 +01:00
parent 35760ac68f
commit 90690702e1
29 changed files with 195 additions and 72 deletions

View file

@ -23,3 +23,7 @@ version = "0.3"
version = "0.2"
optional = true
features = ["rt-core"]
[dependencies.async-std]
version = "1.0"
optional = true

36
futures/src/executor.rs Normal file
View file

@ -0,0 +1,36 @@
//! Choose your preferred executor to power a runtime.
mod null;
#[cfg(feature = "thread-pool")]
mod thread_pool;
#[cfg(feature = "thread-pool")]
pub use thread_pool::ThreadPool;
#[cfg(feature = "tokio")]
mod tokio;
#[cfg(feature = "async-std")]
mod async_std;
pub use null::Null;
#[cfg(feature = "tokio")]
pub use self::tokio::Tokio;
#[cfg(feature = "async-std")]
pub use self::async_std::AsyncStd;
use futures::Future;
pub trait Executor: Sized {
fn new() -> Result<Self, futures::io::Error>
where
Self: Sized;
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static);
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
f()
}
}

View file

@ -0,0 +1,15 @@
use crate::Executor;
use futures::Future;
pub struct AsyncStd;
impl Executor for AsyncStd {
fn new() -> Result<Self, futures::io::Error> {
Ok(Self)
}
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
let _ = async_std::task::spawn(future);
}
}

View file

@ -0,0 +1,13 @@
use crate::Executor;
use futures::Future;
pub struct Null;
impl Executor for Null {
fn new() -> Result<Self, futures::io::Error> {
Ok(Self)
}
fn spawn(&self, _future: impl Future<Output = ()> + Send + 'static) {}
}

View file

@ -0,0 +1,15 @@
use crate::Executor;
use futures::Future;
pub type ThreadPool = futures::executor::ThreadPool;
impl Executor for futures::executor::ThreadPool {
fn new() -> Result<Self, futures::io::Error> {
futures::executor::ThreadPool::new()
}
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
self.spawn_ok(future);
}
}

View file

@ -0,0 +1,19 @@
use crate::Executor;
use futures::Future;
pub type Tokio = tokio::runtime::Runtime;
impl Executor for Tokio {
fn new() -> Result<Self, futures::io::Error> {
tokio::runtime::Runtime::new()
}
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
let _ = tokio::runtime::Runtime::spawn(self, future);
}
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
tokio::runtime::Runtime::enter(self, f)
}
}

View file

@ -1,8 +1,12 @@
mod command;
pub use futures;
pub mod runtime;
mod command;
mod runtime;
pub mod executor;
pub mod subscription;
pub use command::Command;
pub use executor::Executor;
pub use runtime::Runtime;
pub use subscription::Subscription;

View file

@ -1,9 +1,5 @@
//! Run commands and subscriptions.
mod executor;
pub use executor::Executor;
use crate::{subscription, Command, Subscription};
//! Run commands and keep track of subscriptions.
use crate::{subscription, Command, Executor, Subscription};
use futures::Sink;
use std::marker::PhantomData;

View file

@ -1,27 +0,0 @@
use futures::Future;
pub trait Executor {
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static);
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
f()
}
}
#[cfg(feature = "thread-pool")]
impl Executor for futures::executor::ThreadPool {
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
self.spawn_ok(future);
}
}
#[cfg(feature = "tokio")]
impl Executor for tokio::runtime::Runtime {
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
let _ = tokio::runtime::Runtime::spawn(self, future);
}
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
tokio::runtime::Runtime::enter(self, f)
}
}