chore: Add a few comments about unsafe usage

This commit is contained in:
Mads Marquart 2021-01-28 20:37:12 +01:00 committed by Mike Welsh
parent 39aa7536df
commit bb4240ad1f
2 changed files with 15 additions and 7 deletions

View File

@ -19,11 +19,12 @@ pub struct SwfStr {
}
impl SwfStr {
/// Create a new `SwfStr` from a byte slice with a given encoding.
/// The string will be truncated if a null byte is encountered.
/// Create a new `SwfStr` from a byte slice.
/// The data is not required to be valid for the given encoding.
#[inline]
pub fn from_bytes(string: &[u8]) -> &Self {
// SAFETY: Casting is safe because internal representations are
// the same, see repr(transparent).
unsafe { &*(string as *const [u8] as *const Self) }
}

View File

@ -689,11 +689,18 @@ impl WebAudioBackend {
.unwrap();
let audio_buffer = Rc::new(RefCell::new(audio_buffer));
let data_array = unsafe { js_sys::Uint8Array::view(&audio_data[..]) };
let array_buffer = data_array.buffer().slice_with_end(
// Clone the audio data into an ArrayBuffer
// SAFETY: (compare with the docs for `Uint8Array::view`)
// - We don't resize WASMs backing buffer before the view is cloned
// - We don't mutate `data_array`
// - Since we clone the buffer, its lifetime is correctly disconnected from `audio_data`
let array_buffer = {
let data_array = unsafe { js_sys::Uint8Array::view(audio_data) };
data_array.buffer().slice_with_end(
data_array.byte_offset(),
data_array.byte_offset() + data_array.byte_length(),
);
)
};
NUM_SOUNDS_LOADING.with(|n| n.set(n.get() + 1));