-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(async): Add tokio as an executor option (#36)
This PR is based on the work @alexschrod did in PR #29. All I did was carry it over the finish line. This PR adds a feature to the crate named `tokio-runtime`. If you disable default features and enable this new one, cacache uses tokio as its async executor. This makes integrating cacache with tokio-using projects easier, because the file types leak out if you use anything more than the top-level convenience functions. The PR implements the feature using shims in a new submodule named `async_lib`. This module conditionally uses either async-std or tokio based on feature selection, and hides some differences with convenience functions. This change should not be a breaking change, because the default is still async-std. There are a few other small changes in this PR worth noting. - The README shows how to switch runtimes. - There's a justfile to run common tasks, including those in makefile.toml. The default shell is `sh`, so this might not work out of the box for Windows users. - The tests can now run under either runtime. The justfile has a recipe that runs them both. - The benchmarks can also run under either runtime. The justfile has two recipe for this, one using bench and the other using criterion's runner. - The dependencies now pull in async-attributes by default along with async-std. This made it easier to swap runtimes in the tests. - All dependency versions have been bumped. Co-authored-by: @alexschrod
- Loading branch information
Showing
13 changed files
with
407 additions
and
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# List available just recipes | ||
@help: | ||
just -l | ||
|
||
# Run tests on both runtimes with cargo nextest | ||
@test: | ||
echo "----------\nasync-std:\n" | ||
cargo nextest run | ||
echo "\n----------\ntokio:\n" | ||
cargo nextest run --no-default-features --features tokio-runtime | ||
|
||
# Run benchmarks with `cargo bench` | ||
@bench: | ||
echo "----------\nasync-std:\n" | ||
cargo bench | ||
echo "\n----------\ntokio:\n" | ||
cargo bench --no-default-features --features tokio-runtime | ||
|
||
# Run benchmarks with `cargo criterion` | ||
@criterion: | ||
echo "----------\nasync-std:\n" | ||
cargo criterion | ||
echo "\n----------\ntokio:\n" | ||
cargo criterion --no-default-features --features tokio-runtime | ||
|
||
# Generate a changelog with git-cliff | ||
changelog TAG: | ||
git-cliff --prepend CHANGELOG.md -u --tag {{TAG}} | ||
|
||
# Prepare a release | ||
release *args: | ||
cargo release --workspace {{args}} | ||
|
||
# Install workspace tools | ||
@install-tools: | ||
cargo install cargo-nextest | ||
cargo install cargo-release | ||
cargo install git-cliff | ||
cargo install cargo-criterion | ||
|
||
# Lint and automatically fix what we can fix | ||
@lint: | ||
cargo clippy --fix --allow-dirty --allow-staged | ||
cargo fmt |
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,125 @@ | ||
#[cfg(feature = "async-std")] | ||
pub use async_std::fs::File; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::fs::File; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use futures::io::AsyncRead; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::io::AsyncRead; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use futures::io::AsyncReadExt; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::io::AsyncReadExt; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use futures::io::AsyncBufReadExt; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::io::AsyncBufReadExt; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use futures::io::AsyncWrite; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::io::AsyncWrite; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use futures::io::AsyncWriteExt; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::io::AsyncWriteExt; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use async_std::fs::read; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::fs::read; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use async_std::fs::copy; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::fs::copy; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use async_std::fs::metadata; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::fs::metadata; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use async_std::fs::remove_file; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::fs::remove_file; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use async_std::fs::create_dir_all; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::fs::create_dir_all; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use async_std::fs::remove_dir_all; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::fs::remove_dir_all; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use async_std::fs::DirBuilder; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::fs::DirBuilder; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use async_std::fs::OpenOptions; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::fs::OpenOptions; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use async_std::io::BufReader; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::io::BufReader; | ||
|
||
#[cfg(feature = "async-std")] | ||
#[inline] | ||
pub fn lines_to_stream<R>(lines: futures::io::Lines<R>) -> futures::io::Lines<R> { | ||
lines | ||
} | ||
#[cfg(feature = "tokio")] | ||
#[inline] | ||
pub fn lines_to_stream<R>(lines: tokio::io::Lines<R>) -> tokio_stream::wrappers::LinesStream<R> { | ||
tokio_stream::wrappers::LinesStream::new(lines) | ||
} | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use async_std::task::spawn_blocking; | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::task::spawn_blocking; | ||
|
||
#[cfg(feature = "async-std")] | ||
pub use async_std::task::JoinHandle; | ||
#[cfg(feature = "async-std")] | ||
#[inline] | ||
pub fn unwrap_joinhandle_value<T>(value: T) -> T { | ||
value | ||
} | ||
#[cfg(feature = "tokio")] | ||
pub use tokio::task::JoinHandle; | ||
#[cfg(feature = "tokio")] | ||
#[inline] | ||
pub fn unwrap_joinhandle_value<T>(value: Result<T, tokio::task::JoinError>) -> T { | ||
value.unwrap() | ||
} | ||
|
||
use crate::errors::{Internal, InternalResult}; | ||
use tempfile::NamedTempFile; | ||
|
||
#[cfg(feature = "async-std")] | ||
#[inline] | ||
pub async fn create_named_tempfile(tmp_path: std::path::PathBuf) -> InternalResult<NamedTempFile> { | ||
spawn_blocking(|| NamedTempFile::new_in(tmp_path)) | ||
.await | ||
.to_internal() | ||
} | ||
|
||
#[cfg(feature = "tokio")] | ||
#[inline] | ||
pub async fn create_named_tempfile(tmp_path: std::path::PathBuf) -> InternalResult<NamedTempFile> { | ||
let tmpfile = spawn_blocking(|| NamedTempFile::new_in(tmp_path)) | ||
.await | ||
.to_internal()?; | ||
tmpfile.to_internal() | ||
} |
Oops, something went wrong.