From 6b2b28f31a9ed28e8c82a0f8746aa0272b3bd119 Mon Sep 17 00:00:00 2001 From: Roman Volosatovs Date: Mon, 20 May 2024 16:14:31 +0200 Subject: [PATCH] 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 | 16 +++++----- examples/rand-no_std.rs | 19 ------------ src/ext/mod.rs | 3 -- 8 files changed, 49 insertions(+), 52 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 (71%) delete mode 100644 examples/rand-no_std.rs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index df3f87b..c233a09 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 c6c1c16..266e5f0 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 0000000..e338185 --- /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 09fc75b..3ab79e6 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 0000000..3428031 --- /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 71% rename from src/ext/rand.rs rename to crates/wasi-ext/src/rand.rs index 1c97fb1..08a49b7 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,21 +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( + 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 5781754..0000000 --- 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 c982618..b581f22 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())