NULLx76 / ringbuffer

A fixed-size circular buffer written in Rust.
https://crates.io/crates/ringbuffer
MIT License
95 stars 20 forks source link

Break in v0.13->v0.14 #116

Closed nbrr closed 1 year ago

nbrr commented 1 year ago

Studying this piece of code from audio-visualizer, I extracted it in my play project as

pub fn setup_stream(config: &StreamConfig, device: &Device, buffer: Arc<Mutex<AllocRingBuffer<f32>>>) -> Stream {
    device
        .build_input_stream(
            config,
            move |data: &[f32], _info| {
                let mut audio_buffer = buffer.lock().unwrap();
                audio_buffer.extend(data.iter().copied());
            },
            move |error: StreamError| eprintln!("error on input audio stream: {}", error),
        None
        )
        .unwrap()
}

Build is successful with ringbuffer v0.13 but it fails with v0.14:

error[E0277]: `*mut f32` cannot be sent between threads safely
   --> src/lib.rs:24:13
    |
22  |           .build_input_stream(
    |            ------------------ required by a bound introduced by this call
23  |               config,
24  | /             move |data: &[f32], _info| {
25  | |                 let mut audio_buffer = buffer.lock().unwrap();
26  | |                 audio_buffer.extend(data.iter().copied());
27  | |             },
    | |_____________^ `*mut f32` cannot be sent between threads safely
    |
    = help: within `AllocRingBuffer<f32>`, the trait `Send` is not implemented for `*mut f32`
    = note: required because it appears within the type `AllocRingBuffer<f32>`
    = note: required for `Mutex<AllocRingBuffer<f32>>` to implement `Sync`
    = note: required for `Arc<Mutex<AllocRingBuffer<f32>>>` to implement `Send`
note: required because it's used within this closure
   --> src/lib.rs:24:13
    |
24  |             move |data: &[f32], _info| {
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `build_input_stream`
   --> ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cpal-0.15.2/src/traits.rs:134:46
    |
134 |         D: FnMut(&[T], &InputCallbackInfo) + Send + 'static,
    |                                              ^^^^ required by this bound in `DeviceTrait::build_input_stream`

There is no changelog or github tag to compare, but looking at the doc.rs pages for 0.13 and 0.14 what stands out is that it went from Send to !Send. Was that expected?

jdonszelmann commented 1 year ago

You are correct, we switched up some code and did not sufficiently test this. #117 addresses this, by implementing Send and Sync explicitly (which is sound). I'm sorry for the inconvenience, and hope this change solves your problem.

jdonszelmann commented 1 year ago

0.14.1 should fix this.