Skip to content

Commit 4959ccc

Browse files
committed
Make async-std the default
- Change cfg directives to use async-std channels and executor if nothing is specified via rust flags. - Add new empty RUSTFLAGS matrix variant to CI jobs. - Add a justfile for local testing. - Update README.
1 parent 8353890 commit 4959ccc

File tree

12 files changed

+75
-38
lines changed

12 files changed

+75
-38
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ jobs:
2424
strategy:
2525
matrix:
2626
flags:
27-
- --cfg async_executor_impl=\"async-std\" --cfg async_channel_impl=\"async-std\"
27+
# no flags set, default to async-std
28+
- ""
29+
# crates that explicitly choose async-std should keep working
30+
- --cfg async_executor_impl=\"async-std\" --cfg async_channel_impl=\"async-std\"
2831
- --cfg async_executor_impl=\"async-std\" --cfg async_channel_impl=\"flume\"
2932
- --cfg async_executor_impl=\"tokio\" --cfg async_channel_impl=\"tokio\"
3033
- --cfg async_executor_impl=\"tokio\" --cfg async_channel_impl=\"flume\"
@@ -59,7 +62,6 @@ jobs:
5962
runs-on: ubuntu-latest
6063
env:
6164
RUST_LOG: info
62-
RUSTFLAGS: "--cfg async_executor_impl=\"async-std\" --cfg async_channel_impl=\"async-std\""
6365
steps:
6466
- uses: actions/checkout@v4
6567
- uses: katyo/publish-crates@v2

.github/workflows/build_nix.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ jobs:
1010
strategy:
1111
matrix:
1212
flags:
13+
# no flags set, default to async-std
14+
- ""
15+
# crates that explicitly choose async-std should keep working
1316
- --cfg async_executor_impl=\"async-std\" --cfg async_channel_impl=\"async-std\"
1417
- --cfg async_executor_impl=\"async-std\" --cfg async_channel_impl=\"flume\"
1518
- --cfg async_executor_impl=\"tokio\" --cfg async_channel_impl=\"tokio\"

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "async-compatibility-layer"
33
description = "an abstraction layer for using both async-std and tokio"
44
authors = ["Espresso Systems <[email protected]>"]
5-
version = "1.1.0"
5+
version = "1.2.0"
66
edition = "2021"
77
license = "MIT"
88

@@ -41,7 +41,7 @@ opentelemetry-jaeger = { version = "0.19.0", features = [
4141
], optional = true }
4242
opentelemetry-aws = { version = "0.8.0", features = ["trace"], optional = true }
4343

