From dcd8f4ab4445e6bed1d30fc1c8bb7e01e1a72009 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Sat, 7 Dec 2024 18:46:19 +0100 Subject: [PATCH] Remove nesting --- src/backends/wasm_js.rs | 67 +++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/src/backends/wasm_js.rs b/src/backends/wasm_js.rs index 3c1afb07..30113f96 100644 --- a/src/backends/wasm_js.rs +++ b/src/backends/wasm_js.rs @@ -18,43 +18,46 @@ const MAX_BUFFER_SIZE: u16 = 256; pub fn fill_inner(dest: &mut [MaybeUninit]) -> Result<(), Error> { CRYPTO.with(|crypto| { let crypto = crypto.as_ref().ok_or(Error::WEB_CRYPTO)?; + inner(crypto, dest) + }) +} - #[cfg(not(target_feature = "atomics"))] - { - for chunk in dest.chunks_mut(MAX_BUFFER_SIZE.into()) { - if crypto.get_random_values(chunk).is_err() { - return Err(Error::WEB_GET_RANDOM_VALUES); - } - } +#[cfg(not(target_feature = "atomics"))] +fn inner(crypto: &Crypto, dest: &mut [MaybeUninit]) -> Result<(), Error> { + for chunk in dest.chunks_mut(MAX_BUFFER_SIZE.into()) { + if crypto.get_random_values(chunk).is_err() { + return Err(Error::WEB_GET_RANDOM_VALUES); } - #[cfg(target_feature = "atomics")] - { - // getRandomValues does not work with all types of WASM memory, - // so we initially write to browser memory to avoid exceptions. - let buf = Uint8Array::new_with_length(MAX_BUFFER_SIZE.into()); - for chunk in dest.chunks_mut(MAX_BUFFER_SIZE.into()) { - let chunk_len: u32 = chunk - .len() - .try_into() - .expect("chunk length is bounded by MAX_BUFFER_SIZE"); - // The chunk can be smaller than buf's length, so we call to - // JS to create a smaller view of buf without allocation. - let sub_buf = if chunk_len == u32::from(MAX_BUFFER_SIZE) { - buf.clone() - } else { - buf.subarray(0, chunk_len) - }; + } + Ok(()) +} - if crypto.get_random_values(&sub_buf).is_err() { - return Err(Error::WEB_GET_RANDOM_VALUES); - } +#[cfg(target_feature = "atomics")] +fn inner(crypto: &Crypto, dest: &mut [MaybeUninit]) -> Result<(), Error> { + // getRandomValues does not work with all types of WASM memory, + // so we initially write to browser memory to avoid exceptions. + let buf = Uint8Array::new_with_length(MAX_BUFFER_SIZE.into()); + for chunk in dest.chunks_mut(MAX_BUFFER_SIZE.into()) { + let chunk_len: u32 = chunk + .len() + .try_into() + .expect("chunk length is bounded by MAX_BUFFER_SIZE"); + // The chunk can be smaller than buf's length, so we call to + // JS to create a smaller view of buf without allocation. + let sub_buf = if chunk_len == u32::from(MAX_BUFFER_SIZE) { + buf.clone() + } else { + buf.subarray(0, chunk_len) + }; - // SAFETY: `sub_buf`'s length is the same length as `chunk` - unsafe { sub_buf.raw_copy_to_ptr(chunk.as_mut_ptr().cast::()) }; - } + if crypto.get_random_values(&sub_buf).is_err() { + return Err(Error::WEB_GET_RANDOM_VALUES); } - Ok(()) - }) + + // SAFETY: `sub_buf`'s length is the same length as `chunk` + unsafe { sub_buf.raw_copy_to_ptr(chunk.as_mut_ptr().cast::()) }; + } + Ok(()) } #[wasm_bindgen]