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_DIRandWASMEDGE_LIB_DIR -
Suppose that you have already downloaded the
Wasmedge-0.9.1binary 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
- root@0a877562f39e:~/workspace/me/WasmEdge-0.9.1-Linux# tree .
- .
- |-- bin
- | |-- wasmedge
- | `-- wasmedgec
- |-- include
- | `-- wasmedge
- | |-- enum_configure.h
- | |-- enum_errcode.h
- | |-- enum_types.h
- | |-- int128.h
- | |-- version.h
- | `-- wasmedge.h
- `-- lib64
-
`-- libwasmedge_c.so
-
4 directories, 9 files ```
- Set
WASMEDGE_INCLUDE_DIRandWASMEDGE_LIB_DIRenvironment variables to specify theincludeandlib(orlib64) directories. After that, go to thewasmedge-sysdirectory andcargo buildthe 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
- 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 cloneWasmEdge repo in~/workspace/me/WasmEdge, and follow the instructions to build WasmEdge native library. The generatedincludeandlibdirectories should be in~/workspace/me/WasmEdge/build. -
Then, set
WASMEDGE_BUILD_DIRenvironment variable to specify thebuilddirectory. After that, go to thewasmedge-sysdirectory andcargo buildthe crate.1. root@0a877562f39e:~/workspace/me/WasmEdge# export WASMEDGE_BUILD_DIR=/root/workspace/me/WasmEdge/build- By thestandalonemode
Besides the two methods mentioned above, the standalone mode enables building WasmEdge native library directly before building the crate itself.
-
Suppose that you
git cloneWasmEdge repo in~/workspace/me/WasmEdge. Go to thewasmedge-sysdirectory, 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
- // cargo build with standalone feature
- 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.
