Skip to content

Commit

Permalink
Add explicit web support (#67)
Browse files Browse the repository at this point in the history
This commit allows async-channel to be used fully in web environments

Signed-off-by: John Nunley <dev@notgull.net>
  • Loading branch information
notgull authored Sep 17, 2023
1 parent 618701f commit 642c52b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,25 @@ jobs:
- uses: actions/checkout@v4
- name: Install Rust
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
- run: rustup target add wasm32-unknown-unknown
- run: cargo build --all --all-features --all-targets
- name: Run cargo check (without dev-dependencies to catch missing feature flags)
if: startsWith(matrix.rust, 'nightly')
run: cargo check -Z features=dev_dep
- run: cargo test
- run: cargo test --no-default-features
- name: Install cargo-hack
uses: taiki-e/install-action@cargo-hack
- name: Install cargo-hack and wasm-pack
uses: taiki-e/install-action@v2
with:
tool: cargo-hack,wasm-pack
- run: rustup target add thumbv7m-none-eabi
- name: Run cargo check (without dev-dependencies to catch missing feature flags)
run: cargo hack build --all --no-dev-deps
- run: cargo hack build --all --target thumbv7m-none-eabi --no-default-features --no-dev-deps
- name: Run cargo check for WASM
run: cargo check --all --all-features --all-targets --target wasm32-unknown-unknown
- name: Test WASM
run: wasm-pack test --headless --chrome

msrv:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pin-project-lite = "0.2.11"
easy-parallel = "3"
futures-lite = "1"

[target.'cfg(target_family = "wasm")'.dev-dependencies]
wasm-bindgen-test = "0.3.37"

[features]
default = ["std"]
std = ["concurrent-queue/std", "event-listener/std", "event-listener-strategy/std"]
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl<T> Sender<T> {
/// drop(r);
/// assert_eq!(s.send_blocking(2), Err(SendError(2)));
/// ```
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub fn send_blocking(&self, msg: T) -> Result<(), SendError<T>> {
self.send(msg).wait()
}
Expand Down Expand Up @@ -599,7 +599,7 @@ impl<T> Receiver<T> {
/// assert_eq!(r.recv_blocking(), Ok(1));
/// assert_eq!(r.recv_blocking(), Err(RecvError));
/// ```
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub fn recv_blocking(&self) -> Result<T, RecvError> {
self.recv().wait()
}
Expand Down Expand Up @@ -1084,7 +1084,7 @@ easy_wrapper! {
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Send<'a, T>(SendInner<'a, T> => Result<(), SendError<T>>);
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub(crate) wait();
}

Expand Down Expand Up @@ -1134,7 +1134,7 @@ easy_wrapper! {
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Recv<'a, T>(RecvInner<'a, T> => Result<T, RecvError>);
#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
pub(crate) wait();
}

Expand Down
24 changes: 21 additions & 3 deletions tests/bounded.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::bool_assert_comparison, unused_imports)]

use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread::sleep;
Expand All @@ -8,6 +8,10 @@ use async_channel::{bounded, RecvError, SendError, TryRecvError, TrySendError};
use easy_parallel::Parallel;
use futures_lite::{future, prelude::*};

#[cfg(target_family = "wasm")]
use wasm_bindgen_test::wasm_bindgen_test as test;

