Skip to content

Commit

Permalink
feat(wasi-ext): add workspace crate
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Volosatovs <rvolosatovs@riseup.net>
  • Loading branch information
rvolosatovs committed May 20, 2024
1 parent fd0fcc1 commit 6b2b28f
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 52 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 15 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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 = []
Expand Down Expand Up @@ -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"]
21 changes: 21 additions & 0 deletions crates/wasi-ext/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "wasi-ext"
version = "0.1.0"
authors = ["Roman Volosatovs <rvolosatovs@riseup.net>"]
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"]
4 changes: 2 additions & 2 deletions examples/rand.rs → crates/wasi-ext/examples/rand.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
2 changes: 2 additions & 0 deletions crates/wasi-ext/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(feature = "rand")]
pub mod rand;
16 changes: 8 additions & 8 deletions src/ext/rand.rs → crates/wasi-ext/src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand Down
19 changes: 0 additions & 19 deletions examples/rand-no_std.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/ext/mod.rs
Original file line number Diff line number Diff line change
@@ -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())
Expand Down

0 comments on commit 6b2b28f

Please sign in to comment.