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