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
      3. root@0a877562f39e:~/workspace/me/WasmEdge-0.9.1-Linux# tree .
      4. .
      5. |-- bin
      6. | |-- wasmedge
      7. | `-- wasmedgec
      8. |-- include
      9. | `-- wasmedge
      10. | |-- enum_configure.h
      11. | |-- enum_errcode.h
      12. | |-- enum_types.h
      13. | |-- int128.h
      14. | |-- version.h
      15. | `-- wasmedge.h
      16. `-- lib64
      17. `-- libwasmedge_c.so
      18. 4 directories, 9 files
    • 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
      2. root@0a877562f39e:~/workspace/me/WasmEdge/bindings/rust/wasmedge-sys# export WASMEDGE_LIB_DIR=/root/workspace/me/WasmEdge-0.9.1-Linux/lib64
  • 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
      3. // cargo build with standalone feature
      4. root@0a877562f39e:~/workspace/me/WasmEdge/bindings/rust/wasmedge-sys# cargo build --features standalone
  • 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