Skip to content

Commit 5595cb2

Browse files
committed
chore: split examples into std and no_std
Signed-off-by: Roman Volosatovs <[email protected]>
1 parent afe9cb6 commit 5595cb2

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

Cargo.toml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,32 @@ std = []
2828
# Unstable feature to support being a libstd dependency
2929
rustc-dep-of-std = ["compiler_builtins", "core", "rustc-std-workspace-alloc"]
3030

31+
[[example]]
32+
name = "cli-command-no_std"
33+
crate-type = ["cdylib"]
34+
3135
[[example]]
3236
name = "cli-command"
3337
crate-type = ["cdylib"]
38+
required-features = ["std"]
39+
40+
[[example]]
41+
name = "hello-world"
42+
required-features = ["std"]
43+
44+
[[example]]
45+
name = "http-proxy-no_std"
46+
crate-type = ["cdylib"]
3447

3548
[[example]]
3649
name = "http-proxy"
3750
crate-type = ["cdylib"]
51+
required-features = ["std"]
3852

3953
[[example]]
40-
name = "rand"
54+
name = "rand-no_std"
4155
required-features = ["rand"]
56+
57+
[[example]]
58+
name = "rand"
59+
required-features = ["std", "rand"]

examples/cli-command-no_std.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
wasi::cli::command::export!(Example);
2+
3+
struct Example;
4+
5+
impl wasi::exports::cli::run::Guest for Example {
6+
fn run() -> Result<(), ()> {
7+
let stdout = wasi::cli::stdout::get_stdout();
8+
stdout.blocking_write_and_flush(b"Hello, WASI!").unwrap();
9+
Ok(())
10+
}
11+
}

examples/hello-world-no_std.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let stdout = wasi::cli::stdout::get_stdout();
3+
stdout.blocking_write_and_flush(b"Hello, world!\n").unwrap();
4+
}

examples/http-proxy-no_std.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use wasi::http::types::{
2+
Fields, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam,
3+
};
4+
5+
wasi::http::proxy::export!(Example);
6+
7+
struct Example;
8+
9+
impl wasi::exports::http::incoming_handler::Guest for Example {
10+
fn handle(_request: IncomingRequest, response_out: ResponseOutparam) {
11+
let resp = OutgoingResponse::new(Fields::new());
12+
let body = resp.body().unwrap();
13+
14+
ResponseOutparam::set(response_out, Ok(resp));
15+
16+
let out = body.write().unwrap();
17+
out.blocking_write_and_flush(b"Hello, WASI!").unwrap();
18+
drop(out);
19+
20+
OutgoingBody::finish(body, None).unwrap();
21+
}
22+
}

examples/rand-no_std.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use wasi::ext::rand::rand::Rng as _;
2+
use wasi::ext::rand::{HostInsecureRng, HostRng};
3+
4+
fn main() {
5+
let stdout = wasi::cli::stdout::get_stdout();
6+
7+
let r: u64 = HostRng.gen();
8+
9+
stdout
10+
.blocking_write_and_flush(
11+
format!("Cryptographically-secure random u64 is {r}\n").as_bytes(),
12+
)
13+
.unwrap();
14+
15+
let r: u64 = HostInsecureRng.gen();
16+
stdout
17+
.blocking_write_and_flush(format!("Pseudo-random u64 is {r}\n").as_bytes())
18+
.unwrap();
19+
}

0 commit comments

Comments
 (0)