summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/rust/Makefile10
-rw-r--r--examples/rust/hello-world/Cargo.toml12
-rw-r--r--examples/rust/hello-world/src/lib.rs41
3 files changed, 60 insertions, 3 deletions
diff --git a/examples/rust/Makefile b/examples/rust/Makefile
index b41bc33..3d645fa 100644
--- a/examples/rust/Makefile
+++ b/examples/rust/Makefile
@@ -2,7 +2,7 @@ include ../../shared.mk
SDIR = examples/rust
-examples: rust-echo-request rust-upload-reflector
+examples: rust-echo-request rust-upload-reflector hello-world
rust-echo-request: echo-request/Cargo.toml echo-request/src/lib.rs
$(PP_GEN) $(SDIR)/echo-request/target/wasm32-wasi/
@@ -12,6 +12,10 @@ rust-upload-reflector: upload-reflector/Cargo.toml upload-reflector/src/lib.rs
$(PP_GEN) $(SDIR)/upload-reflector/target/wasm32-wasi/
$(v)cd upload-reflector; cargo build --target=wasm32-wasi
+hello-world: hello-world/Cargo.toml hello-world/src/lib.rs
+ $(PP_GEN) $(SDIR)/hello-world/target/wasm32-wasi/
+ $(v)cd hello-world; cargo build --target=wasm32-wasi
+
clean:
- rm -f echo-request/Cargo.lock upload-reflector/Cargo.lock
- rm -rf echo-request/target upload-reflector/target
+ rm -f */Cargo.lock
+ rm -rf */target
diff --git a/examples/rust/hello-world/Cargo.toml b/examples/rust/hello-world/Cargo.toml
new file mode 100644
index 0000000..2ff520e
--- /dev/null
+++ b/examples/rust/hello-world/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "rust-hello-world"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+unit-wasm = { path = "../../../src/rust", version = "0.1.2" }
+
+[lib]
+crate-type = ["cdylib"]
diff --git a/examples/rust/hello-world/src/lib.rs b/examples/rust/hello-world/src/lib.rs
new file mode 100644
index 0000000..9be1abd
--- /dev/null
+++ b/examples/rust/hello-world/src/lib.rs
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+
+/*
+ * Copyright (C) Andrew Clayton
+ * Copyright (C) Timo Stark
+ * Copyright (C) F5, Inc.
+ */
+
+use unit_wasm::rusty::*;
+
+static mut REQUEST_BUF: *mut u8 = std::ptr::null_mut();
+
+#[no_mangle]
+pub extern "C" fn uwr_request_handler(addr: *mut u8) -> i32 {
+ let ctx = &mut UWR_CTX_INITIALIZER();
+
+ uwr_init_ctx(ctx, addr, 4096);
+ uwr_set_req_buf(ctx, unsafe { &mut REQUEST_BUF }, LUW_SRB_ALLOC);
+
+ uwr_write_str!(
+ ctx,
+ " * Welcome to WebAssembly in Rust on Unit! \
+ [libunit-wasm ({}.{}.{}/{:#010x})] *\n\n",
+ LUW_VERSION_MAJOR,
+ LUW_VERSION_MINOR,
+ LUW_VERSION_PATCH,
+ LUW_VERSION_NUMBER,
+ );
+
+ uwr_http_init_headers(ctx, 2, 0);
+ uwr_http_add_header_content_type(ctx, "text/plain");
+ uwr_http_add_header_content_len(ctx);
+ uwr_http_send_headers(ctx);
+
+ uwr_http_send_response(ctx);
+ uwr_http_response_end();
+
+ uwr_free(unsafe { REQUEST_BUF });
+
+ return 0;
+}