From edecb26ff325e1c905022e0f27dcfe1cb6187c07 Mon Sep 17 00:00:00 2001 From: Greg Wiley Date: Sat, 5 Nov 2022 20:34:08 -0700 Subject: [PATCH] Fix compile issues in wasm-beep example (#723) Fixes #721 Changes to a dependency library broke compilation of the example. Changes needed to get the example working and warning-free include: 1. specifying additional type constraints in local implementations 2. using the `FromSample` trait to convert samples in the handler 3. adding a default matcher in response to the non-exhaustive attribute on `SampleFormat` Also adds a github action to ensure the wasm-beep example compiles. --- .github/workflows/cpal.yml | 14 ++++++++++++++ examples/wasm-beep/src/lib.rs | 8 +++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.github/workflows/cpal.yml b/.github/workflows/cpal.yml index eaf606e..c39eeef 100644 --- a/.github/workflows/cpal.yml +++ b/.github/workflows/cpal.yml @@ -313,3 +313,17 @@ jobs: - name: Build iphonesimulator feedback example run: cd examples/ios-feedback && xcodebuild -scheme cpal-ios-example -configuration Debug -derivedDataPath build -sdk iphonesimulator + wasm-beep-build: + # this only confirms that the Rust source builds + # and checks to prevent regressions like #721. + # + # It does not test the javascript/web integration + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install Target + run: rustup target add wasm32-unknown-unknown + - name: Cargo Build + working-directory: ./examples/wasm-beep + run: cargo build --target wasm32-unknown-unknown + diff --git a/examples/wasm-beep/src/lib.rs b/examples/wasm-beep/src/lib.rs index 0308650..301329d 100644 --- a/examples/wasm-beep/src/lib.rs +++ b/examples/wasm-beep/src/lib.rs @@ -38,12 +38,13 @@ pub fn beep() -> Handle { cpal::SampleFormat::F32 => run::(&device, &config.into()), cpal::SampleFormat::I16 => run::(&device, &config.into()), cpal::SampleFormat::U16 => run::(&device, &config.into()), + _ => panic!("unsupported sample format"), }) } fn run(device: &cpal::Device, config: &cpal::StreamConfig) -> Stream where - T: cpal::Sample, + T: cpal::Sample + cpal::SizedSample + cpal::FromSample, { let sample_rate = config.sample_rate.0 as f32; let channels = config.channels as usize; @@ -71,10 +72,11 @@ where fn write_data(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32) where - T: cpal::Sample, + T: cpal::Sample + cpal::FromSample, { for frame in output.chunks_mut(channels) { - let value: T = cpal::Sample::from::(&next_sample()); + let sample = next_sample(); + let value = T::from_sample::(sample); for sample in frame.iter_mut() { *sample = value; }