๐ฌ Dolphin
A lightweight and safe Rust FFI library for dynamically loading and invoking functions from shared libraries (.dll, .so, .dylib). Dolphin provides a simple interface to call native C functions from any compiled library at runtime.
Features
- ๐ Dynamic Loading - Load functions from any shared library at runtime
- ๐ Safe API - Wraps unsafe FFI calls in a safe, ergonomic interface
- โก Performance - Pre-load function addresses for zero-overhead repeated calls
- ๐ฏ Flexible - Pass any data type including primitives, strings, and complex structs
- ๐ Return Values - Call functions that return values (integers, floats, structs, etc.)
- ๐ Cross-Platform - Works on Windows (
.dll), Linux (.so), and macOS (.dylib)
Installation
Add Dolphin to your Cargo.toml:
[]
= "0.3.0"
Quick Start
Simple Usage
use load_and_invoke;
Functions with Return Values
use load_and_invoke_with_return;
High-Performance Pattern (Pre-loading)
For repeated calls, pre-load the function once and reuse the address:
use ;
Pre-loading also works with return values:
use ;
Working with Structs
use load_and_invoke;
API Overview
Core Functions
For Void Functions (no return value):
-
load(library_path, function_name)- Load a function and return its address- Returns:
Option<usize>- The function address if found
- Returns:
-
invoke(address, arguments)- Call a pre-loaded function- Returns:
Result<(), String>- Success or error message
- Returns:
-
load_and_invoke(library_path, function_name, arguments)- Load and call in one step- Returns:
Result<(), String>- Success or error message
- Returns:
For Functions with Return Values:
-
load_with_return::<R>(library_path, function_name)- Load a function that returns typeR- Returns:
Result<usize, String>- The function address or error message
- Returns:
-
invoke_with_return::<T, R>(address, arguments)- Call a pre-loaded function that returns typeR- Returns:
Result<R, String>- The return value or error message
- Returns:
-
load_and_invoke_with_return::<T, R>(library_path, function_name, arguments)- Load and call in one step- Returns:
Result<R, String>- The return value or error message
- Returns:
C Function Requirements
Void Functions
C functions that don't return values must follow this signature:
void
Functions with Return Values
C functions that return values must follow this signature:
ReturnType
Where ReturnType can be any C type (int, double, struct, etc.).
The data pointer contains the serialized arguments, and len is the byte count.
Example C Library
// Void function (no return)
void
// Function returning an integer
int32_t
// Function returning a double
double
Compile it:
# macOS
# Linux
# Windows
Examples
The repository includes comprehensive examples:
# Clone the repository
# Build C examples
&& &&
# Run examples
C# Interop: See examples/CSHARP.md for documentation on C# interop via NativeAOT (advanced).
Use Cases
- Plugin Systems - Dynamically load and execute plugins at runtime
- C Library Integration - Call C libraries without compile-time linking
- C# Interop - Call .NET/C# functions via NativeAOT (see examples/CSHARP.md for details)
- Hot Reloading - Reload functions without restarting your application
- Language Interop - Bridge Rust with C, C++, or any C-compatible library
- Legacy Code - Interface with existing native libraries
Performance
Dolphin is designed for performance:
- Zero overhead for pre-loaded functions
- No runtime dependencies beyond
libloading - Minimal allocations - Direct memory operations
- Efficient - Function addresses are simple integers
Benchmark comparison:
load_and_invoke: ~1-2ยตs per call (includes library loading)load+invoke: ~50-100ns per call (pre-loaded)
Safety
While FFI is inherently unsafe, Dolphin provides guardrails:
- โ Validates function addresses before calling
- โ
Returns
Resulttypes for error handling - โ Safe wrapper API around unsafe operations
- โ ๏ธ User must ensure correct function signatures
- โ ๏ธ User must ensure data layout matches C expectations
Platform Support
| Platform | Library Format | Tested |
|---|---|---|
| macOS | .dylib |
โ |
| Linux | .so |
โ |
| Windows | .dll |
โ |
Contributing
Contributions are welcome! Please feel free to submit issues or pull requests.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
Built with:
libloading- Cross-platform dynamic library loading
Links
- Repository: https://github.com/Logan-Garrett/dolphin
- Documentation: https://docs.rs/dolphin
- Crates.io: https://crates.io/crates/dolphin
Made with ๐ฌ by Logan Garrett
Testing
cargo test # Run basic tests
cargo test -- --ignored # Run integration tests (requires gcc)
cargo test -- --include-ignored # Run all tests