Skip to content

Commit

Permalink
feat(rt): add TokioExecutor (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkarw authored Jul 22, 2022
1 parent 82660d9 commit 9214294
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ pub use crate::error::{GenericError, Result};

pub mod client;
pub mod common;
pub mod rt;

mod error;
4 changes: 4 additions & 0 deletions src/rt/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! Runtime utilities
/// Implementation of [`hyper::rt::Executor`] that utilises [`tokio::spawn`].
pub mod tokio_executor;
42 changes: 42 additions & 0 deletions src/rt/tokio_executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use hyper::rt::Executor;
use std::future::Future;

/// Future executor that utilises `tokio` threads.
#[non_exhaustive]
#[derive(Default, Debug)]
pub struct TokioExecutor {}

impl<Fut> Executor<Fut> for TokioExecutor
where
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
{
fn execute(&self, fut: Fut) {
tokio::spawn(fut);
}
}

impl TokioExecutor {
/// Create new executor that relies on [`tokio::spawn`] to execute futures.
pub fn new() -> Self {
Self {}
}
}

#[cfg(test)]
mod tests {
use crate::rt::tokio_executor::TokioExecutor;
use hyper::rt::Executor;
use tokio::sync::oneshot;

#[cfg(not(miri))]
#[tokio::test]
async fn simple_execute() -> Result<(), Box<dyn std::error::Error>> {
let (tx, rx) = oneshot::channel();
let executor = TokioExecutor::new();
executor.execute(async move {
tx.send(()).unwrap();
});
rx.await.map_err(Into::into)
}
}

0 comments on commit 9214294

Please sign in to comment.