From 03e63a68e1f194b369f2b04ef9e467d1ed6cc229 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Mon, 13 May 2024 13:08:36 +0200 Subject: [PATCH 1/7] feat(io): implement `Read` and `Write` for streams Signed-off-by: Roman Volosatovs --- examples/cli-command.rs | 7 +++- examples/hello-world.rs | 7 +++- examples/http-proxy.rs | 7 +++- src/{errors.rs => ext/mod.rs} | 6 +-- src/ext/std.rs | 69 +++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- 6 files changed, 88 insertions(+), 10 deletions(-) rename src/{errors.rs => ext/mod.rs} (79%) create mode 100644 src/ext/std.rs diff --git a/examples/cli-command.rs b/examples/cli-command.rs index 809d15a2..e3932ab2 100644 --- a/examples/cli-command.rs +++ b/examples/cli-command.rs @@ -1,11 +1,14 @@ +use std::io::Write as _; + wasi::cli::command::export!(Example); struct Example; impl wasi::exports::cli::run::Guest for Example { fn run() -> Result<(), ()> { - let stdout = wasi::cli::stdout::get_stdout(); - stdout.blocking_write_and_flush(b"Hello, WASI!").unwrap(); + let mut stdout = wasi::cli::stdout::get_stdout(); + stdout.write_all(b"Hello, WASI!").unwrap(); + stdout.flush().unwrap(); Ok(()) } } diff --git a/examples/hello-world.rs b/examples/hello-world.rs index 026e1bc1..62d1210c 100644 --- a/examples/hello-world.rs +++ b/examples/hello-world.rs @@ -1,4 +1,7 @@ +use std::io::Write as _; + fn main() { - let stdout = wasi::cli::stdout::get_stdout(); - stdout.blocking_write_and_flush(b"Hello, world!\n").unwrap(); + let mut stdout = wasi::cli::stdout::get_stdout(); + stdout.write_all(b"Hello, world!\n").unwrap(); + stdout.flush().unwrap(); } diff --git a/examples/http-proxy.rs b/examples/http-proxy.rs index 9a2e54ab..8226e8bc 100644 --- a/examples/http-proxy.rs +++ b/examples/http-proxy.rs @@ -1,3 +1,5 @@ +use std::io::Write as _; + use wasi::http::types::{ Fields, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam, }; @@ -13,8 +15,9 @@ impl wasi::exports::http::incoming_handler::Guest for Example { ResponseOutparam::set(response_out, Ok(resp)); - let out = body.write().unwrap(); - out.blocking_write_and_flush(b"Hello, WASI!").unwrap(); + let mut out = body.write().unwrap(); + out.write_all(b"Hello, WASI!").unwrap(); + out.flush().unwrap(); drop(out); OutgoingBody::finish(body, None).unwrap(); diff --git a/src/errors.rs b/src/ext/mod.rs similarity index 79% rename from src/errors.rs rename to src/ext/mod.rs index 467e6fa7..b581f22d 100644 --- a/src/errors.rs +++ b/src/ext/mod.rs @@ -1,8 +1,8 @@ +#[cfg(feature = "std")] +mod std; + impl core::fmt::Display for crate::io::error::Error { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.write_str(&self.to_debug_string()) } } - -#[cfg(feature = "std")] -impl std::error::Error for crate::io::error::Error {} diff --git a/src/ext/std.rs b/src/ext/std.rs new file mode 100644 index 00000000..bd0178d0 --- /dev/null +++ b/src/ext/std.rs @@ -0,0 +1,69 @@ +use std::error::Error; +use std::io; +use std::num::NonZeroU64; + +use crate::io::streams::StreamError; + +impl Error for crate::io::error::Error {} + +impl io::Read for crate::io::streams::InputStream { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let n = buf + .len() + .try_into() + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + match self.blocking_read(n) { + Ok(chunk) => { + let n = chunk.len(); + if n > buf.len() { + return Err(io::Error::new( + io::ErrorKind::Other, + "more bytes read than requested", + )); + } + buf[..n].copy_from_slice(&chunk); + Ok(n) + } + Err(StreamError::Closed) => Ok(0), + Err(StreamError::LastOperationFailed(e)) => { + Err(io::Error::new(io::ErrorKind::Other, e.to_debug_string())) + } + } + } +} + +impl io::Write for crate::io::streams::OutputStream { + fn write(&mut self, buf: &[u8]) -> io::Result { + let n = loop { + match self.check_write().map(NonZeroU64::new) { + Ok(Some(n)) => { + break n; + } + Ok(None) => { + self.subscribe().block(); + } + Err(StreamError::Closed) => return Ok(0), + Err(StreamError::LastOperationFailed(e)) => { + return Err(io::Error::new(io::ErrorKind::Other, e.to_debug_string())) + } + }; + }; + let n = n + .get() + .try_into() + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + let n = buf.len().min(n); + crate::io::streams::OutputStream::write(self, &buf[..n]).map_err(|e| match e { + StreamError::Closed => io::ErrorKind::UnexpectedEof.into(), + StreamError::LastOperationFailed(e) => { + io::Error::new(io::ErrorKind::Other, e.to_debug_string()) + } + })?; + Ok(n) + } + + fn flush(&mut self) -> io::Result<()> { + self.blocking_flush() + .map_err(|e| io::Error::new(io::ErrorKind::Other, e)) + } +} diff --git a/src/lib.rs b/src/lib.rs index ad02f10e..fa6baceb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -179,7 +179,7 @@ #[cfg(feature = "std")] extern crate std; -mod errors; +mod ext; // These modules are all auto-generated by `./ci/regenerate.sh` mod bindings; From 9825655f7ddb236a2984e27ec92d6699f00956bc Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Mon, 13 May 2024 13:33:21 +0200 Subject: [PATCH 2/7] feat(rand): add `rand` crate integration Signed-off-by: Roman Volosatovs --- Cargo.toml | 7 +++++ examples/rand.rs | 20 ++++++++++++++ src/ext/mod.rs | 3 ++ src/ext/rand.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- 5 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 examples/rand.rs create mode 100644 src/ext/rand.rs diff --git a/Cargo.toml b/Cargo.toml index dd0e9ef5..f39163d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,9 @@ compiler_builtins = { version = "0.1", optional = true } core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" } rustc-std-workspace-alloc = { version = "1.0", optional = true } +# When built with `rand` crate integration +rand = { version = "0.8.5", default-features = false, optional = true } + [features] default = ["std"] std = [] @@ -32,3 +35,7 @@ crate-type = ["cdylib"] [[example]] name = "http-proxy" crate-type = ["cdylib"] + +[[example]] +name = "rand" +required-features = ["rand"] diff --git a/examples/rand.rs b/examples/rand.rs new file mode 100644 index 00000000..63cc85b7 --- /dev/null +++ b/examples/rand.rs @@ -0,0 +1,20 @@ +use std::io::Write as _; + +use wasi::ext::rand::rand::Rng as _; +use wasi::ext::rand::{HostInsecureRng, HostRng}; + +fn main() { + let mut stdout = wasi::cli::stdout::get_stdout(); + + let r: u64 = HostRng.gen(); + stdout + .write_all(format!("Cryptographically-secure random u64 is {r}\n").as_bytes()) + .unwrap(); + + let r: u64 = HostInsecureRng.gen(); + stdout + .write_all(format!("Pseudo-random u64 is {r}\n").as_bytes()) + .unwrap(); + + stdout.flush().unwrap(); +} diff --git a/src/ext/mod.rs b/src/ext/mod.rs index b581f22d..c9826180 100644 --- a/src/ext/mod.rs +++ b/src/ext/mod.rs @@ -1,6 +1,9 @@ #[cfg(feature = "std")] mod std; +#[cfg(feature = "rand")] +pub mod rand; + impl core::fmt::Display for crate::io::error::Error { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.write_str(&self.to_debug_string()) diff --git a/src/ext/rand.rs b/src/ext/rand.rs new file mode 100644 index 00000000..1c97fb10 --- /dev/null +++ b/src/ext/rand.rs @@ -0,0 +1,71 @@ +pub use rand; + +use rand::{CryptoRng, RngCore}; + +/// The secure interface for cryptographically-secure random numbers +pub struct HostRng; + +impl CryptoRng for HostRng {} + +impl RngCore for HostRng { + #[inline] + fn next_u32(&mut self) -> u32 { + crate::random::random::get_random_u64() as _ + } + + #[inline] + fn next_u64(&mut self) -> u64 { + crate::random::random::get_random_u64() + } + + fn fill_bytes(&mut self, dest: &mut [u8]) { + let n = dest.len(); + if usize::BITS <= u64::BITS || n <= u64::MAX as _ { + dest.copy_from_slice(&crate::random::random::get_random_bytes(n as _)); + } else { + let (head, tail) = dest.split_at_mut(u64::MAX as _); + head.copy_from_slice(&crate::random::random::get_random_bytes(u64::MAX)); + self.fill_bytes(tail); + } + } + + #[inline] + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> { + self.fill_bytes(dest); + Ok(()) + } +} + +/// The insecure interface for insecure pseudo-random numbers +pub struct HostInsecureRng; + +impl RngCore for HostInsecureRng { + #[inline] + fn next_u32(&mut self) -> u32 { + crate::random::insecure::get_insecure_random_u64() as _ + } + + #[inline] + fn next_u64(&mut self) -> u64 { + crate::random::insecure::get_insecure_random_u64() + } + + fn fill_bytes(&mut self, dest: &mut [u8]) { + let n = dest.len(); + if usize::BITS <= u64::BITS || n <= u64::MAX as _ { + dest.copy_from_slice(&crate::random::insecure::get_insecure_random_bytes(n as _)); + } else { + let (head, tail) = dest.split_at_mut(u64::MAX as _); + head.copy_from_slice(&crate::random::insecure::get_insecure_random_bytes( + u64::MAX, + )); + self.fill_bytes(tail); + } + } + + #[inline] + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> { + self.fill_bytes(dest); + Ok(()) + } +} diff --git a/src/lib.rs b/src/lib.rs index fa6baceb..7b829c8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -179,7 +179,7 @@ #[cfg(feature = "std")] extern crate std; -mod ext; +pub mod ext; // These modules are all auto-generated by `./ci/regenerate.sh` mod bindings; From 6c5dc3971b05c83b66b17655cf1c9850ff1952b6 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Mon, 13 May 2024 13:50:22 +0200 Subject: [PATCH 3/7] chore: split examples into `std` and `no_std` Signed-off-by: Roman Volosatovs --- Cargo.toml | 20 +++++++++++++++++++- examples/cli-command-no_std.rs | 11 +++++++++++ examples/hello-world-no_std.rs | 4 ++++ examples/http-proxy-no_std.rs | 22 ++++++++++++++++++++++ examples/rand-no_std.rs | 19 +++++++++++++++++++ 5 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 examples/cli-command-no_std.rs create mode 100644 examples/hello-world-no_std.rs create mode 100644 examples/http-proxy-no_std.rs create mode 100644 examples/rand-no_std.rs diff --git a/Cargo.toml b/Cargo.toml index f39163d8..c6c1c161 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,14 +28,32 @@ std = [] # Unstable feature to support being a libstd dependency rustc-dep-of-std = ["compiler_builtins", "core", "rustc-std-workspace-alloc"] +[[example]] +name = "cli-command-no_std" +crate-type = ["cdylib"] + [[example]] name = "cli-command" crate-type = ["cdylib"] +required-features = ["std"] + +[[example]] +name = "hello-world" +required-features = ["std"] + +[[example]] +name = "http-proxy-no_std" +crate-type = ["cdylib"] [[example]] name = "http-proxy" crate-type = ["cdylib"] +required-features = ["std"] [[example]] -name = "rand" +name = "rand-no_std" required-features = ["rand"] + +[[example]] +name = "rand" +required-features = ["std", "rand"] diff --git a/examples/cli-command-no_std.rs b/examples/cli-command-no_std.rs new file mode 100644 index 00000000..809d15a2 --- /dev/null +++ b/examples/cli-command-no_std.rs @@ -0,0 +1,11 @@ +wasi::cli::command::export!(Example); + +struct Example; + +impl wasi::exports::cli::run::Guest for Example { + fn run() -> Result<(), ()> { + let stdout = wasi::cli::stdout::get_stdout(); + stdout.blocking_write_and_flush(b"Hello, WASI!").unwrap(); + Ok(()) + } +} diff --git a/examples/hello-world-no_std.rs b/examples/hello-world-no_std.rs new file mode 100644 index 00000000..026e1bc1 --- /dev/null +++ b/examples/hello-world-no_std.rs @@ -0,0 +1,4 @@ +fn main() { + let stdout = wasi::cli::stdout::get_stdout(); + stdout.blocking_write_and_flush(b"Hello, world!\n").unwrap(); +} diff --git a/examples/http-proxy-no_std.rs b/examples/http-proxy-no_std.rs new file mode 100644 index 00000000..9a2e54ab --- /dev/null +++ b/examples/http-proxy-no_std.rs @@ -0,0 +1,22 @@ +use wasi::http::types::{ + Fields, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam, +}; + +wasi::http::proxy::export!(Example); + +struct Example; + +impl wasi::exports::http::incoming_handler::Guest for Example { + fn handle(_request: IncomingRequest, response_out: ResponseOutparam) { + let resp = OutgoingResponse::new(Fields::new()); + let body = resp.body().unwrap(); + + ResponseOutparam::set(response_out, Ok(resp)); + + let out = body.write().unwrap(); + out.blocking_write_and_flush(b"Hello, WASI!").unwrap(); + drop(out); + + OutgoingBody::finish(body, None).unwrap(); + } +} diff --git a/examples/rand-no_std.rs b/examples/rand-no_std.rs new file mode 100644 index 00000000..57817544 --- /dev/null +++ b/examples/rand-no_std.rs @@ -0,0 +1,19 @@ +use wasi::ext::rand::rand::Rng as _; +use wasi::ext::rand::{HostInsecureRng, HostRng}; + +fn main() { + let stdout = wasi::cli::stdout::get_stdout(); + + let r: u64 = HostRng.gen(); + + stdout + .blocking_write_and_flush( + format!("Cryptographically-secure random u64 is {r}\n").as_bytes(), + ) + .unwrap(); + + let r: u64 = HostInsecureRng.gen(); + stdout + .blocking_write_and_flush(format!("Pseudo-random u64 is {r}\n").as_bytes()) + .unwrap(); +} From 5225d3ae1659ba17c6a3c21d1213352025d7f591 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Mon, 13 May 2024 14:03:06 +0200 Subject: [PATCH 4/7] ci: build all std and no_std examples Signed-off-by: Roman Volosatovs --- .github/workflows/main.yml | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 40268fb0..df3f87b3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -26,16 +26,43 @@ jobs: uses: bytecodealliance/actions/wasm-tools/setup@v1 with: version: "1.202.0" - - run: cargo build --examples --target wasm32-wasi - run: curl -LO https://github.com/bytecodealliance/wasmtime/releases/download/v19.0.0/wasi_snapshot_preview1.command.wasm + + - run: cargo build --examples --target wasm32-wasi --no-default-features + + - run: wasm-tools component new ./target/wasm32-wasi/debug/examples/hello-world-no_std.wasm --adapt ./wasi_snapshot_preview1.command.wasm -o component.wasm + - run: wasmtime run component.wasm + + - run: cargo build --examples --target wasm32-unknown-unknown --no-default-features + + - run: wasm-tools component new ./target/wasm32-unknown-unknown/debug/examples/cli_command_no_std.wasm -o component.wasm + - run: wasmtime run component.wasm + + - run: wasm-tools component new ./target/wasm32-unknown-unknown/debug/examples/http_proxy_no_std.wasm -o component.wasm + - run: wasm-tools component targets wit component.wasm -w wasi:http/proxy + + - run: cargo build --examples --target wasm32-wasi + - run: wasm-tools component new ./target/wasm32-wasi/debug/examples/hello-world.wasm --adapt ./wasi_snapshot_preview1.command.wasm -o component.wasm - run: wasmtime run component.wasm + - run: cargo build --examples --target wasm32-unknown-unknown + - run: wasm-tools component new ./target/wasm32-unknown-unknown/debug/examples/cli_command.wasm -o component.wasm - run: wasmtime run component.wasm + - run: wasm-tools component new ./target/wasm32-unknown-unknown/debug/examples/http_proxy.wasm -o component.wasm - run: wasm-tools component targets wit component.wasm -w wasi:http/proxy + - run: cargo build --examples --target wasm32-wasi --no-default-features --features rand + + - run: wasm-tools component new ./target/wasm32-wasi/debug/examples/rand-no_std.wasm --adapt ./wasi_snapshot_preview1.command.wasm -o component.wasm + - run: wasmtime run component.wasm + + - run: cargo build --examples --target wasm32-wasi --features rand + + - run: wasm-tools component new ./target/wasm32-wasi/debug/examples/rand.wasm --adapt ./wasi_snapshot_preview1.command.wasm -o component.wasm + - run: wasmtime run component.wasm rustfmt: name: Rustfmt From fd0fcc14e3eea1c056d8e185d29a20cfe20aee8a Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Mon, 13 May 2024 16:24:14 +0200 Subject: [PATCH 5/7] refactor(rand): use `writeln` to write to stdout Signed-off-by: Roman Volosatovs --- examples/rand.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/rand.rs b/examples/rand.rs index 63cc85b7..09fc75bf 100644 --- a/examples/rand.rs +++ b/examples/rand.rs @@ -7,14 +7,10 @@ fn main() { let mut stdout = wasi::cli::stdout::get_stdout(); let r: u64 = HostRng.gen(); - stdout - .write_all(format!("Cryptographically-secure random u64 is {r}\n").as_bytes()) - .unwrap(); + writeln!(stdout, "Cryptographically-secure random u64 is {r}").unwrap(); let r: u64 = HostInsecureRng.gen(); - stdout - .write_all(format!("Pseudo-random u64 is {r}\n").as_bytes()) - .unwrap(); + writeln!(stdout, "Pseudo-random u64 is {r}").unwrap(); stdout.flush().unwrap(); } From a65a016337d8adbe1c510e65b98d05a7b9bf42b9 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Mon, 20 May 2024 16:14:31 +0200 Subject: [PATCH 6/7] feat(wasi-ext): add workspace crate Signed-off-by: Roman Volosatovs --- .github/workflows/main.yml | 7 +---- Cargo.toml | 29 ++++++++++--------- crates/wasi-ext/Cargo.toml | 21 ++++++++++++++ .../wasi-ext/examples}/rand.rs | 4 +-- crates/wasi-ext/src/lib.rs | 2 ++ {src/ext => crates/wasi-ext/src}/rand.rs | 18 +++++------- examples/rand-no_std.rs | 19 ------------ src/ext/mod.rs | 3 -- 8 files changed, 49 insertions(+), 54 deletions(-) create mode 100644 crates/wasi-ext/Cargo.toml rename {examples => crates/wasi-ext/examples}/rand.rs (79%) create mode 100644 crates/wasi-ext/src/lib.rs rename {src/ext => crates/wasi-ext/src}/rand.rs (69%) delete mode 100644 examples/rand-no_std.rs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index df3f87b3..c233a09c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -54,12 +54,7 @@ jobs: - run: wasm-tools component new ./target/wasm32-unknown-unknown/debug/examples/http_proxy.wasm -o component.wasm - run: wasm-tools component targets wit component.wasm -w wasi:http/proxy - - run: cargo build --examples --target wasm32-wasi --no-default-features --features rand - - - run: wasm-tools component new ./target/wasm32-wasi/debug/examples/rand-no_std.wasm --adapt ./wasi_snapshot_preview1.command.wasm -o component.wasm - - run: wasmtime run component.wasm - - - run: cargo build --examples --target wasm32-wasi --features rand + - run: cargo build --examples --workspace --target wasm32-wasi --features rand - run: wasm-tools component new ./target/wasm32-wasi/debug/examples/rand.wasm --adapt ./wasi_snapshot_preview1.command.wasm -o component.wasm - run: wasmtime run component.wasm diff --git a/Cargo.toml b/Cargo.toml index c6c1c161..266e5f0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,14 +2,26 @@ name = "wasi" version = "0.13.0+wasi-0.2.0" authors = ["The Cranelift Project Developers"] -license = "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT" description = "WASI API bindings for Rust" -edition = "2021" categories = ["no-std", "wasm"] keywords = ["webassembly", "wasm"] -repository = "https://github.com/bytecodealliance/wasi-rs" readme = "README.md" documentation = "https://docs.rs/wasi" +license.workspace = true +edition.workspace = true +repository.workspace = true + +[workspace.package] +edition = "2021" +license = "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT" +repository = "https://github.com/bytecodealliance/wasi-rs" + +[workspace.dependencies] +rand = { version = "0.8.5", default-features = false } +wasi = { version = "0.13", path = ".", default-features = false } + +[workspace] +members = ["./crates/*"] [dependencies] wit-bindgen-rt = { version = "0.23.0", features = ["bitflags"] } @@ -19,9 +31,6 @@ compiler_builtins = { version = "0.1", optional = true } core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" } rustc-std-workspace-alloc = { version = "1.0", optional = true } -# When built with `rand` crate integration -rand = { version = "0.8.5", default-features = false, optional = true } - [features] default = ["std"] std = [] @@ -49,11 +58,3 @@ crate-type = ["cdylib"] name = "http-proxy" crate-type = ["cdylib"] required-features = ["std"] - -[[example]] -name = "rand-no_std" -required-features = ["rand"] - -[[example]] -name = "rand" -required-features = ["std", "rand"] diff --git a/crates/wasi-ext/Cargo.toml b/crates/wasi-ext/Cargo.toml new file mode 100644 index 00000000..e338185f --- /dev/null +++ b/crates/wasi-ext/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "wasi-ext" +version = "0.1.0" +authors = ["Roman Volosatovs "] +description = "Third-party crate integrations for WASI" + +license.workspace = true +edition.workspace = true +repository.workspace = true + +[features] +default = ["std"] +std = ["wasi/std"] + +[dependencies] +rand = { workspace = true, optional = true } +wasi = { workspace = true } + +[[example]] +name = "rand" +required-features = ["rand", "std"] diff --git a/examples/rand.rs b/crates/wasi-ext/examples/rand.rs similarity index 79% rename from examples/rand.rs rename to crates/wasi-ext/examples/rand.rs index 09fc75bf..3ab79e63 100644 --- a/examples/rand.rs +++ b/crates/wasi-ext/examples/rand.rs @@ -1,7 +1,7 @@ use std::io::Write as _; -use wasi::ext::rand::rand::Rng as _; -use wasi::ext::rand::{HostInsecureRng, HostRng}; +use wasi_ext::rand::rand::Rng as _; +use wasi_ext::rand::{HostInsecureRng, HostRng}; fn main() { let mut stdout = wasi::cli::stdout::get_stdout(); diff --git a/crates/wasi-ext/src/lib.rs b/crates/wasi-ext/src/lib.rs new file mode 100644 index 00000000..3428031e --- /dev/null +++ b/crates/wasi-ext/src/lib.rs @@ -0,0 +1,2 @@ +#[cfg(feature = "rand")] +pub mod rand; diff --git a/src/ext/rand.rs b/crates/wasi-ext/src/rand.rs similarity index 69% rename from src/ext/rand.rs rename to crates/wasi-ext/src/rand.rs index 1c97fb10..f4a34881 100644 --- a/src/ext/rand.rs +++ b/crates/wasi-ext/src/rand.rs @@ -10,21 +10,21 @@ impl CryptoRng for HostRng {} impl RngCore for HostRng { #[inline] fn next_u32(&mut self) -> u32 { - crate::random::random::get_random_u64() as _ + wasi::random::random::get_random_u64() as _ } #[inline] fn next_u64(&mut self) -> u64 { - crate::random::random::get_random_u64() + wasi::random::random::get_random_u64() } fn fill_bytes(&mut self, dest: &mut [u8]) { let n = dest.len(); if usize::BITS <= u64::BITS || n <= u64::MAX as _ { - dest.copy_from_slice(&crate::random::random::get_random_bytes(n as _)); + dest.copy_from_slice(&wasi::random::random::get_random_bytes(n as _)); } else { let (head, tail) = dest.split_at_mut(u64::MAX as _); - head.copy_from_slice(&crate::random::random::get_random_bytes(u64::MAX)); + head.copy_from_slice(&wasi::random::random::get_random_bytes(u64::MAX)); self.fill_bytes(tail); } } @@ -42,23 +42,21 @@ pub struct HostInsecureRng; impl RngCore for HostInsecureRng { #[inline] fn next_u32(&mut self) -> u32 { - crate::random::insecure::get_insecure_random_u64() as _ + wasi::random::insecure::get_insecure_random_u64() as _ } #[inline] fn next_u64(&mut self) -> u64 { - crate::random::insecure::get_insecure_random_u64() + wasi::random::insecure::get_insecure_random_u64() } fn fill_bytes(&mut self, dest: &mut [u8]) { let n = dest.len(); if usize::BITS <= u64::BITS || n <= u64::MAX as _ { - dest.copy_from_slice(&crate::random::insecure::get_insecure_random_bytes(n as _)); + dest.copy_from_slice(&wasi::random::insecure::get_insecure_random_bytes(n as _)); } else { let (head, tail) = dest.split_at_mut(u64::MAX as _); - head.copy_from_slice(&crate::random::insecure::get_insecure_random_bytes( - u64::MAX, - )); + head.copy_from_slice(&wasi::random::insecure::get_insecure_random_bytes(u64::MAX)); self.fill_bytes(tail); } } diff --git a/examples/rand-no_std.rs b/examples/rand-no_std.rs deleted file mode 100644 index 57817544..00000000 --- a/examples/rand-no_std.rs +++ /dev/null @@ -1,19 +0,0 @@ -use wasi::ext::rand::rand::Rng as _; -use wasi::ext::rand::{HostInsecureRng, HostRng}; - -fn main() { - let stdout = wasi::cli::stdout::get_stdout(); - - let r: u64 = HostRng.gen(); - - stdout - .blocking_write_and_flush( - format!("Cryptographically-secure random u64 is {r}\n").as_bytes(), - ) - .unwrap(); - - let r: u64 = HostInsecureRng.gen(); - stdout - .blocking_write_and_flush(format!("Pseudo-random u64 is {r}\n").as_bytes()) - .unwrap(); -} diff --git a/src/ext/mod.rs b/src/ext/mod.rs index c9826180..b581f22d 100644 --- a/src/ext/mod.rs +++ b/src/ext/mod.rs @@ -1,9 +1,6 @@ #[cfg(feature = "std")] mod std; -#[cfg(feature = "rand")] -pub mod rand; - impl core::fmt::Display for crate::io::error::Error { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.write_str(&self.to_debug_string()) From 8adc72919bb8a9aae57dec9089488a59ba393b86 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Mon, 20 May 2024 16:23:24 +0200 Subject: [PATCH 7/7] ci: build in `--workspace` mode Signed-off-by: Roman Volosatovs --- .github/workflows/main.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index c233a09c..da90d70e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,11 +13,11 @@ jobs: - name: Install Rust run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }} && rustup component add rustfmt - run: rustup target add wasm32-wasi wasm32-unknown-unknown - - run: cargo build - - run: cargo build --no-default-features - - run: cargo build --target wasm32-wasi - - run: cargo build --target wasm32-wasi --no-default-features - - run: cargo test --doc + - run: cargo build --workspace + - run: cargo build --workspace --no-default-features + - run: cargo build --workspace --target wasm32-wasi + - run: cargo build --workspace --target wasm32-wasi --no-default-features + - run: cargo test --workspace --doc - name: Install Wasmtime uses: bytecodealliance/actions/wasmtime/setup@v1 with: