Skip to content

Commit 13202f5

Browse files
authored
rand for WASIp3 (#157)
* rand for WASIp3 * only include module under wasi
1 parent 9f3675d commit 13202f5

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub mod io;
6565
pub mod iter;
6666
#[cfg(all(target_os = "wasi", target_env = "p2"))]
6767
pub mod net;
68-
#[cfg(all(target_os = "wasi", target_env = "p2"))]
68+
#[cfg(target_os = "wasi")]
6969
pub mod rand;
7070
#[cfg(all(target_os = "wasi", target_env = "p2"))]
7171
pub mod runtime;

‎src/rand/mod.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Random number generation.
22
3+
#[cfg(target_env = "p2")]
34
use wasip2::random;
5+
#[cfg(target_env = "p3")]
6+
use wasip3::random;
47

58
/// Fill the slice with cryptographically secure random bytes.
69
pub fn get_random_bytes(buf: &mut [u8]) {

‎tests/rand.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// Check that the rand interface works with empty and non-empty buffers.
2+
#[wstd::test]
3+
async fn fills_random_bytes() {
4+
let mut secure = [0; 32];
5+
wstd::rand::get_random_bytes(&mut secure);
6+
assert!(secure.iter().any(|byte| *byte != 0));
7+
8+
let mut insecure = [0; 32];
9+
wstd::rand::get_insecure_random_bytes(&mut insecure);
10+
assert!(insecure.iter().any(|byte| *byte != 0));
11+
12+
wstd::rand::get_random_bytes(&mut []);
13+
wstd::rand::get_insecure_random_bytes(&mut []);
14+
}

0 commit comments

Comments
 (0)