From 656c03609a2ba480dc88b28ade288b33a4b3a26b Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Tue, 29 Aug 2023 17:01:29 +0100 Subject: examples/rust: Add a minimal hello world rust example This is about the smallest it can be. Its Unit application config would look like "applications": { "rust-hello-world": { "type": "wasm", "module": "/path/to/unit-wasm/examples/rust/hello-world/target/wasm32-wasi/debug/rust_hello_world.wasm", "request_handler": "uwr_request_handler", "malloc_handler": "luw_malloc_handler", "free_handler": "luw_free_handler" } } Signed-off-by: Andrew Clayton --- examples/rust/hello-world/src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 examples/rust/hello-world/src/lib.rs (limited to 'examples/rust/hello-world/src/lib.rs') 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; +} -- cgit