Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
spacecams committed Sep 8, 2022
1 parent 4a84ba7 commit 5f535fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
21 changes: 10 additions & 11 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub trait ReadExt: io::Read {
// TODO: There is an RFC proposing a method like this for the standard library.
fn read_into(&mut self, buf: &mut [u8]) -> io::Result<()>;

/// Reads `n` bytes and returns them in a vector.
fn read_bytes(&mut self, n: usize) -> io::Result<Vec<u8>>;
/// Reads 4 bytes and returns them in an array.
fn read_4_bytes(&mut self) -> io::Result<[u8; 4]>;

/// Skip over `n` bytes.
fn skip_bytes(&mut self, n: usize) -> io::Result<()>;
Expand Down Expand Up @@ -110,11 +110,8 @@ impl<R> ReadExt for R
}

#[inline(always)]
fn read_bytes(&mut self, n: usize) -> io::Result<Vec<u8>> {
// This method is called with n = 4 bytes for WAVE header data.
// Should not worry about initialization overhead with 4 bytes.
let mut buf = Vec::<u8>::with_capacity(n);
buf.resize(n, 0);
fn read_4_bytes(&mut self) -> io::Result<[u8; 4]> {
let mut buf = [0; 4];
try!(self.read_into(&mut buf[..]));
Ok(buf)
}
Expand Down Expand Up @@ -190,8 +187,10 @@ impl<R> ReadExt for R
}

#[inline(always)]
fn read_le_f32(&mut self) -> io::Result<f32> {
self.read_le_u32().map(|u| unsafe { mem::transmute::<u32, f32>(u) })
fn read_le_f32(&mut self) -> io::Result<f32> {
let mut buf = [0u8; 4];
try!(self.read_into(&mut buf));
Ok(f32::from_le_bytes(buf))
}
}

Expand Down Expand Up @@ -765,14 +764,14 @@ pub fn read_wave_header<R: io::Read>(reader: &mut R) -> Result<u64> {
// into it is more cumbersome, but also avoids a heap allocation. Is
// the compiler smart enough to avoid the heap allocation anyway? I
// would not expect it to be.
if b"RIFF" != &try!(reader.read_bytes(4))[..] {
if b"RIFF" != &try!(reader.read_4_bytes())[..] {
return Err(Error::FormatError("no RIFF tag found"));
}

let file_len = try!(reader.read_le_u32());

// Next four bytes indicate the file type, which should be WAVE.
if b"WAVE" != &try!(reader.read_bytes(4))[..] {
if b"WAVE" != &try!(reader.read_4_bytes())[..] {
return Err(Error::FormatError("no WAVE tag found"));
}

Expand Down
4 changes: 2 additions & 2 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ impl<'parent, W: io::Write + io::Seek> SampleWriter16<'parent, W> {
self.index += 2;
}

unsafe fn write_u16_unchecked(&mut self, value: u16) {
unsafe fn write_u16_le_unchecked(&mut self, value: u16) {
// On little endian machines the compiler produces assembly code
// that merges the following two lines into a single instruction.

Expand All @@ -937,7 +937,7 @@ impl<'parent, W: io::Write + io::Seek> SampleWriter16<'parent, W> {
/// samples are written than allocated when the writer was created.
#[inline(always)]
pub unsafe fn write_sample_unchecked<S: Sample>(&mut self, sample: S) {
self.write_u16_unchecked(sample.as_i16() as u16);
self.write_u16_le_unchecked(sample.as_i16() as u16);
self.index += 2;
}

Expand Down

0 comments on commit 5f535fb

Please sign in to comment.