Skip to content

Commit 9214294

Browse files
authored
feat(rt): add TokioExecutor (#4)
1 parent 82660d9 commit 9214294

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ pub use crate::error::{GenericError, Result};
55

66
pub mod client;
77
pub mod common;
8+
pub mod rt;
89

910
mod error;

src/rt/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! Runtime utilities
2+
3+
/// Implementation of [`hyper::rt::Executor`] that utilises [`tokio::spawn`].
4+
pub mod tokio_executor;

src/rt/tokio_executor.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use hyper::rt::Executor;
2+
use std::future::Future;
3+
4+
/// Future executor that utilises `tokio` threads.
5+
#[non_exhaustive]
6+
#[derive(Default, Debug)]
7+
pub struct TokioExecutor {}
8+
9+
impl<Fut> Executor<Fut> for TokioExecutor
10+
where
11+
Fut: Future + Send + 'static,
12+
Fut::Output: Send + 'static,
13+
{
14+
fn execute(&self, fut: Fut) {
15+
tokio::spawn(fut);
16+
}
17+
}
18+
19+
impl TokioExecutor {
20+
/// Create new executor that relies on [`tokio::spawn`] to execute futures.
21+
pub fn new() -> Self {
22+
Self {}
23+
}
24+
}
25+
26+
#[cfg(test)]
27+
mod tests {
28+
use crate::rt::tokio_executor::TokioExecutor;
29+
use hyper::rt::Executor;
30+
use tokio::sync::oneshot;
31+
32+
#[cfg(not(miri))]
33+
#[tokio::test]
34+
async fn simple_execute() -> Result<(), Box<dyn std::error::Error>> {
35+
let (tx, rx) = oneshot::channel();
36+
let executor = TokioExecutor::new();
37+
executor.execute(async move {
38+
tx.send(()).unwrap();
39+
});
40+
rx.await.map_err(Into::into)
41+
}
42+
}

0 commit comments

Comments
 (0)