Crates.io | purewasm |
lib.rs | purewasm |
version | 0.1.0 |
source | src |
created_at | 2023-10-03 16:09:28.401298 |
updated_at | 2023-10-03 16:09:28.401298 |
description | Pure WASM bindings |
homepage | |
repository | https://github.com/purewasm/rust-purewasm |
max_upload_size | |
id | 991196 |
size | 20,072 |
rust-purewasm is a purewasm library for rust.
⚠ Under Develeopment
The rust-purewasm is a Rust library designed to build pure functions using WebAssembly (Wasm). WebAssembly is a portable binary intermediate language that enables code execution in a virtual machine. It provides a secure sandboxed environment where code can run, with a limited instruction set that focuses on pure functions and numerical operations on its own memory. While WebAssembly is commonly used in web technology, it is also suitable for developing deterministic(pure) functions in various contexts.
However, WebAssembly has a limitation when it comes to supporting complex data types. Despite this limitation, purewasm aims to work around it by making assumptions about memory and providing encoding-decoding capabilities for JSON and CBOR codecs.
The runtime component of purewasm handles the execution of WebAssembly modules. It follows these steps:
The wasm module component of purewasm is responsible for processing the input data, preparing the output data, and returning its pointer and length to the runtime. It performs the following tasks:
A purewasm function only takes one input and returns only one output. Here's an example of how to define a function in Rust that can be used as a WebAssembly module with JSON and CBOR codecs:
fn example(ptr: i32, len: i32) -> (i32, i32) {
// ...
}
cargo install rust-purewasm
purewasm = { version = "0.1.0", features = ["bindgen-json"] }
#![no_main]
#![cfg_attr(not(test), no_std)]
extern crate alloc;
use purewasm::bindgen::prelude::*;
use serde::{Serialize, Deserialize};
use alloc::{format, string::String};
#[derive(Debug, Serialize, Deserialize)]
pub struct Input {
pub code: i32,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CustomResult {
pub msg: String,
}
#[purewasm_bindgen]
pub fn handle_example(input: Input) -> PureResult<CustomResult> {
Ok(CustomResult {
msg: format!("The input code is {}", input.code),
})
}
purewasm = { version = "0.1.0", features = ["wasmtime"] }
use purewasm::wasmtime::PureModule;
fn main() {
let wasm_file = "../target/wasm32-unknown-unknown/release/purewasm_json_module.wasm";
let input = serde_json::to_vec(&serde_json::json!({"code": 6})).unwrap();
let mut module = PureModule::from_file(wasm_file);
let result = module.call_fn("handle_example", &input);
let result: serde_json::Value = serde_json::from_slice(&result).unwrap();
println!("Result: {:?}", result);
}
See cbor example
APACHE 2.0