Skip to content

Commit

Permalink
switch runtime integrations to executor-trait
Browse files Browse the repository at this point in the history
Signed-off-by: Marc-Antoine Perennou <[email protected]>
  • Loading branch information
Keruspe committed Dec 27, 2020
1 parent 6b2fca8 commit 43b60de
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 103 deletions.
12 changes: 6 additions & 6 deletions async-global-executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ license = "MIT"

[features]
default = ["lapin/default", "async-io"]
async-io = ["async-global-executor/async-io", "async-lapin"]
async-io = ["async-global-executor-trait/async-io", "async-lapin"]

[dependencies]
blocking = "^1.0"

[dependencies.async-global-executor]
version = "^2.0"
[dependencies.async-global-executor-trait]
version = "^1.0"
default-features = false

[dependencies.async-lapin]
Expand All @@ -33,6 +30,9 @@ version = "^1.6"
path = ".."
default-features = false

[dev-dependencies]
async-global-executor = "^2.0"

[dev-dependencies.tracing]
version = "^0.1"
default-features = false
Expand Down
20 changes: 2 additions & 18 deletions async-global-executor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use lapin::{executor::Executor, ConnectionProperties};
use std::{future::Future, pin::Pin};
use lapin::ConnectionProperties;

// ConnectionProperties extension

