Skip to content

Commit 79f08c5

Browse files
authored
Merge pull request #803 from JasonLG1979/fix_clippy_warnings
Fix clippy warnings introduced by #797.
2 parents 4c77854 + 5ffce06 commit 79f08c5

1 file changed

Lines changed: 41 additions & 49 deletions

File tree

playback/src/audio_backend/alsa.rs

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,33 @@ const NUM_PERIODS: u32 = 4;
1919
#[derive(Debug, Error)]
2020
enum AlsaError {
2121
#[error("AlsaSink, device {device} may be invalid or busy, {err}")]
22-
PCMSetUpError { device: String, err: alsa::Error },
22+
PcmSetUp { device: String, err: alsa::Error },
2323
#[error("AlsaSink, device {device} unsupported access type RWInterleaved, {err}")]
24-
UnsupportedAccessTypeError { device: String, err: alsa::Error },
24+
UnsupportedAccessType { device: String, err: alsa::Error },
2525
#[error("AlsaSink, device {device} unsupported format {format:?}, {err}")]
26-
UnsupportedFormatError {
26+
UnsupportedFormat {
2727
device: String,
2828
format: AudioFormat,
2929
err: alsa::Error,
3030
},
3131
#[error("AlsaSink, device {device} unsupported sample rate {samplerate}, {err}")]
32-
UnsupportedSampleRateError {
32+
UnsupportedSampleRate {
3333
device: String,
3434
samplerate: u32,
3535
err: alsa::Error,
3636
},
3737
#[error("AlsaSink, device {device} unsupported channel count {channel_count}, {err}")]
38-
UnsupportedChannelCountError {
38+
UnsupportedChannelCount {
3939
device: String,
4040
channel_count: u8,
4141
err: alsa::Error,
4242
},
4343
#[error("AlsaSink Hardware Parameters Error, {0}")]
44-
HwParamsError(alsa::Error),
44+
HwParams(alsa::Error),
4545
#[error("AlsaSink Software Parameters Error, {0}")]
46-
SwParamsError(alsa::Error),
46+
SwParams(alsa::Error),
4747
#[error("AlsaSink PCM Error, {0}")]
48-
PCMError(alsa::Error),
48+
Pcm(alsa::Error),
4949
}
5050

5151
pub struct AlsaSink {
@@ -70,10 +70,10 @@ fn list_outputs() -> io::Result<()> {
7070
// mimic aplay -L
7171
let name = a
7272
.name
73-
.ok_or(io::Error::new(io::ErrorKind::Other, "Could not parse name"))?;
73+
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not parse name"))?;
7474
let desc = a
7575
.desc
76-
.ok_or(io::Error::new(io::ErrorKind::Other, "Could not parse desc"))?;
76+
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not parse desc"))?;
7777
println!("{}\n\t{}\n", name, desc.replace("\n", "\n\t"));
7878
}
7979
}
@@ -83,11 +83,10 @@ fn list_outputs() -> io::Result<()> {
8383
}
8484

8585
fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, usize), AlsaError> {
86-
let pcm =
87-
PCM::new(dev_name, Direction::Playback, false).map_err(|e| AlsaError::PCMSetUpError {
88-
device: dev_name.to_string(),
89-
err: e,
90-
})?;
86+
let pcm = PCM::new(dev_name, Direction::Playback, false).map_err(|e| AlsaError::PcmSetUp {
87+
device: dev_name.to_string(),
88+
err: e,
89+
})?;
9190

9291
let alsa_format = match format {
9392
AudioFormat::F64 => Format::float64(),
@@ -103,64 +102,56 @@ fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, usize), Alsa
103102
};
104103

105104
let bytes_per_period = {
106-
let hwp = HwParams::any(&pcm).map_err(|e| AlsaError::HwParamsError(e))?;
107-
hwp.set_access(Access::RWInterleaved).map_err(|e| {
108-
AlsaError::UnsupportedAccessTypeError {
105+
let hwp = HwParams::any(&pcm).map_err(AlsaError::HwParams)?;
106+
hwp.set_access(Access::RWInterleaved)
107+
.map_err(|e| AlsaError::UnsupportedAccessType {
109108
device: dev_name.to_string(),
110109
err: e,
111-
}
112-
})?;
110+
})?;
113111

114112
hwp.set_format(alsa_format)
115-
.map_err(|e| AlsaError::UnsupportedFormatError {
113+
.map_err(|e| AlsaError::UnsupportedFormat {
116114
device: dev_name.to_string(),
117-
format: format,
115+
format,
118116
err: e,
119117
})?;
120118

121119
hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest).map_err(|e| {
122-
AlsaError::UnsupportedSampleRateError {
120+
AlsaError::UnsupportedSampleRate {
123121
device: dev_name.to_string(),
124122
samplerate: SAMPLE_RATE,
125123
err: e,
126124
}
127125
})?;
128126

129-
hwp.set_channels(NUM_CHANNELS as u32).map_err(|e| {
130-
AlsaError::UnsupportedChannelCountError {
127+
hwp.set_channels(NUM_CHANNELS as u32)
128+
.map_err(|e| AlsaError::UnsupportedChannelCount {
131129
device: dev_name.to_string(),
132130
channel_count: NUM_CHANNELS,
133131
err: e,
134-
}
135-
})?;
132+
})?;
136133

137134
// Deal strictly in time and periods.
138135
hwp.set_periods(NUM_PERIODS, ValueOr::Nearest)
139-
.map_err(|e| AlsaError::HwParamsError(e))?;
136+
.map_err(AlsaError::HwParams)?;
140137

141138
hwp.set_period_time_near(PERIOD_TIME.as_micros() as u32, ValueOr::Nearest)
142-
.map_err(|e| AlsaError::HwParamsError(e))?;
139+
.map_err(AlsaError::HwParams)?;
143140

144-
pcm.hw_params(&hwp).map_err(|e| AlsaError::PCMError(e))?;
141+
pcm.hw_params(&hwp).map_err(AlsaError::Pcm)?;
145142

146-
let swp = pcm
147-
.sw_params_current()
148-
.map_err(|e| AlsaError::PCMError(e))?;
143+
let swp = pcm.sw_params_current().map_err(AlsaError::Pcm)?;
149144

150145
// Don't assume we got what we wanted.
151146
// Ask to make sure.
152-
let frames_per_period = hwp
153-
.get_period_size()
154-
.map_err(|e| AlsaError::HwParamsError(e))?;
147+
let frames_per_period = hwp.get_period_size().map_err(AlsaError::HwParams)?;
155148

156-
let frames_per_buffer = hwp
157-
.get_buffer_size()
158-
.map_err(|e| AlsaError::HwParamsError(e))?;
149+
let frames_per_buffer = hwp.get_buffer_size().map_err(AlsaError::HwParams)?;
159150

160151
swp.set_start_threshold(frames_per_buffer - frames_per_period)
161-
.map_err(|e| AlsaError::SwParamsError(e))?;
152+
.map_err(AlsaError::SwParams)?;
162153

163-
pcm.sw_params(&swp).map_err(|e| AlsaError::PCMError(e))?;
154+
pcm.sw_params(&swp).map_err(AlsaError::Pcm)?;
164155

165156
// Let ALSA do the math for us.
166157
pcm.frames_to_bytes(frames_per_period) as usize
@@ -219,10 +210,9 @@ impl Sink for AlsaSink {
219210
// Write any leftover data in the period buffer
220211
// before draining the actual buffer
221212
self.write_bytes(&[])?;
222-
let pcm = self.pcm.as_mut().ok_or(io::Error::new(
223-
io::ErrorKind::Other,
224-
"Error stopping AlsaSink, PCM is None",
225-
))?;
213+
let pcm = self.pcm.as_mut().ok_or_else(|| {
214+
io::Error::new(io::ErrorKind::Other, "Error stopping AlsaSink, PCM is None")
215+
})?;
226216
pcm.drain().map_err(|e| {
227217
io::Error::new(
228218
io::ErrorKind::Other,
@@ -262,10 +252,12 @@ impl AlsaSink {
262252
pub const NAME: &'static str = "alsa";
263253

264254
fn write_buf(&mut self) -> io::Result<()> {
265-
let pcm = self.pcm.as_mut().ok_or(io::Error::new(
266-
io::ErrorKind::Other,
267-
"Error writing from AlsaSink buffer to PCM, PCM is None",
268-
))?;
255+
let pcm = self.pcm.as_mut().ok_or_else(|| {
256+
io::Error::new(
257+
io::ErrorKind::Other,
258+
"Error writing from AlsaSink buffer to PCM, PCM is None",
259+
)
260+
})?;
269261
let io = pcm.io_bytes();
270262
if let Err(err) = io.writei(&self.period_buffer) {
271263
// Capture and log the original error as a warning, and then try to recover.

0 commit comments

Comments
 (0)