Add explicit web support (#67)

This commit allows async-channel to be used fully in web environments

Signed-off-by: John Nunley <dev@notgull.net>
This commit is contained in:
John Nunley 2023-09-16 21:11:25 -07:00 committed by GitHub
parent 618701fab5
commit 642c52b0ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 12 deletions

View File

@ -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

View File

@ -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"]

View File

@ -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()
}
@ -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()
}
@ -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();
}
@ -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();
}

View File

@ -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;
@ -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)
}
@ -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);
@ -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);
@ -109,6 +114,7 @@ fn try_recv() {
.run();
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn recv() {
let (s, r) = bounded(100);
@ -131,6 +137,7 @@ fn recv() {
.run();
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn try_send() {
let (s, r) = bounded(1);
@ -153,6 +160,7 @@ fn try_send() {
.run();
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn send() {
let (s, r) = bounded(1);
@ -176,6 +184,7 @@ fn send() {
.run();
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn send_after_close() {
let (s, r) = bounded(100);
@ -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);
@ -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;
@ -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);
@ -309,6 +321,7 @@ fn close_wakes_sender() {
.run();
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn close_wakes_receiver() {
let (s, r) = bounded::<()>(1);
@ -324,6 +337,7 @@ fn close_wakes_receiver() {
.run();
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn forget_blocked_sender() {
let (s1, r) = bounded(2);
@ -353,6 +367,7 @@ fn forget_blocked_sender() {
.run();
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn forget_blocked_receiver() {
let (s, r1) = bounded(2);
@ -380,6 +395,7 @@ fn forget_blocked_receiver() {
.run();
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn spsc() {
const COUNT: usize = 100_000;
@ -401,6 +417,7 @@ fn spsc() {
.run();
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn mpmc() {
const COUNT: usize = 25_000;
@ -428,6 +445,7 @@ fn mpmc() {
}
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn mpmc_stream() {
const COUNT: usize = 25_000;
@ -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);

View File

@ -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;
@ -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)
}
@ -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();
@ -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();
@ -97,6 +102,7 @@ fn try_recv() {
.run();
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn recv() {
let (s, r) = unbounded();
@ -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::<()>();
@ -235,6 +242,7 @@ fn close_wakes_receiver() {
.run();
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn spsc() {
const COUNT: usize = 100_000;
@ -256,6 +264,7 @@ fn spsc() {
.run();
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn mpmc() {
const COUNT: usize = 25_000;
@ -285,6 +294,7 @@ fn mpmc() {
}
}
#[cfg(not(target_family = "wasm"))]
#[test]
fn mpmc_stream() {
const COUNT: usize = 25_000;
@ -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>();