From 04d282ccd6b7ef525d2b01cc3c8f77317c07d51b Mon Sep 17 00:00:00 2001 From: Ian Hobson Date: Fri, 12 May 2023 16:52:06 +0200 Subject: [PATCH] clippy: Use Avoid unnecessary indexing in for loops --- src/fmt.rs | 18 +++++++++--------- src/wavereader.rs | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/fmt.rs b/src/fmt.rs index 2e588c7..f29aabc 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -328,12 +328,12 @@ impl WaveFmt { "frames buffer does not contain a number of samples % channel_count == 0" ); - for n in 0..from_frames.len() { + for frame in from_frames { match (self.valid_bits_per_sample(), self.bits_per_sample) { - (0..=8,8) => write_cursor.write_u8((from_frames[n] + 0x80) as u8 ).unwrap(), // EBU 3285 §A2.2 - (9..=16,16) => write_cursor.write_i16::(from_frames[n] as i16).unwrap(), - (10..=24,24) => write_cursor.write_i24::(from_frames[n]).unwrap(), - (25..=32,32) => write_cursor.write_i32::(from_frames[n]).unwrap(), + (0..=8,8) => write_cursor.write_u8((frame + 0x80) as u8 ).unwrap(), // EBU 3285 §A2.2 + (9..=16,16) => write_cursor.write_i16::(*frame as i16).unwrap(), + (10..=24,24) => write_cursor.write_i24::(*frame).unwrap(), + (25..=32,32) => write_cursor.write_i32::(*frame).unwrap(), (b,_)=> panic!("Unrecognized integer format, bits per sample {}, channels {}, block_alignment {}", b, self.channel_count, self.block_alignment) } @@ -344,8 +344,8 @@ impl WaveFmt { /// Read bytes into frames pub fn unpack_frames(&self, from_bytes: &[u8], into_frames: &mut [i32]) -> () { let mut rdr = Cursor::new(from_bytes); - for n in 0..(into_frames.len()) { - into_frames[n] = match (self.valid_bits_per_sample(), self.bits_per_sample) { + for frame in into_frames { + *frame = match (self.valid_bits_per_sample(), self.bits_per_sample) { (0..=8,8) => rdr.read_u8().unwrap() as i32 - 0x80_i32, // EBU 3285 §A2.2 (9..=16,16) => rdr.read_i16::().unwrap() as i32, (10..=24,24) => rdr.read_i24::().unwrap(), @@ -421,8 +421,8 @@ where ) -> Result { assert!(into.len() % format.channel_count as usize == 0); - for n in 0..(into.len()) { - into[n] = match (format.valid_bits_per_sample(), format.bits_per_sample) { + for frame in into { + *frame = match (format.valid_bits_per_sample(), format.bits_per_sample) { (0..=8,8) => self.read_u8().unwrap() as i32 - 0x80_i32, // EBU 3285 §A2.2 (9..=16,16) => self.read_i16::().unwrap() as i32, (10..=24,24) => self.read_i24::().unwrap(), diff --git a/src/wavereader.rs b/src/wavereader.rs index 317e733..79ba81b 100644 --- a/src/wavereader.rs +++ b/src/wavereader.rs @@ -119,8 +119,8 @@ impl AudioFrameReader { let tell = self.inner.stream_position()?; if (tell - self.start) < self.length { - for n in 0..(self.format.channel_count as usize) { - buffer[n] = match (self.format.bits_per_sample, framed_bits_per_sample) { + for sample in buffer.iter_mut().take(self.format.channel_count as usize) { + *sample = match (self.format.bits_per_sample, framed_bits_per_sample) { (0..=8,8) => self.inner.read_u8()? as i32 - 0x80_i32, // EBU 3285 §A2.2 (9..=16,16) => self.inner.read_i16::()? as i32, (10..=24,24) => self.inner.read_i24::()?, @@ -148,8 +148,8 @@ impl AudioFrameReader { let tell = self.inner.stream_position()?; if (tell - self.start) < self.length { - for n in 0..(self.format.channel_count as usize) { - buffer[n] = match (self.format.bits_per_sample, framed_bits_per_sample) { + for sample in buffer.iter_mut().take(self.format.channel_count as usize) { + *sample = match (self.format.bits_per_sample, framed_bits_per_sample) { (25..=32,32) => self.inner.read_f32::()?, (b,_)=> panic!("Unrecognized integer format, bits per sample {}, channels {}, block_alignment {}", b, self.format.channel_count, self.format.block_alignment)