
M E T A C A L L
MetaCall complete example with Function Mesh between multiple programming languages.
This example has been designed in order to illustrate how to use Rust loader through ports of other languages, especially the Python port.
This allows the users to invoke functions of rust libraries from other languages, including python, js, ruby, etc.
We can build metacall from source using following commands:
git clone --branch v0.5.30 https://github.com/metacall/core --depth 1
mkdir core/build && cd core/build
cmake \
-DNODEJS_CMAKE_DEBUG=On \
-DOPTION_BUILD_LOADERS_PY=On \
-DOPTION_BUILD_LOADERS_RS=On \
-DOPTION_BUILD_PORTS=On \
-DOPTION_BUILD_PORTS_PY=On \
-DOPTION_BUILD_DETOURS=Off \
-DOPTION_BUILD_SCRIPTS=Off \
-DOPTION_BUILD_TESTS=Off \
-DOPTION_BUILD_EXAMPLES=Off \
..
cmake --build . --target install
ldconfig /usr/local/lib
Here we choose to wrap melody, a language that compiles to ECMAScript regular expressions. In that way, we can create regex formula in a more readable and maintainable way, not just for rust but for many other languages.
Create a rust library, add melody
as a dependency, and wrap the compile
function of melody
. The reason why we create a wrapper layer is that we can export the functions we need instead of all of them.
Check the src/lib.rs
and you will find that it's super intuitive to wrap a function. Basically, you just write a regular rust function and mark it as pub
.
After that, compile the library with cargo b
.
After obtaining the rust library, we can call it from python.
Check the main.py
to see what is happening.
First, we load the compiled library using metacall_load_from_package
, and then invoke the function with metacall
.
Note that you should install metacall
first or provide a environment variable LOADER_LIBRARY_PATH
which includes metacall
libraries, in order to use the python port.
# build docker image
docker build -t metacall/python-rust-example .
# run it!
docker run --rm -it metacall/python-rust-example
-
Load from file through Python(
load_from_file.py
) -
Load from memory through Python(
load_from_memory.py
)
Currently, Rust loader supports primitive types as parameters and the return value, including:
- Numeric types
- String
- Vec
- HashMap