Skip to content

Commit cad0e94

Browse files
committed
Update Rust composition example
Signed-off-by: Ryan Levick <[email protected]>
1 parent 882a18d commit cad0e94

File tree

123 files changed

+7412
-1376
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+7412
-1376
lines changed

examples/rust/Cargo.lock

Lines changed: 93 additions & 82 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/rust/Cargo.toml

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@ edition = "2021"
66
[lib]
77
crate-type = ["cdylib"]
88

9-
[package.metadata.component]
10-
package = "component:spin-fileserver-example"
11-
adapter = "../../adapters/fd1e948d/wasi_snapshot_preview1.reactor.wasm"
12-
139
[dependencies]
14-
cargo-component-bindings = "0.4.0"
15-
wit-bindgen = "0.13.0"
16-
spin-sdk = { git = "https://github.com/fermyon/spin" }
17-
futures = "0.3.28"
10+
wit-bindgen = "0.16.0"
11+
spin-sdk = "3.0.1"
12+
futures = "0.3.30"
1813

1914
[workspace]

examples/rust/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@ component for serving static files.
1111
- [Spin v2.0+](https://developer.fermyon.com/spin/install)
1212
- [Rust](https://rustup.rs/), including the `wasm32-wasi` target
1313
- [cargo-component](https://github.com/bytecodealliance/cargo-component)
14-
- [wasm-tools](https://github.com/bytecodealliance/wasm-tools/)
15-
- Note that you'll need [this fork](https://github.com/dicej/wasm-tools/tree/wasm-compose-resource-imports) until [this PR](https://github.com/bytecodealliance/wasm-tools/pull/1261) has been merged and released.
14+
- [wac](https://github.com/bytecodealliance/wac)
1615
- [curl](https://curl.se/download.html) or a web browser for testing
1716

1817
Once you have Rust installed, the following should give you everything else:
1918

2019
```shell
2120
rustup target add wasm32-wasi
2221
cargo install cargo-component
23-
cargo install --locked --git https://github.com/dicej/wasm-tools \
24-
--branch wasm-compose-resource-imports wasm-tools
22+
cargo install wac-cli
2523
```
2624

2725
## Building and Running

examples/rust/compose.yml

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

examples/rust/spin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ component = "spin-fileserver-example"
1414
source = "target/wasm32-wasi/release/composed.wasm"
1515
files = [{ source = "../static", destination = "/" }]
1616
[component.spin-fileserver-example.build]
17-
command = "cargo component build --release && (cd .. && cargo component build --release) && RUST_LOG=error wasm-tools compose target/wasm32-wasi/release/spin_fileserver_example.wasm -c compose.yml -o target/wasm32-wasi/release/composed.wasm"
17+
command = "cargo component build --release && (cd .. && cargo component build --release) && RUST_LOG=error wac plug --plug ../../target/wasm32-wasi/release/spin_static_fs.wasm target/wasm32-wasi/release/spin_fileserver_example.wasm -o target/wasm32-wasi/release/composed.wasm"
1818
watch = ["src/**/*.rs", "Cargo.toml"]

examples/rust/src/bindings.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Generated by `wit-bindgen` 0.25.0. DO NOT EDIT!
2+
// Options used:
3+
4+
#[cfg(target_arch = "wasm32")]
5+
#[link_section = "component-type:wit-bindgen:0.25.0:spin-fileserver-example:encoded world"]
6+
#[doc(hidden)]
7+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 209] = *b"\
8+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07D\x01A\x02\x01A\0\x04\
9+
\x019component:spin-fileserver-example/spin-fileserver-example\x04\0\x0b\x1d\x01\
10+
\0\x17spin-fileserver-example\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0d\
11+
wit-component\x070.208.1\x10wit-bindgen-rust\x060.25.0";
12+
13+
#[inline(never)]
14+
#[doc(hidden)]
15+
#[cfg(target_arch = "wasm32")]
16+
pub fn __link_custom_section_describing_imports() {
17+
wit_bindgen_rt::maybe_link_cabi_realloc();
18+
}

examples/rust/src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use {
2-
bindings::wasi::http::incoming_handler,
2+
bindings::wasi::http0_2_0::incoming_handler,
33
futures::SinkExt,
44
spin_sdk::{
55
http::{Fields, IncomingRequest, Method, OutgoingResponse, ResponseOutparam},
@@ -12,9 +12,7 @@ mod bindings {
1212
path: "../wit",
1313
world: "delegate",
1414
with: {
15-
"wasi:http/[email protected]": spin_sdk::wit::wasi::http::types,
16-
"wasi:io/[email protected]": spin_sdk::wit::wasi::io::streams,
17-
"wasi:io/[email protected]": spin_sdk::wit::wasi::io,
15+
"wasi:http/[email protected]": ::spin_sdk::wit::wasi::http::types,
1816
}
1917
});
2018
}
@@ -23,10 +21,11 @@ mod bindings {
2321
async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam) {
2422
match (request.method(), request.path_with_query().as_deref()) {
2523
(Method::Get, Some("/hello")) => {
26-
let response = OutgoingResponse::new(
27-
200,
28-
&Fields::new(&[("content-type".to_string(), b"text/plain".to_vec())]),
29-
);
24+
let fields = Fields::new();
25+
fields
26+
.append(&"content-type".to_owned(), &b"text/plain".to_vec())
27+
.unwrap();
28+
let response = OutgoingResponse::new(fields);
3029

3130
let mut body = response.take_body();
3231

@@ -43,7 +42,9 @@ async fn handle_request(request: IncomingRequest, response_out: ResponseOutparam
4342
}
4443

4544
_ => {
46-
response_out.set(OutgoingResponse::new(405, &Fields::new(&[])));
45+
let response = OutgoingResponse::new(Fields::new());
46+
response.set_status_code(405).unwrap();
47+
response_out.set(response);
4748
}
4849
}
4950
}

examples/wit/command-extended.wit

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,35 @@
11
// All of the same imports and exports available in the wasi:cli/command world
22
// with addition of HTTP proxy related imports:
33
world command-extended {
4-
import wasi:clocks/wall-clock@0.2.0-rc-2023-10-18;
5-
import wasi:clocks/monotonic-clock@0.2.0-rc-2023-10-18;
6-
import wasi:clocks/timezone@0.2.0-rc-2023-10-18;
7-
import wasi:filesystem/types@0.2.0-rc-2023-10-18;
8-
import wasi:filesystem/preopens@0.2.0-rc-2023-10-18;
9-
import wasi:sockets/instance-network@0.2.0-rc-2023-10-18;
10-
import wasi:sockets/ip-name-lookup@0.2.0-rc-2023-10-18;
11-
import wasi:sockets/network@0.2.0-rc-2023-10-18;
12-
import wasi:sockets/tcp-create-socket@0.2.0-rc-2023-10-18;
13-
import wasi:sockets/tcp@0.2.0-rc-2023-10-18;
14-
import wasi:sockets/udp-create-socket@0.2.0-rc-2023-10-18;
15-
import wasi:sockets/udp@0.2.0-rc-2023-10-18;
16-
import wasi:random/random@0.2.0-rc-2023-10-18;
17-
import wasi:random/insecure@0.2.0-rc-2023-10-18;
18-
import wasi:random/insecure-seed@0.2.0-rc-2023-10-18;
19-
import wasi:io/poll@0.2.0-rc-2023-10-18;
20-
import wasi:io/streams@0.2.0-rc-2023-10-18;
21-
import wasi:cli/environment@0.2.0-rc-2023-10-18;
22-
import wasi:cli/exit@0.2.0-rc-2023-10-18;
23-
import wasi:cli/stdin@0.2.0-rc-2023-10-18;
24-
import wasi:cli/stdout@0.2.0-rc-2023-10-18;
25-
import wasi:cli/stderr@0.2.0-rc-2023-10-18;
26-
import wasi:cli/terminal-input@0.2.0-rc-2023-10-18;
27-
import wasi:cli/terminal-output@0.2.0-rc-2023-10-18;
28-
import wasi:cli/terminal-stdin@0.2.0-rc-2023-10-18;
29-
import wasi:cli/terminal-stdout@0.2.0-rc-2023-10-18;
30-
import wasi:cli/terminal-stderr@0.2.0-rc-2023-10-18;
4+
import wasi:clocks/wall-clock@0.2.0;
5+
import wasi:clocks/monotonic-clock@0.2.0;
6+
import wasi:filesystem/types@0.2.0;
7+
import wasi:filesystem/preopens@0.2.0;
8+
import wasi:sockets/instance-network@0.2.0;
9+
import wasi:sockets/ip-name-lookup@0.2.0;
10+
import wasi:sockets/network@0.2.0;
11+
import wasi:sockets/tcp-create-socket@0.2.0;
12+
import wasi:sockets/tcp@0.2.0;
13+
import wasi:sockets/udp-create-socket@0.2.0;
14+
import wasi:sockets/udp@0.2.0;
15+
import wasi:random/random@0.2.0;
16+
import wasi:random/insecure@0.2.0;
17+
import wasi:random/insecure-seed@0.2.0;
18+
import wasi:io/poll@0.2.0;
19+
import wasi:io/streams@0.2.0;
20+
import wasi:cli/environment@0.2.0;
21+
import wasi:cli/exit@0.2.0;
22+
import wasi:cli/stdin@0.2.0;
23+
import wasi:cli/stdout@0.2.0;
24+
import wasi:cli/stderr@0.2.0;
25+
import wasi:cli/terminal-input@0.2.0;
26+
import wasi:cli/terminal-output@0.2.0;
27+
import wasi:cli/terminal-stdin@0.2.0;
28+
import wasi:cli/terminal-stdout@0.2.0;
29+
import wasi:cli/terminal-stderr@0.2.0;
3130

3231
// We should replace all others with `include self.command`
3332
// as soon as the unioning of worlds is available:
3433
// https://github.com/WebAssembly/component-model/issues/169
35-
import wasi:logging/logging@0.2.0-rc-2023-10-18;
36-
import wasi:http/outgoing-handler@0.2.0-rc-2023-10-18;
34+
import wasi:http/outgoing-handler@0.2.0;
3735
}

examples/wit/delegate.wit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
world delegate {
2-
import wasi:http/incoming-handler@0.2.0-rc-2023-10-18;
2+
import wasi:http/incoming-handler@0.2.0;
33
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package wasi:cli@0.2.0-rc-2023-10-18;
2+
3+
world command {
4+
include reactor;
5+
6+
export run;
7+
}

0 commit comments

Comments
 (0)