Skip to content

Commit 9f7ed67

Browse files
committed
wasm32/js: Clarify truncating conversions.
usize and u32 both implement `From<u16>` so use u16 for the length.
1 parent 782223d commit 9f7ed67

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/js.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};
99

1010
// Size of our temporary Uint8Array buffer used with WebCrypto methods
1111
// Maximum is 65536 bytes see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
12-
const WEB_CRYPTO_BUFFER_SIZE: usize = 256;
12+
const WEB_CRYPTO_BUFFER_SIZE: u16 = 256;
1313
// Node.js's crypto.randomFillSync requires the size to be less than 2**31.
1414
const NODE_MAX_BUFFER_SIZE: usize = (1 << 31) - 1;
1515

@@ -50,9 +50,10 @@ pub(crate) fn getrandom_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error>
5050
RngSource::Web(crypto, buf) => {
5151
// getRandomValues does not work with all types of WASM memory,
5252
// so we initially write to browser memory to avoid exceptions.
53-
for chunk in dest.chunks_mut(WEB_CRYPTO_BUFFER_SIZE) {
53+
for chunk in dest.chunks_mut(WEB_CRYPTO_BUFFER_SIZE.into()) {
5454
// The chunk can be smaller than buf's length, so we call to
5555
// JS to create a smaller view of buf without allocation.
56+
#[allow(clippy::cast_possible_truncation)]
5657
let sub_buf = buf.subarray(0, chunk.len() as u32);
5758

5859
if crypto.get_random_values(&sub_buf).is_err() {
@@ -95,7 +96,7 @@ fn getrandom_init() -> Result<RngSource, Error> {
9596
},
9697
};
9798

98-
let buf = Uint8Array::new_with_length(WEB_CRYPTO_BUFFER_SIZE as u32);
99+
let buf = Uint8Array::new_with_length(WEB_CRYPTO_BUFFER_SIZE.into());
99100
Ok(RngSource::Web(crypto, buf))
100101
}
101102

0 commit comments

Comments
 (0)