44-
[target.'cfg(all(async_executor_impl = "tokio"))'.dependencies]
44+
[target.'cfg(async_executor_impl = "tokio")'.dependencies]
4545
console-subscriber = { version = "0.2.0" }
4646
tokio = { version = "1", features = [
4747
"fs",
@@ -60,13 +60,13 @@ tokio = { version = "1", features = [
6060
] }
6161
tokio-stream = { version = "0.1.14" }
6262

63-
[target.'cfg(all(async_executor_impl = "async-std"))'.dependencies]
63+
[target.'cfg(not(async_executor_impl = "tokio"))'.dependencies]
6464
async-std = { version = "1.12", features = [
6565
"attributes",
6666
"unstable",
6767
]}
6868

69-
[target.'cfg(all(async_channel_impl = "tokio"))'.dependencies]
69+
[target.'cfg(async_channel_impl = "tokio")'.dependencies]
7070
tokio = { version = "1", features = [
7171
"fs",
7272
"io-util",
@@ -84,7 +84,7 @@ tokio = { version = "1", features = [
8484
] }
8585
tokio-stream = { version = "0.1.14" }
8686

87-
[target.'cfg(all(async_channel_impl = "async-std"))'.dependencies]
87+
[target.'cfg(not(any(async_channel_impl = "tokio", async_channel_impl = "flume")))'.dependencies]
8888
async-std = { version = "1.12", features = [
8989
"attributes",
9090
"unstable",

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@
22

33
This crate exports four things:
44

5-
- A compatibility/abstraction layer for writing async-executor agnostic code. We support two async executors: async-std and tokio. Each may be toggled with a configuration flag.
6-
- A compatibility/abstraction layer for writing async channel agnostic code. We support three async channel implementations: async-std-channels. Each may be toggled with a configuration flag.
5+
- A compatibility/abstraction layer for writing async-executor agnostic code. We
6+
support two async executors: async-std and tokio. Each may be toggled with a
7+
configuration flag.
8+
- A compatibility/abstraction layer for writing async channel agnostic code. We
9+
support three async channel implementations: async-std, tokio and flume. Each
10+
may be toggled with a configuration flag.
711
- A library exporting a bunch of useful async primitives.
812
- A tracing configuration layer optionally supporting console and opentelemetry integration.
913

1014
# Example usage
15+
By default the `async-std` executor and channels are used.
16+
17+
To use tokio:
1118

1219
```bash
1320
RUSTFLAGS='--cfg async_executor_impl="tokio" --cfg async_channel_impl="tokio"' cargo build
1421
```
1522

16-
`async_executor_impl` may be either `tokio` or `async-std`. `async_channel_impl` may be either `tokio`, `async-std`, or `flume`. Note that using `tokio` channels requires `tokio` to be the runtime. Note that the async executor impl and async channel impl must be set in order for this crate to compile successfully.
17-
23+
`async_executor_impl` may be either `tokio` or `async-std`. `async_channel_impl`
24+
may be either `tokio`, `async-std`, or `flume`.
1825

26+
Note that using `tokio` channels requires `tokio` to be the runtime.

justfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
async_std := '--cfg async_executor_impl=\"async-std\" --cfg async_channel_impl=\"async-std\"'
2+
tokio := '--cfg async_executor_impl=\"tokio\" --cfg async_channel_impl=\"tokio\"'
3+
async_std_flume := '--cfg async_executor_impl=\"async-std\" --cfg async_channel_impl=\"flume\"'
4+
tokio_flume := '--cfg async_executor_impl=\"tokio\" --cfg async_channel_impl=\"flume\"'
5+
6+
7+
matrix *args:
8+
{{args}}
9+
env RUSTFLAGS="{{async_std}}" RUSTDOCFLAGS="{{async_std}}" {{args}}
10+
env RUSTFLAGS="{{tokio}}" RUSTDOCFLAGS="{{tokio}}" {{args}}
11+
env RUSTFLAGS="{{async_std_flume}}" RUSTDOCFLAGS="{{async_std_flume}}" {{args}}
12+
env RUSTFLAGS="{{tokio_flume}}" RUSTDOCFLAGS="{{tokio_flume}}" {{args}}
13+
14+
test *args:
15+
cargo test {{args}}
16+
17+
test-all *args:
18+
just matrix just test {{args}}
19+
20+
test-all-logging-utils *args:
21+
just test-all --features logging-utils {{args}}
22+
23+
clippy:
24+
cargo clippy --all-targets --workspace --release --bins --tests --examples --features="logging-utils" -- -D warnings
25+
26+
clippy-all *args:
27+
just matrix just clippy

src/async_primitives/subscribable_mutex.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ use futures::{stream::FuturesOrdered, Future, FutureExt};
55
use std::{fmt, time::Duration};
66
use tracing::warn;
77

8-
#[cfg(async_executor_impl = "async-std")]
8+
#[cfg(not(async_executor_impl = "tokio"))]
99
use async_std::prelude::StreamExt;
1010
#[cfg(async_executor_impl = "tokio")]
1111
use tokio_stream::StreamExt;
12-
#[cfg(not(any(async_executor_impl = "async-std", async_executor_impl = "tokio")))]
13-
std::compile_error! {"The cfg flag async_executor_impl must be set in rustflags to either \"async-std\" or \"tokio\" for this crate. Try adding `--cfg async_executor_impl=\"tokio\""}
1412

1513
/// A mutex that can register subscribers to be notified. This works in the same way as [`Mutex`], but has some additional functions:
1614
///
@@ -254,7 +252,7 @@ mod tests {
254252
async_executor_impl = "tokio",
255253
tokio::test(flavor = "multi_thread", worker_threads = 2)
256254
)]
257-
#[cfg_attr(async_executor_impl = "async-std", async_std::test)]
255+
#[cfg_attr(not(async_executor_impl = "tokio"), async_std::test)]
258256
async fn test_wait_timeout_until() {
259257
let mutex: Arc<SubscribableMutex<usize>> = Arc::default();
260258
{
@@ -279,7 +277,7 @@ mod tests {
279277
async_executor_impl = "tokio",
280278
tokio::test(flavor = "multi_thread", worker_threads = 2)
281279
)]
282-
#[cfg_attr(async_executor_impl = "async-std", async_std::test)]
280+
#[cfg_attr(not(async_executor_impl = "tokio"), async_std::test)]
283281
async fn test_wait_timeout_until_fail() {
284282
let mutex: Arc<SubscribableMutex<usize>> = Arc::default();
285283
{
@@ -303,7 +301,7 @@ mod tests {
303301
async_executor_impl = "tokio",
304302
tokio::test(flavor = "multi_thread", worker_threads = 2)
305303
)]
306-
#[cfg_attr(async_executor_impl = "async-std", async_std::test)]
304+
#[cfg_attr(not(async_executor_impl = "tokio"), async_std::test)]
307305
async fn test_compare_and_set() {
308306
let mutex = SubscribableMutex::new(5usize);
309307
let subscriber = mutex.subscribe().await;
@@ -325,7 +323,7 @@ mod tests {
325323
async_executor_impl = "tokio",
326324
tokio::test(flavor = "multi_thread", worker_threads = 2)
327325
)]
328-
#[cfg_attr(async_executor_impl = "async-std", async_std::test)]
326+
#[cfg_attr(not(async_executor_impl = "tokio"), async_std::test)]
329327
async fn test_subscriber() {
330328
let mutex = SubscribableMutex::new(5usize);
331329
let subscriber = mutex.subscribe().await;

src/channel.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ mod oneshot;
2121
/// Unbounded channels
2222
mod unbounded;
2323

24-
#[cfg(all(async_executor_impl = "async-std", async_channel_impl = "tokio"))]
25-
compile_error!("async_executor_impl = 'async-std-executor' and async_channel_impl = 'channel-tokio' cannot be used together; 'channel-tokio' needs the tokio runtime");
24+
#[cfg(all(not(async_executor_impl = "tokio"), async_channel_impl = "tokio"))]
25+
compile_error!(
26+
"async_channel_impl = 'tokio' requires tokio runtime, e. g. async_executor_impl = 'tokio'"
27+
);
2628

2729
pub use bounded::{bounded, BoundedStream, Receiver, RecvError, SendError, Sender, TryRecvError};
2830
pub use oneshot::{oneshot, OneShotReceiver, OneShotRecvError, OneShotSender, OneShotTryRecvError};

src/channel/bounded.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ mod inner {
7575
}
7676

7777
/// inner module, used to group feature-specific imports
78-
#[cfg(async_channel_impl = "async-std")]
78+
#[cfg(not(any(async_channel_impl = "flume", async_channel_impl = "tokio")))]
7979
mod inner {
8080
pub use async_std::channel::{RecvError, SendError, TryRecvError};
8181

@@ -134,7 +134,7 @@ impl<T> Receiver<T> {
134134
let result = self.0.recv_async().await;
135135
#[cfg(async_channel_impl = "tokio")]
136136
let result = self.0.recv().await.ok_or(RecvError);
137-
#[cfg(async_channel_impl = "async-std")]
137+
#[cfg(not(any(async_channel_impl = "flume", async_channel_impl = "tokio")))]
138138
let result = self.0.recv().await;
139139

140140
result
@@ -144,7 +144,7 @@ impl<T> Receiver<T> {
144144
where
145145
T: 'static,
146146
{
147-
#[cfg(async_channel_impl = "async-std")]
147+
#[cfg(not(any(async_channel_impl = "flume", async_channel_impl = "tokio")))]
148148
let result = self.0;
149149
#[cfg(async_channel_impl = "tokio")]
150150
let result = tokio_stream::wrappers::ReceiverStream::new(self.0);
@@ -226,7 +226,7 @@ impl<T> Stream for BoundedStream<T> {
226226
Pin::new(&mut self.0),
227227
cx,
228228
);
229-
#[cfg(async_channel_impl = "async-std")]
229+
#[cfg(not(any(async_channel_impl = "flume", async_channel_impl = "tokio")))]
230230
return <async_std::channel::Receiver<T> as Stream>::poll_next(Pin::new(&mut self.0), cx);
231231
}
232232
}

src/channel/oneshot.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ mod inner {
6161
}
6262

6363
/// inner module, used to group feature-specific imports
64-
#[cfg(async_channel_impl = "async-std")]
64+
#[cfg(not(any(async_channel_impl = "flume", async_channel_impl = "tokio")))]
6565
mod inner {
6666
use async_std::channel::{Receiver, Sender};
6767
pub use async_std::channel::{
@@ -90,11 +90,11 @@ impl<T> OneShotSender<T> {
9090
///
9191
/// If this fails because the receiver is dropped, a warning will be printed.
9292
pub fn send(self, msg: T) {
93-
#[cfg(async_channel_impl = "async-std")]
93+
#[cfg(not(any(async_channel_impl = "flume", async_channel_impl = "tokio")))]
9494
if self.0.try_send(msg).is_err() {
9595
tracing::warn!("Could not send msg on OneShotSender, did the receiver drop?");
9696
}
97-
#[cfg(not(all(async_channel_impl = "async-std")))]
97+
#[cfg(any(async_channel_impl = "flume", async_channel_impl = "tokio"))]
9898
if self.0.send(msg).is_err() {
9999
tracing::warn!("Could not send msg on OneShotSender, did the receiver drop?");
100100
}
@@ -112,7 +112,7 @@ impl<T> OneShotReceiver<T> {
112112
let result = self.0.await.map_err(Into::into);
113113
#[cfg(async_channel_impl = "flume")]
114114
let result = self.0.recv_async().await;
115-
#[cfg(async_channel_impl = "async-std")]
115+
#[cfg(not(any(async_channel_impl = "flume", async_channel_impl = "tokio")))]
116116
let result = self.0.recv().await;
117117

118118
result

0 commit comments

Comments
 (0)