Rust Bindings

You can also embed WasmEdge into your Rust application via the WasmEdge Rust SDK.

The definitions of WasmEdge Rust SDK involve two Rust crates, wasmedge-sys and wasmedge-sdk. They are designed based on different principles and for different purposes. The wasmedge-sys crate defines a group of low-level Rust APIs, which simply wrap WasmEdge C APIs and provide the safe counterpart, while the wasmedge-sdk crate provides more elegant and ergonomic APIs, which are more suitable for application development.

  • The wasmedge-sys crate defines a group of low-level Rust APIs, which simply wrap WasmEdge C APIs and provide the safe counterparts. The APIs in wasmedge-sys should be used to construct high-level libraries. For the details of the APIs, please visit wasmedge-sys API documentation.
  • The wasmedge-sdk crate is based on the wasmedge-sys crate and provides a more elegant and idiomatic Rust APIs. These APIs are more suitable for business-oriented design and development. The wasmedge-sdk crate is still under active development and coming soon.

The wasmedge-sys crate

Build

wasmedge-sys depends on the dynamic library and the header files of WasmEdge. To cargo build wasmedge-sys there are several choices to specify the dependencies:

  • By specifying WASMEDGE_INCLUDE_DIR and WASMEDGE_LIB_DIR

  • Suppose that you have already downloaded the Wasmedge-0.9.1 binary package from WasmEdge Releases and put it in the ~/workspace/me/ directory. The directory structure of the released package is shown below.

    ``` 1. root@0a877562f39e:~/workspace/me/WasmEdge-0.9.1-Linux# pwd 2. /root/workspace/me/WasmEdge-0.9.1-Linux

    1. root@0a877562f39e:~/workspace/me/WasmEdge-0.9.1-Linux# tree .
    2. .
    3. |-- bin
    4. | |-- wasmedge
    5. | `-- wasmedgec
    6. |-- include
    7. | `-- wasmedge
    8. | |-- enum_configure.h
    9. | |-- enum_errcode.h
    10. | |-- enum_types.h
    11. | |-- int128.h
    12. | |-- version.h
    13. | `-- wasmedge.h
    14. `-- lib64
    15. `-- libwasmedge_c.so

    16. 4 directories, 9 files ```

    17. Set WASMEDGE_INCLUDE_DIR and WASMEDGE_LIB_DIR environment variables to specify the include and lib (or lib64) directories. After that, go to the wasmedge-sys directory and cargo build the crate.

    ``` 1. root@0a877562f39e:~/workspace/me/WasmEdge/bindings/rust/wasmedge-sys# export WASMEDGE_INCLUDE_DIR=/root/workspace/me/WasmEdge-0.9.1-Linux/include/wasmedge

    1. root@0a877562f39e:~/workspace/me/WasmEdge/bindings/rust/wasmedge-sys# export WASMEDGE_LIB_DIR=/root/workspace/me/WasmEdge-0.9.1-Linux/lib64 ```
    2. By specifying WASMEDGE_BUILD_DIR
  • Suppose that you git clone WasmEdge repo in ~/workspace/me/WasmEdge, and follow the instructions to build WasmEdge native library. The generated include and lib directories should be in ~/workspace/me/WasmEdge/build.

  • Then, set WASMEDGE_BUILD_DIR environment variable to specify the build directory. After that, go to the wasmedge-sys directory and cargo build the crate.

    1. root@0a877562f39e:~/workspace/me/WasmEdge# export WASMEDGE_BUILD_DIR=/root/workspace/me/WasmEdge/build - By the standalone mode

Besides the two methods mentioned above, the standalone mode enables building WasmEdge native library directly before building the crate itself.

  • Suppose that you git clone WasmEdge repo in ~/workspace/me/WasmEdge. Go to the wasmedge-sys directory, and follow the instructions shown below:

    ``` 1. // set WASMEDGE_DIR 2. root@0a877562f39e:~/workspace/me/WasmEdge/bindings/rust/wasmedge-sys# export WASMEDGE_DIR=/root/workspace/me/WasmEdge

    1. // cargo build with standalone feature
    2. root@0a877562f39e:~/workspace/me/WasmEdge/bindings/rust/wasmedge-sys# cargo build --features standalone ```
    3. By WasmEdge docker image

If you choose WasmEdge docker image to build your own container for development, the pre-built WasmEdge binary package is located in $HOME/.wasmedge directory by default. The build script (build.rs) of wasmedge-sys crate can detect the package and build the crate automatically.

Examples