Skip to content

Commit 6b2b28f

Browse files
committed
feat(wasi-ext): add workspace crate
Signed-off-by: Roman Volosatovs <[email protected]>
1 parent fd0fcc1 commit 6b2b28f

File tree

8 files changed

+49
-52
lines changed

8 files changed

+49
-52
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ jobs:
5454
- run: wasm-tools component new ./target/wasm32-unknown-unknown/debug/examples/http_proxy.wasm -o component.wasm
5555
- run: wasm-tools component targets wit component.wasm -w wasi:http/proxy
5656

57-
- run: cargo build --examples --target wasm32-wasi --no-default-features --features rand
58-
59-
- run: wasm-tools component new ./target/wasm32-wasi/debug/examples/rand-no_std.wasm --adapt ./wasi_snapshot_preview1.command.wasm -o component.wasm
60-
- run: wasmtime run component.wasm
61-
62-
- run: cargo build --examples --target wasm32-wasi --features rand
57+
- run: cargo build --examples --workspace --target wasm32-wasi --features rand
6358

6459
- run: wasm-tools component new ./target/wasm32-wasi/debug/examples/rand.wasm --adapt ./wasi_snapshot_preview1.command.wasm -o component.wasm
6560
- run: wasmtime run component.wasm

Cargo.toml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,26 @@
22
name = "wasi"
33
version = "0.13.0+wasi-0.2.0"
44
authors = ["The Cranelift Project Developers"]
5-
license = "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT"
65
description = "WASI API bindings for Rust"
7-
edition = "2021"
86
categories = ["no-std", "wasm"]
97
keywords = ["webassembly", "wasm"]
10-
repository = "https://github.com/bytecodealliance/wasi-rs"
118
readme = "README.md"
129
documentation = "https://docs.rs/wasi"
10+
license.workspace = true
11+
edition.workspace = true
12+
repository.workspace = true
13+
14+
[workspace.package]
15+
edition = "2021"
16+
license = "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT"
17+
repository = "https://github.com/bytecodealliance/wasi-rs"
18+
19+
[workspace.dependencies]
20+
rand = { version = "0.8.5", default-features = false }
21+
wasi = { version = "0.13", path = ".", default-features = false }
22+
23+
[workspace]
24+
members = ["./crates/*"]
1325

1426
[dependencies]
1527
wit-bindgen-rt = { version = "0.23.0", features = ["bitflags"] }
@@ -19,9 +31,6 @@ compiler_builtins = { version = "0.1", optional = true }
1931
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }
2032
rustc-std-workspace-alloc = { version = "1.0", optional = true }
2133

22-
# When built with `rand` crate integration
23-
rand = { version = "0.8.5", default-features = false, optional = true }
24-
2534
[features]
2635
default = ["std"]
2736
std = []
@@ -49,11 +58,3 @@ crate-type = ["cdylib"]
4958
name = "http-proxy"
5059
crate-type = ["cdylib"]
5160
required-features = ["std"]
52-
53-
[[example]]
54-
name = "rand-no_std"
55-
required-features = ["rand"]
56-
57-
[[example]]
58-
name = "rand"
59-
required-features = ["std", "rand"]

crates/wasi-ext/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "wasi-ext"
3+
version = "0.1.0"
4+
authors = ["Roman Volosatovs <[email protected]>"]
5+
description = "Third-party crate integrations for WASI"
6+
7+
license.workspace = true
8+
edition.workspace = true
9+
repository.workspace = true
10+
11+
[features]
12+
default = ["std"]
13+
std = ["wasi/std"]
14+
15+
[dependencies]
16+
rand = { workspace = true, optional = true }
17+
wasi = { workspace = true }
18+
19+
[[example]]
20+
name = "rand"
21+
required-features = ["rand", "std"]

examples/rand.rs renamed to crates/wasi-ext/examples/rand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::io::Write as _;
22

3-
use wasi::ext::rand::rand::Rng as _;
4-
use wasi::ext::rand::{HostInsecureRng, HostRng};
3+
use wasi_ext::rand::rand::Rng as _;
4+
use wasi_ext::rand::{HostInsecureRng, HostRng};
55

66
fn main() {
77
let mut stdout = wasi::cli::stdout::get_stdout();

crates/wasi-ext/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[cfg(feature = "rand")]
2+
pub mod rand;

src/ext/rand.rs renamed to crates/wasi-ext/src/rand.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ impl CryptoRng for HostRng {}
1010
impl RngCore for HostRng {
1111
#[inline]
1212
fn next_u32(&mut self) -> u32 {
13-
crate::random::random::get_random_u64() as _
13+
wasi::random::random::get_random_u64() as _
1414
}
1515

1616
#[inline]
1717
fn next_u64(&mut self) -> u64 {
18-
crate::random::random::get_random_u64()
18+
wasi::random::random::get_random_u64()
1919
}
2020

2121
fn fill_bytes(&mut self, dest: &mut [u8]) {
2222
let n = dest.len();
2323
if usize::BITS <= u64::BITS || n <= u64::MAX as _ {
24-
dest.copy_from_slice(&crate::random::random::get_random_bytes(n as _));
24+
dest.copy_from_slice(&wasi::random::random::get_random_bytes(n as _));
2525
} else {
2626
let (head, tail) = dest.split_at_mut(u64::MAX as _);
27-
head.copy_from_slice(&crate::random::random::get_random_bytes(u64::MAX));
27+
head.copy_from_slice(&wasi::random::random::get_random_bytes(u64::MAX));
2828
self.fill_bytes(tail);
2929
}
3030
}
@@ -42,21 +42,21 @@ pub struct HostInsecureRng;
4242
impl RngCore for HostInsecureRng {
4343
#[inline]
4444
fn next_u32(&mut self) -> u32 {
45-
crate::random::insecure::get_insecure_random_u64() as _
45+
wasi::random::insecure::get_insecure_random_u64() as _
4646
}
4747

4848
#[inline]
4949
fn next_u64(&mut self) -> u64 {
50-
crate::random::insecure::get_insecure_random_u64()
50+
wasi::random::insecure::get_insecure_random_u64()
5151
}
5252

5353
fn fill_bytes(&mut self, dest: &mut [u8]) {
5454
let n = dest.len();
5555
if usize::BITS <= u64::BITS || n <= u64::MAX as _ {
56-
dest.copy_from_slice(&crate::random::insecure::get_insecure_random_bytes(n as _));
56+
dest.copy_from_slice(&wasi::random::insecure::get_insecure_random_bytes(n as _));
5757
} else {
5858
let (head, tail) = dest.split_at_mut(u64::MAX as _);
59-
head.copy_from_slice(&crate::random::insecure::get_insecure_random_bytes(
59+
head.copy_from_slice(&wasi::random::insecure::get_insecure_random_bytes(
6060
u64::MAX,
6161
));
6262
self.fill_bytes(tail);

examples/rand-no_std.rs

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/ext/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#[cfg(feature = "std")]
22
mod std;
33

4-
#[cfg(feature = "rand")]
5-
pub mod rand;
6-
74
impl core::fmt::Display for crate::io::error::Error {
85
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
96
f.write_str(&self.to_debug_string())

0 commit comments

Comments
 (0)