Expand All @@ -16,26 +15,11 @@ pub trait LapinAsyncGlobalExecutorExt {

impl LapinAsyncGlobalExecutorExt for ConnectionProperties {
fn with_async_global_executor(self) -> Self {
self.with_executor(AsyncGlobalExecutorExecutor)
self.with_executor(async_global_executor_trait::AsyncGlobalExecutor)
}

#[cfg(feature = "async-io")]
fn with_async_io(self) -> Self {
async_lapin::LapinAsyncIoExt::with_async_io(self)
}
}

// Executor

#[derive(Debug)]
struct AsyncGlobalExecutorExecutor;

impl Executor for AsyncGlobalExecutorExecutor {
fn spawn(&self, f: Pin<Box<dyn Future<Output = ()> + Send>>) {
async_global_executor::spawn(f).detach();
}

fn spawn_blocking(&self, f: Box<dyn FnOnce() + Send>) {
async_global_executor::spawn(blocking::unblock(f)).detach();
}
}
9 changes: 4 additions & 5 deletions async-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@ license = "MIT"
[features]
default = ["async-lapin/default"]

[dependencies]
async-executor-trait = "^1.0"

[dependencies.async-lapin]
version = "^1.1"
path = "../async-lapin"
default-features = false

[dependencies.async-std]
version = "^1.7"
features = ["default", "unstable"]

[dependencies.lapin]
version = "^1.6"
path = ".."
default-features = false

[dev-dependencies.async-std]
version = "^1.7"
version = "^1.8"
features = ["attributes", "default", "unstable"]

[dev-dependencies.tracing]
Expand Down
20 changes: 2 additions & 18 deletions async-std/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use async_lapin::LapinAsyncIoExt;
use lapin::{executor::Executor, ConnectionProperties};
use std::{future::Future, pin::Pin};
use lapin::ConnectionProperties;

// ConnectionProperties extension

Expand All @@ -23,26 +22,11 @@ pub trait LapinAsyncStdExt {

impl LapinAsyncStdExt for ConnectionProperties {
fn with_async_std_executor(self) -> Self {
self.with_executor(AsyncStdExecutor)
self.with_executor(async_executor_trait::AsyncStd)
}

fn with_async_std_reactor(self) -> Self {
// async-std uses async-io underneath, use async-io reactor until async-std exposes its own API
self.with_async_io_reactor()
}
}

// Executor

#[derive(Debug)]
struct AsyncStdExecutor;

impl Executor for AsyncStdExecutor {
fn spawn(&self, f: Pin<Box<dyn Future<Output = ()> + Send>>) {
async_std::task::spawn(f);
}

fn spawn_blocking(&self, f: Box<dyn FnOnce() + Send>) {
async_std::task::spawn_blocking(f);
}
}
6 changes: 3 additions & 3 deletions bastion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ license = "MIT"
[features]
default = ["lapin/default"]

[dependencies]
bastion-executor-trait = "^0.4"

[dependencies.lapin]
version = "^1.6"
path = ".."
default-features = false

[dependencies]
bastion-executor = "^0.4"

[dev-dependencies.tracing]
version = "^0.1"
default-features = false
Expand Down
18 changes: 2 additions & 16 deletions bastion/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use lapin::{executor::Executor, ConnectionProperties};
use std::{future::Future, pin::Pin};
use lapin::ConnectionProperties;

pub trait BastionExt {
fn with_bastion(self) -> Self
Expand All @@ -16,19 +15,6 @@ pub trait BastionExt {

impl BastionExt for ConnectionProperties {
fn with_bastion_executor(self) -> Self {
self.with_executor(BastionExecutor)
}
}

#[derive(Debug)]
struct BastionExecutor;

impl Executor for BastionExecutor {
fn spawn(&self, f: Pin<Box<dyn Future<Output = ()> + Send>>) {
bastion_executor::pool::spawn(f, Default::default());
}

fn spawn_blocking(&self, f: Box<dyn FnOnce() + Send>) {
bastion_executor::blocking::spawn_blocking(async move { f() }, Default::default());
self.with_executor(bastion_executor_trait::Bastion)
}
}
7 changes: 4 additions & 3 deletions lapinou/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ license = "MIT"
default = ["async-lapin/default"]

[dependencies]
async-executor = "^1.0"
blocking = "^1.0"
smol = "^1.2.4"
smol-executor-trait = "^1.0"

[dependencies.async-lapin]
version = "^1.1"
Expand All @@ -29,6 +27,9 @@ version = "^1.6"
path = ".."
default-features = false

[dev-dependencies]
smol = "^1.0"

[dev-dependencies.tracing]
version = "^0.1"
default-features = false
Expand Down
20 changes: 2 additions & 18 deletions lapinou/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use async_lapin::LapinAsyncIoExt;
use lapin::{executor::Executor, ConnectionProperties};
use std::{future::Future, pin::Pin};
use lapin::ConnectionProperties;

// ConnectionProperties extension

Expand All @@ -23,25 +22,10 @@ pub trait LapinSmolExt {

impl LapinSmolExt for ConnectionProperties {
fn with_smol_executor(self) -> Self {
self.with_executor(SmolExecutor)
self.with_executor(smol_executor_trait::Smol)
}

fn with_smol_reactor(self) -> Self {
self.with_async_io_reactor()
}
}

// Executor

#[derive(Debug)]
struct SmolExecutor;

impl Executor for SmolExecutor {
fn spawn(&self, f: Pin<Box<dyn Future<Output = ()> + Send>>) {
smol::spawn(f).detach();
}

fn spawn_blocking(&self, f: Box<dyn FnOnce() + Send>) {
smol::spawn(blocking::unblock(f)).detach();
}
}
1 change: 1 addition & 0 deletions tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ features = ["net", "rt", "time"]
async-trait = "^0.1"
futures-io = "^0.3"
parking_lot = "^0.11"
tokio-executor-trait = "^1.0"

[dev-dependencies.tokio]
version = "^1.0"
Expand Down
18 changes: 2 additions & 16 deletions tokio/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use lapin::{executor::Executor, ConnectionProperties};
use std::{future::Future, pin::Pin};
use lapin::ConnectionProperties;
use tokio::runtime::Handle;

pub trait LapinTokioExt {
Expand All @@ -25,7 +24,7 @@ pub trait LapinTokioExt {

impl LapinTokioExt for ConnectionProperties {
fn with_tokio_executor(self) -> Self {
self.with_executor(TokioExecutor(Handle::current()))
self.with_executor(tokio_executor_trait::Tokio::default().with_handle(Handle::current()))
}

#[cfg(unix)]
Expand All @@ -34,19 +33,6 @@ impl LapinTokioExt for ConnectionProperties {
}
}

#[derive(Debug)]
struct TokioExecutor(Handle);

impl Executor for TokioExecutor {
fn spawn(&self, f: Pin<Box<dyn Future<Output = ()> + Send>>) {
self.0.spawn(f);
}

fn spawn_blocking(&self, f: Box<dyn FnOnce() + Send>) {
self.0.spawn_blocking(f);
}
}

#[cfg(unix)]
mod unix {
use async_trait::async_trait;
Expand Down

0 comments on commit 43b60de

Please sign in to comment.