Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yunusemredilber/rust-runtime-package-system-example
A simple example of a dylib based runtime rust to rust ffi system (dynamic arguments & return value)
https://github.com/yunusemredilber/rust-runtime-package-system-example
dylib dynamic package runtime rust rust-lang
Last synced: 2 days ago
JSON representation
A simple example of a dylib based runtime rust to rust ffi system (dynamic arguments & return value)
- Host: GitHub
- URL: https://github.com/yunusemredilber/rust-runtime-package-system-example
- Owner: yunusemredilber
- License: gpl-3.0
- Created: 2023-08-26T15:22:43.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-02T05:24:29.000Z (over 1 year ago)
- Last Synced: 2024-01-29T11:09:47.455Z (12 months ago)
- Topics: dylib, dynamic, package, runtime, rust, rust-lang
- Language: Rust
- Homepage:
- Size: 20.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Example: Rust Runtime Package System
This is a straightforward example illustrating a runtime Rust-to-Rust FFI system that relies on dynamic libraries (dylibs).
> This package aims to provide an example of achieving dynamism through FFI (Foreign Function Interface).
> It does not cover aspects like package metadata, path resolution, compression, mutability, etc.## Usage
### 1. Building the Test Package
This system assumes that you can create your packages just like regular cdylib crates.
Since it follows the usual crate structure, you can build it using Cargo as follows:```bash
cd core-package
cargo build
cd ..
```This command will generate the `libcore_package.dylib` file inside the `core-package/target/debug/` directory, which we can later use for FFI.
The example exposes two functions:
- `add_numbers(args: *mut FFIValue, len: size_t) -> FFIValue`
- `print(args: *mut FFIValue, len: size_t) -> FFIValue`To verify if these functions are accessible, you can use the `nm` utility:
```bash
nm core-package/target/debug/libcore_package.dylib | grep "print"
# 0000000000004840 T _print
```This output indicates that the `print` function is accessible at runtime.
### 2. Consuming The Package
Once we have built the example package, we can run the consumer as follows:
```bash
cd package-consumer
cargo run
# hey
```This command will invoke our package's `print` function.
## How it Works
Creating numerous function signatures and mapping them at runtime is not practical.
This system takes a more dynamic approach by sending an array of enums (`FFIValue`).
The `FFIValue` enum contains basic data types like `f64`, `bool`, and `char`.
The library implementer can consume the array of values to obtain arguments.> Note: The `FFIValue` enum must be consistent in both the library and the consumer. Optionally, these two crates can obtain `FFIValue` from a separate crate.
--
> Author: Yunus Emre Dilber ([@yunusemredilber](https://github.com/yunusemredilber))