cpal icon indicating copy to clipboard operation
cpal copied to clipboard

Main crate example doesn't compile

Open AndrejMitrovic opened this issue 2 years ago • 4 comments

From https://docs.rs/cpal/latest/cpal/:

use cpal::Data;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
let stream = device.build_output_stream(
    &config,
    move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
        // react to stream events and read or write stream data here.
    },
    move |err| {
        // react to errors here.
    },
    None // None=blocking, Some(Duration)=timeout
);

This didn't compile for me until I changed &config to &config.into() just like in the examples.

AndrejMitrovic avatar Dec 30 '23 03:12 AndrejMitrovic

PRs to fix the docs are welcome!

est31 avatar Dec 30 '23 04:12 est31

Is there a way to make the example code part of the CI so we don't get regressions?

AndrejMitrovic avatar Dec 30 '23 07:12 AndrejMitrovic

Yeah one can use doctests for that. Right now the relevant code sections are tagged with no_run, which should already catch the issue, but I suppose that CI doesn't run doctests right now. I think it might make sense to first focus on fixing the examples, and then make a second PR to add running doctests to CI (apparently they are not).

est31 avatar Dec 31 '23 00:12 est31

The rustdoc actually compiles the test just fine and is already in CI, but only because it actually has the .into() in a hidden line. It's in line 62 of lib.rs. Here's the full example, omitting the //!.

use cpal::Data;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
# let host = cpal::default_host();
# let device = host.default_output_device().unwrap();
# let config = device.default_output_config().unwrap().into();
let stream = device.build_output_stream(
    &config,
    move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
        // react to stream events and read or write stream data here.
    },
    move |err| {
        // react to errors here.
    },
    None // None=blocking, Some(Duration)=timeout
);

LucasFA avatar Jan 25 '24 23:01 LucasFA