-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add tokio@1 runtime * doc: add `runtime_tokio1` feature to README
- Loading branch information
Showing
9 changed files
with
187 additions
and
4 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
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
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
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
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
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,65 @@ | ||
use crate::join_handle::{InnerJoinHandle, JoinHandle}; | ||
use crate::{AgnostikExecutor, LocalAgnostikExecutor}; | ||
use std::future::Future; | ||
use std::sync::Mutex; | ||
use tokio1_crate as tokio; | ||
|
||
/// A wrapper around the `tokio` (version 1.*) crate which implements `AgnostikExecutor` and | ||
/// `LocalAgnostikExecutor`. | ||
pub struct Tokio1Executor(Mutex<tokio::runtime::Runtime>); | ||
|
||
impl Tokio1Executor { | ||
/// Create a new `Tokio1Executor`. | ||
pub fn new() -> Self { | ||
Self::with_runtime(tokio::runtime::Runtime::new().expect("failed to create runtime")) | ||
} | ||
|
||
/// Create a new `TokioExecutor` with a custom runtime. | ||
pub fn with_runtime(runtime: tokio::runtime::Runtime) -> Self { | ||
Tokio1Executor(Mutex::new(runtime)) | ||
} | ||
|
||
pub(crate) fn set_runtime(&self, runtime: tokio::runtime::Runtime) { | ||
let mut inner = self.0.lock().unwrap(); | ||
*inner = runtime; | ||
} | ||
} | ||
|
||
impl AgnostikExecutor for Tokio1Executor { | ||
fn spawn<F>(&self, future: F) -> JoinHandle<F::Output> | ||
where | ||
F: Future + Send + 'static, | ||
F::Output: Send + 'static, | ||
{ | ||
let handle = tokio::task::spawn(future); | ||
JoinHandle(InnerJoinHandle::Tokio1(handle)) | ||
} | ||
|
||
fn spawn_blocking<F, T>(&self, task: F) -> JoinHandle<T> | ||
where | ||
F: FnOnce() -> T + Send + 'static, | ||
T: Send + 'static, | ||
{ | ||
let handle = tokio::task::spawn_blocking(task); | ||
JoinHandle(InnerJoinHandle::Tokio1(handle)) | ||
} | ||
|
||
fn block_on<F>(&self, future: F) -> F::Output | ||
where | ||
F: Future + Send + 'static, | ||
F::Output: Send + 'static, | ||
{ | ||
self.0.lock().unwrap().block_on(future) | ||
} | ||
} | ||
|
||
impl LocalAgnostikExecutor for Tokio1Executor { | ||
fn spawn_local<F>(&self, future: F) -> JoinHandle<F::Output> | ||
where | ||
F: Future + 'static, | ||
F::Output: 'static, | ||
{ | ||
let handle = tokio::task::spawn_local(future); | ||
JoinHandle(InnerJoinHandle::Tokio1(handle)) | ||
} | ||
} |
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
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
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,47 @@ | ||
pub use agnostik::prelude::*; | ||
pub use tokio1_crate as tokio; | ||
|
||
#[cfg(feature = "runtime_tokio1")] | ||
mod tokio_tests { | ||
use super::*; | ||
#[test] | ||
fn test_tokio() { | ||
agnostik::block_on(async { | ||
agnostik::spawn(async { | ||
let mut i = 0; | ||
while i < 5 { | ||
println!("Counting from Tokio: {}", i); | ||
i += 1; | ||
} | ||
}) | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_basic_scheduler() { | ||
let rt = tokio::runtime::Runtime::new().unwrap(); | ||
let rt = std::sync::Arc::new(rt); | ||
|
||
for _ in 0..100 { | ||
let rt = rt.clone(); | ||
std::thread::spawn(move || { | ||
rt.block_on( | ||
async move { tokio::time::sleep(std::time::Duration::from_secs(1)).await }, | ||
); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(feature = "runtime_tokio1")] | ||
#[test] | ||
fn test_tokio_implicit() { | ||
let res = agnostik::block_on(async { | ||
agnostik::spawn(async { | ||
println!("hello world"); | ||
1 | ||
}) | ||
.await | ||
}); | ||
assert_eq!(res, 1); | ||
} |