gchoi_acts/rust-wasmer
Public
wasmer run gchoi_acts/rust-wasmer

Rust Wasmer

Example code of Rust Wasmer.

Creating the Library


touch calculator.wai
// calculator.wai

/// Addition of two numbers
add: func(a: float32, b: float32) -> float32

/// Subtraction of two numbers
subtract: func(a: float32, b: float32) -> float32

/// Multiplication of two numbers
multiply: func(a: float32, b: float32) -> float32

/// Division of two numbers
divide: func(a: float32, b: float32) -> float32
// src/lib.rs

wai_bindgen_rust::export!("calculator.wai");
struct Calculator;

impl crate::calculator::Calculator for Calculator {
    fn add(a: f32, b: f32) -> f32 { a + b }
    fn subtract(a: f32, b: f32) -> f32 { a - b }
    fn multiply(a: f32, b: f32) -> f32 { a * b }
    fn divide(a: f32, b: f32) -> f32 {
        if b != 0.0 {
            a / b
        }
        else {
            0.0
        }
    }
}

Publishing the Library


Install wapm

make install-wapm

Wapm Login

If you don't have an account for https://wasmer.io/, please make one.

wapm login --user <user_id> --password <password>

Or edit wasmer user ID and password in wapm-login in Makefile, and type the following:

make wapm-login

Edit Cargo.toml

[package.metadata.wapm]
namespace = "<YOUR_USERNAME>"
abi = "none"
bindings = { wai-version = "0.2.0", exports = "calculator.wai" }

[lib]
crate-type = ["cdylib", "rlib"]

Publish the package

Wasmer Demo

More packages
wasmer/wasmer-hello website
wasmer/wasmer-edge-nuxt website
The WebAssembly interface to wasmer-pack.
Wasmer Compiler
ptitseb avatar
ptitseb
ptitseb avatar
ptitseb
The Wasmer frontend