Skip to content

Commit 055a7c2

Browse files
committed
Remove all as casts
1 parent 65ba727 commit 055a7c2

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/backends/wasm_js.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
1313

1414
// Maximum buffer size allowed in `Crypto.getRandomValuesSize` is 65536 bytes.
1515
// See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
16-
const MAX_BUFFER_SIZE: u32 = 65536;
16+
const MAX_BUFFER_SIZE: usize = 65536;
1717

1818
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
1919
CRYPTO.with(|crypto| {
@@ -24,7 +24,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
2424

2525
#[cfg(not(target_feature = "atomics"))]
2626
fn inner(crypto: &Crypto, dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
27-
for chunk in dest.chunks_mut(MAX_BUFFER_SIZE as usize) {
27+
for chunk in dest.chunks_mut(MAX_BUFFER_SIZE) {
2828
if crypto.get_random_values(chunk).is_err() {
2929
return Err(Error::WEB_GET_RANDOM_VALUES);
3030
}
@@ -36,19 +36,19 @@ fn inner(crypto: &Crypto, dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3636
fn inner(crypto: &Crypto, dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3737
// getRandomValues does not work with all types of WASM memory,
3838
// so we initially write to browser memory to avoid exceptions.
39-
let buf_len = u32::min(
40-
dest.len().try_into().unwrap_or(MAX_BUFFER_SIZE),
41-
MAX_BUFFER_SIZE,
42-
);
43-
let buf = Uint8Array::new_with_length(buf_len);
44-
for chunk in dest.chunks_mut(buf_len as usize) {
45-
let chunk_len: u32 = chunk
39+
let buf_len = usize::min(dest.len(), MAX_BUFFER_SIZE);
40+
let buf_len_u32 = buf_len
41+
.try_into()
42+
.expect("buffer length is bounded by MAX_BUFFER_SIZE");
43+
let buf = Uint8Array::new_with_length(buf_len_u32);
44+
for chunk in dest.chunks_mut(buf_len) {
45+
let chunk_len = chunk
4646
.len()
4747
.try_into()
4848
.expect("chunk length is bounded by MAX_BUFFER_SIZE");
4949
// The chunk can be smaller than buf's length, so we call to
5050
// JS to create a smaller view of buf without allocation.
51-
let sub_buf = if chunk_len == buf_len {
51+
let sub_buf = if chunk_len == buf_len_u32 {
5252
&buf
5353
} else {
5454
&buf.subarray(0, chunk_len)

0 commit comments

Comments
 (0)