@@ -18,43 +18,46 @@ const MAX_BUFFER_SIZE: u16 = 256;
1818pub fn fill_inner ( dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
1919 CRYPTO . with ( |crypto| {
2020 let crypto = crypto. as_ref ( ) . ok_or ( Error :: WEB_CRYPTO ) ?;
21+ inner ( crypto, dest)
22+ } )
23+ }
2124
22- #[ cfg( not( target_feature = "atomics" ) ) ]
23- {
24- for chunk in dest. chunks_mut ( MAX_BUFFER_SIZE . into ( ) ) {
25- if crypto. get_random_values ( chunk) . is_err ( ) {
26- return Err ( Error :: WEB_GET_RANDOM_VALUES ) ;
27- }
28- }
25+ #[ cfg( not( target_feature = "atomics" ) ) ]
26+ fn inner ( crypto : & Crypto , dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
27+ for chunk in dest. chunks_mut ( MAX_BUFFER_SIZE . into ( ) ) {
28+ if crypto. get_random_values ( chunk) . is_err ( ) {
29+ return Err ( Error :: WEB_GET_RANDOM_VALUES ) ;
2930 }
30- #[ cfg( target_feature = "atomics" ) ]
31- {
32- // getRandomValues does not work with all types of WASM memory,
33- // so we initially write to browser memory to avoid exceptions.
34- let buf = Uint8Array :: new_with_length ( MAX_BUFFER_SIZE . into ( ) ) ;
35- for chunk in dest. chunks_mut ( MAX_BUFFER_SIZE . into ( ) ) {
36- let chunk_len: u32 = chunk
37- . len ( )
38- . try_into ( )
39- . expect ( "chunk length is bounded by MAX_BUFFER_SIZE" ) ;
40- // The chunk can be smaller than buf's length, so we call to
41- // JS to create a smaller view of buf without allocation.
42- let sub_buf = if chunk_len == u32:: from ( MAX_BUFFER_SIZE ) {
43- buf. clone ( )
44- } else {
45- buf. subarray ( 0 , chunk_len)
46- } ;
31+ }
32+ Ok ( ( ) )
33+ }
4734
48- if crypto. get_random_values ( & sub_buf) . is_err ( ) {
49- return Err ( Error :: WEB_GET_RANDOM_VALUES ) ;
50- }
35+ #[ cfg( target_feature = "atomics" ) ]
36+ fn inner ( crypto : & Crypto , dest : & mut [ MaybeUninit < u8 > ] ) -> Result < ( ) , Error > {
37+ // getRandomValues does not work with all types of WASM memory,
38+ // so we initially write to browser memory to avoid exceptions.
39+ let buf = Uint8Array :: new_with_length ( MAX_BUFFER_SIZE . into ( ) ) ;
40+ for chunk in dest. chunks_mut ( MAX_BUFFER_SIZE . into ( ) ) {
41+ let chunk_len: u32 = chunk
42+ . len ( )
43+ . try_into ( )
44+ . expect ( "chunk length is bounded by MAX_BUFFER_SIZE" ) ;
45+ // The chunk can be smaller than buf's length, so we call to
46+ // JS to create a smaller view of buf without allocation.
47+ let sub_buf = if chunk_len == u32:: from ( MAX_BUFFER_SIZE ) {
48+ buf. clone ( )
49+ } else {
50+ buf. subarray ( 0 , chunk_len)
51+ } ;
5152
52- // SAFETY: `sub_buf`'s length is the same length as `chunk`
53- unsafe { sub_buf. raw_copy_to_ptr ( chunk. as_mut_ptr ( ) . cast :: < u8 > ( ) ) } ;
54- }
53+ if crypto. get_random_values ( & sub_buf) . is_err ( ) {
54+ return Err ( Error :: WEB_GET_RANDOM_VALUES ) ;
5555 }
56- Ok ( ( ) )
57- } )
56+
57+ // SAFETY: `sub_buf`'s length is the same length as `chunk`
58+ unsafe { sub_buf. raw_copy_to_ptr ( chunk. as_mut_ptr ( ) . cast :: < u8 > ( ) ) } ;
59+ }
60+ Ok ( ( ) )
5861}
5962
6063#[ wasm_bindgen]
0 commit comments