-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,6 @@ pub use crate::error::{GenericError, Result}; | |
|
||
pub mod client; | ||
pub mod common; | ||
pub mod rt; | ||
|
||
mod error; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |