Merge pull request #803 from JasonLG1979/fix_clippy_warnings

Fix clippy warnings introduced by #797.
This commit is contained in:
Roderick van Domburg 2021-06-18 22:32:55 +02:00 committed by GitHub
commit 79f08c54f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -19,33 +19,33 @@ const NUM_PERIODS: u32 = 4;
#[derive(Debug, Error)] #[derive(Debug, Error)]
enum AlsaError { enum AlsaError {
#[error("AlsaSink, device {device} may be invalid or busy, {err}")] #[error("AlsaSink, device {device} may be invalid or busy, {err}")]
PCMSetUpError { device: String, err: alsa::Error }, PcmSetUp { device: String, err: alsa::Error },
#[error("AlsaSink, device {device} unsupported access type RWInterleaved, {err}")] #[error("AlsaSink, device {device} unsupported access type RWInterleaved, {err}")]
UnsupportedAccessTypeError { device: String, err: alsa::Error }, UnsupportedAccessType { device: String, err: alsa::Error },
#[error("AlsaSink, device {device} unsupported format {format:?}, {err}")] #[error("AlsaSink, device {device} unsupported format {format:?}, {err}")]
UnsupportedFormatError { UnsupportedFormat {
device: String, device: String,
format: AudioFormat, format: AudioFormat,
err: alsa::Error, err: alsa::Error,
}, },
#[error("AlsaSink, device {device} unsupported sample rate {samplerate}, {err}")] #[error("AlsaSink, device {device} unsupported sample rate {samplerate}, {err}")]
UnsupportedSampleRateError { UnsupportedSampleRate {
device: String, device: String,
samplerate: u32, samplerate: u32,
err: alsa::Error, err: alsa::Error,
}, },
#[error("AlsaSink, device {device} unsupported channel count {channel_count}, {err}")] #[error("AlsaSink, device {device} unsupported channel count {channel_count}, {err}")]
UnsupportedChannelCountError { UnsupportedChannelCount {
device: String, device: String,
channel_count: u8, channel_count: u8,
err: alsa::Error, err: alsa::Error,
}, },
#[error("AlsaSink Hardware Parameters Error, {0}")] #[error("AlsaSink Hardware Parameters Error, {0}")]
HwParamsError(alsa::Error), HwParams(alsa::Error),
#[error("AlsaSink Software Parameters Error, {0}")] #[error("AlsaSink Software Parameters Error, {0}")]
SwParamsError(alsa::Error), SwParams(alsa::Error),
#[error("AlsaSink PCM Error, {0}")] #[error("AlsaSink PCM Error, {0}")]
PCMError(alsa::Error), Pcm(alsa::Error),
} }
pub struct AlsaSink { pub struct AlsaSink {
@ -70,10 +70,10 @@ fn list_outputs() -> io::Result<()> {
// mimic aplay -L // mimic aplay -L
let name = a let name = a
.name .name
.ok_or(io::Error::new(io::ErrorKind::Other, "Could not parse name"))?; .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not parse name"))?;
let desc = a let desc = a
.desc .desc
.ok_or(io::Error::new(io::ErrorKind::Other, "Could not parse desc"))?; .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not parse desc"))?;
println!("{}\n\t{}\n", name, desc.replace("\n", "\n\t")); println!("{}\n\t{}\n", name, desc.replace("\n", "\n\t"));
} }
} }
@ -83,8 +83,7 @@ fn list_outputs() -> io::Result<()> {
} }
fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, usize), AlsaError> { fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, usize), AlsaError> {
let pcm = let pcm = PCM::new(dev_name, Direction::Playback, false).map_err(|e| AlsaError::PcmSetUp {
PCM::new(dev_name, Direction::Playback, false).map_err(|e| AlsaError::PCMSetUpError {
device: dev_name.to_string(), device: dev_name.to_string(),
err: e, err: e,
})?; })?;
@ -103,64 +102,56 @@ fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, usize), Alsa
}; };
let bytes_per_period = { let bytes_per_period = {
let hwp = HwParams::any(&pcm).map_err(|e| AlsaError::HwParamsError(e))?; let hwp = HwParams::any(&pcm).map_err(AlsaError::HwParams)?;
hwp.set_access(Access::RWInterleaved).map_err(|e| { hwp.set_access(Access::RWInterleaved)
AlsaError::UnsupportedAccessTypeError { .map_err(|e| AlsaError::UnsupportedAccessType {
device: dev_name.to_string(), device: dev_name.to_string(),
err: e, err: e,
}
})?; })?;
hwp.set_format(alsa_format) hwp.set_format(alsa_format)
.map_err(|e| AlsaError::UnsupportedFormatError { .map_err(|e| AlsaError::UnsupportedFormat {
device: dev_name.to_string(), device: dev_name.to_string(),
format: format, format,
err: e, err: e,
})?; })?;
hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest).map_err(|e| { hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest).map_err(|e| {
AlsaError::UnsupportedSampleRateError { AlsaError::UnsupportedSampleRate {
device: dev_name.to_string(), device: dev_name.to_string(),
samplerate: SAMPLE_RATE, samplerate: SAMPLE_RATE,
err: e, err: e,
} }
})?; })?;
hwp.set_channels(NUM_CHANNELS as u32).map_err(|e| { hwp.set_channels(NUM_CHANNELS as u32)
AlsaError::UnsupportedChannelCountError { .map_err(|e| AlsaError::UnsupportedChannelCount {
device: dev_name.to_string(), device: dev_name.to_string(),
channel_count: NUM_CHANNELS, channel_count: NUM_CHANNELS,
err: e, err: e,
}
})?; })?;
// Deal strictly in time and periods. // Deal strictly in time and periods.
hwp.set_periods(NUM_PERIODS, ValueOr::Nearest) hwp.set_periods(NUM_PERIODS, ValueOr::Nearest)
.map_err(|e| AlsaError::HwParamsError(e))?; .map_err(AlsaError::HwParams)?;
hwp.set_period_time_near(PERIOD_TIME.as_micros() as u32, ValueOr::Nearest) hwp.set_period_time_near(PERIOD_TIME.as_micros() as u32, ValueOr::Nearest)
.map_err(|e| AlsaError::HwParamsError(e))?; .map_err(AlsaError::HwParams)?;
pcm.hw_params(&hwp).map_err(|e| AlsaError::PCMError(e))?; pcm.hw_params(&hwp).map_err(AlsaError::Pcm)?;
let swp = pcm let swp = pcm.sw_params_current().map_err(AlsaError::Pcm)?;
.sw_params_current()
.map_err(|e| AlsaError::PCMError(e))?;
// Don't assume we got what we wanted. // Don't assume we got what we wanted.
// Ask to make sure. // Ask to make sure.
let frames_per_period = hwp let frames_per_period = hwp.get_period_size().map_err(AlsaError::HwParams)?;
.get_period_size()
.map_err(|e| AlsaError::HwParamsError(e))?;
let frames_per_buffer = hwp let frames_per_buffer = hwp.get_buffer_size().map_err(AlsaError::HwParams)?;
.get_buffer_size()
.map_err(|e| AlsaError::HwParamsError(e))?;
swp.set_start_threshold(frames_per_buffer - frames_per_period) swp.set_start_threshold(frames_per_buffer - frames_per_period)
.map_err(|e| AlsaError::SwParamsError(e))?; .map_err(AlsaError::SwParams)?;
pcm.sw_params(&swp).map_err(|e| AlsaError::PCMError(e))?; pcm.sw_params(&swp).map_err(AlsaError::Pcm)?;
// Let ALSA do the math for us. // Let ALSA do the math for us.
pcm.frames_to_bytes(frames_per_period) as usize pcm.frames_to_bytes(frames_per_period) as usize
@ -219,10 +210,9 @@ impl Sink for AlsaSink {
// Write any leftover data in the period buffer // Write any leftover data in the period buffer
// before draining the actual buffer // before draining the actual buffer
self.write_bytes(&[])?; self.write_bytes(&[])?;
let pcm = self.pcm.as_mut().ok_or(io::Error::new( let pcm = self.pcm.as_mut().ok_or_else(|| {
io::ErrorKind::Other, io::Error::new(io::ErrorKind::Other, "Error stopping AlsaSink, PCM is None")
"Error stopping AlsaSink, PCM is None", })?;
))?;
pcm.drain().map_err(|e| { pcm.drain().map_err(|e| {
io::Error::new( io::Error::new(
io::ErrorKind::Other, io::ErrorKind::Other,
@ -262,10 +252,12 @@ impl AlsaSink {
pub const NAME: &'static str = "alsa"; pub const NAME: &'static str = "alsa";
fn write_buf(&mut self) -> io::Result<()> { fn write_buf(&mut self) -> io::Result<()> {
let pcm = self.pcm.as_mut().ok_or(io::Error::new( let pcm = self.pcm.as_mut().ok_or_else(|| {
io::Error::new(
io::ErrorKind::Other, io::ErrorKind::Other,
"Error writing from AlsaSink buffer to PCM, PCM is None", "Error writing from AlsaSink buffer to PCM, PCM is None",
))?; )
})?;
let io = pcm.io_bytes(); let io = pcm.io_bytes();
if let Err(err) = io.writei(&self.period_buffer) { if let Err(err) = io.writei(&self.period_buffer) {
// Capture and log the original error as a warning, and then try to recover. // Capture and log the original error as a warning, and then try to recover.