Skip to content

Commit

Permalink
Restore compatibility with Rust 1.40
Browse files Browse the repository at this point in the history
Rust 1.36 has no f32::from_le_bytes, but Rust 1.40 does. The
MaybeUninit::write is even newer, added in 1.55. That version is recent
enough that I'm not comfortable bumping the MSRV to such a recent
version, but 1.40 should be okay; Debian oldoldstable ships 1.41.1 at
this time.
  • Loading branch information
ruuda committed Sep 25, 2023
1 parent d153729 commit 2fbbe2c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 25 deletions.
4 changes: 2 additions & 2 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Next

**Compatibility**:

* Ensures compatibility with Rust 1.36.0 through 1.72.1. This bumps the minimum
supported Rust version from 1.16 to 1.36.
* Ensures compatibility with Rust 1.40.0 through 1.72.1. This bumps the minimum
supported Rust version from 1.16 to 1.40.

3.5.0
-----
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
# Note, this is the Minimum Supported Rust Version. We build with this by
# default to ensure we don't accidentally break it, but of course you should be
# able to build with more recent versions.
channel = "1.36.0"
channel = "1.40.0"
1 change: 0 additions & 1 deletion src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use std::cmp;
use std::fs;
use std::io;
use std::marker;
use std::mem;
use std::path;
use super::{Error, Result, Sample, SampleFormat, WavSpec, WavSpecEx};

Expand Down
29 changes: 8 additions & 21 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,33 +763,20 @@ impl<'parent, W: io::Write + io::Seek> SampleWriter16<'parent, W> {
/// Note that nothing is actually written until `flush()` is called.
#[inline(always)]
pub fn write_sample<S: Sample>(&mut self, sample: S) {
assert!((self.index as usize) <= self.buffer.len() - 2,
assert!((self.index as usize) + 2 <= self.buffer.len(),
"Trying to write more samples than reserved for the sample writer.");

let s = sample.as_i16() as u16;

// Write the sample in little endian to the buffer.
self.buffer[self.index as usize].write(s as u8);
self.buffer[self.index as usize + 1].write((s >> 8) as u8);

self.index += 2;
// SAFETY: We performed the bounds check in the above assertion.
unsafe { self.write_sample_unchecked(sample) };
}

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.

self.buffer.get_unchecked_mut(self.index as usize).write(value as u8);
self.buffer.get_unchecked_mut(self.index as usize + 1).write((value >> 8) as u8);
}

#[cfg(not(target_arch = "x86_64"))]
unsafe fn write_u16_le_unchecked(&mut self, value: u16) {
// Write a sample in little-endian to the buffer, independent of the
// endianness of the architecture we are running on.
let idx = self.index as usize;
*self.buffer.get_unchecked_mut(idx) = value as u8;
*self.buffer.get_unchecked_mut(idx + 1) = (value >> 8) as u8;
// that merges the following two lines into a single instruction.
*self.buffer.get_unchecked_mut(self.index as usize) = MaybeUninit::new(value as u8);
self.buffer.get_unchecked_mut(self.index as usize).assume_init();
*self.buffer.get_unchecked_mut(self.index as usize + 1) = MaybeUninit::new((value >> 8) as u8);
self.buffer.get_unchecked_mut(self.index as usize + 1).assume_init();
}

/// Like `write_sample()`, but does not perform a bounds check when writing
Expand Down

0 comments on commit 2fbbe2c

Please sign in to comment.