#[cfg(not(target_family = "wasm"))]
fn ms(ms: u64) -> Duration {
Duration::from_millis(ms)
}
Expand All @@ -25,7 +29,7 @@ fn smoke() {
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
#[test]
fn smoke_blocking() {
let (s, r) = bounded(1);
Expand Down Expand Up @@ -90,6 +94,7 @@ fn len_empty_full() {
assert_eq!(r.is_full(), false);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn try_recv() {
let (s, r) = bounded(100);
Expand All @@ -109,6 +114,7 @@ fn try_recv() {
.run();
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn recv() {
let (s, r) = bounded(100);
Expand All @@ -131,6 +137,7 @@ fn recv() {
.run();
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn try_send() {
let (s, r) = bounded(1);
Expand All @@ -153,6 +160,7 @@ fn try_send() {
.run();
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn send() {
let (s, r) = bounded(1);
Expand All @@ -176,6 +184,7 @@ fn send() {
.run();
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn send_after_close() {
let (s, r) = bounded(100);
Expand All @@ -191,6 +200,7 @@ fn send_after_close() {
assert_eq!(future::block_on(s.send(6)), Err(SendError(6)));
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn recv_after_close() {
let (s, r) = bounded(100);
Expand All @@ -207,6 +217,7 @@ fn recv_after_close() {
assert_eq!(future::block_on(r.recv()), Err(RecvError));
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn len() {
const COUNT: usize = 25_000;
Expand Down Expand Up @@ -293,6 +304,7 @@ fn sender_count() {
assert_eq!(r.receiver_count(), 1);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn close_wakes_sender() {
let (s, r) = bounded(1);
Expand All @@ -309,6 +321,7 @@ fn close_wakes_sender() {
.run();
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn close_wakes_receiver() {
let (s, r) = bounded::<()>(1);
Expand All @@ -324,6 +337,7 @@ fn close_wakes_receiver() {
.run();
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn forget_blocked_sender() {
let (s1, r) = bounded(2);
Expand Down Expand Up @@ -353,6 +367,7 @@ fn forget_blocked_sender() {
.run();
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn forget_blocked_receiver() {
let (s, r1) = bounded(2);
Expand Down Expand Up @@ -380,6 +395,7 @@ fn forget_blocked_receiver() {
.run();
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn spsc() {
const COUNT: usize = 100_000;
Expand All @@ -401,6 +417,7 @@ fn spsc() {
.run();
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn mpmc() {
const COUNT: usize = 25_000;
Expand Down Expand Up @@ -428,6 +445,7 @@ fn mpmc() {
}
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn mpmc_stream() {
const COUNT: usize = 25_000;
Expand Down Expand Up @@ -460,7 +478,7 @@ fn mpmc_stream() {
}
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
#[test]
fn weak() {
let (s, r) = bounded::<usize>(3);
Expand Down
16 changes: 13 additions & 3 deletions tests/unbounded.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::bool_assert_comparison, unused_imports)]

use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread::sleep;
Expand All @@ -8,6 +8,10 @@ use async_channel::{unbounded, RecvError, SendError, TryRecvError, TrySendError}
use easy_parallel::Parallel;
use futures_lite::{future, prelude::*};

#[cfg(target_family = "wasm")]
use wasm_bindgen_test::wasm_bindgen_test as test;

#[cfg(not(target_family = "wasm"))]
fn ms(ms: u64) -> Duration {
Duration::from_millis(ms)
}
Expand All @@ -24,7 +28,7 @@ fn smoke() {
assert_eq!(r.try_recv(), Err(TryRecvError::Empty));
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
#[test]
fn smoke_blocking() {
let (s, r) = unbounded();
Expand Down Expand Up @@ -78,6 +82,7 @@ fn len_empty_full() {
assert_eq!(r.is_full(), false);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn try_recv() {
let (s, r) = unbounded();
Expand All @@ -97,6 +102,7 @@ fn try_recv() {
.run();
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn recv() {
let (s, r) = unbounded();
Expand Down Expand Up @@ -220,6 +226,7 @@ fn sender_count() {
assert_eq!(r.receiver_count(), 1);
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn close_wakes_receiver() {
let (s, r) = unbounded::<()>();
Expand All @@ -235,6 +242,7 @@ fn close_wakes_receiver() {
.run();
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn spsc() {
const COUNT: usize = 100_000;
Expand All @@ -256,6 +264,7 @@ fn spsc() {
.run();
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn mpmc() {
const COUNT: usize = 25_000;
Expand Down Expand Up @@ -285,6 +294,7 @@ fn mpmc() {
}
}

#[cfg(not(target_family = "wasm"))]
#[test]
fn mpmc_stream() {
const COUNT: usize = 25_000;
Expand Down Expand Up @@ -319,7 +329,7 @@ fn mpmc_stream() {
}
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", not(target_family = "wasm")))]
#[test]
fn weak() {
let (s, r) = unbounded::<usize>();
Expand Down

0 comments on commit 642c52b

Please sign in to comment.