memlink-msdk
SDK for building memlink modules with automatic serialization, FFI exports, panic isolation, and arena-based memory management.
Features
#[memlink_export]Macro: Automatic wrapper generation for module methods- Automatic Serialization: MessagePack (rmp-serde) for efficient IPC
- Panic Isolation: Built-in panic catching at FFI boundary
- Arena Allocation: Bump-pointer arena for bounded temporary allocations
- Nested Calls: Module-to-module communication support
- Backpressure Control: Flow control signals from daemon
- Deadline Tracking: Timeout and deadline support for calls
- Structured Logging: Log export to daemon for aggregation
- Metrics API: Counter, gauge, and histogram support
Installation
[]
= "0.1.0"
Note: memlink-msdk-macros is automatically included as a dependency.
Quick Start
Basic Function Export
use *;
Async Functions
pub async
Arena Allocation
Custom Data Structures
use ;
Nested Module Calls
pub async
Backpressure and Deadlines
Logging and Metrics
Module Structure
| Module | Description |
|---|---|
arena |
Bump-pointer arena allocator for temporary allocations |
caller |
Module caller for nested invocations |
context |
Call context with execution environment |
dispatch |
Method dispatch table |
error |
Error types (ModuleError, Result) |
exports |
FFI exports (memlink_init, memlink_call) |
log |
Structured logging API |
macros |
Proc macro re-exports |
metrics |
Metrics recording API |
panic |
Panic isolation utilities |
ref |
Persistent arena references |
request |
Request/Response types |
serialize |
Serialization trait and BincodeSerializer |
Testing
Test your exported functions using the SDK's test utilities:
Function Requirements
Functions annotated with #[memlink_export] must:
- First Parameter:
&CallContextorCallContext - Return Type:
Result<T>whereT: Serialize + DeserializeOwned - Sync or Async: Both are supported
Error Handling
The SDK provides comprehensive error handling:
use *;
Error Types
| Error | Description |
|---|---|
QuotaExceeded |
Arena or resource quota exceeded |
CallFailed(String) |
Nested call failed |
Timeout(Duration) |
Operation timed out |
Panic(String) |
Module panicked |
Serialize(String) |
Serialization error |
ServiceUnavailable |
Service not available |
InvalidMethod |
Unknown method hash |
ModuleNotFound(String) |
Target module not found |
MaxCallDepthExceeded |
Nested call depth limit reached |
Performance
Benchmark results from cargo bench -p memlink-msdk:
Function Call Overhead
| Benchmark | Time | Throughput |
|---|---|---|
echo_empty |
1.14 ns | 874.6 M calls/sec |
echo_small ("hello") |
98.8 ns | 10.1 M calls/sec |
Arena Allocation
| Benchmark | Time | Throughput |
|---|---|---|
alloc_u64 |
2.41 ns | 414.8 M allocs/sec |
alloc_with (u64) |
3.31 ns | 301.8 M allocs/sec |
Note: Benchmarks run on Windows x86_64. Actual performance varies by system configuration and payload size. Run cargo bench for measurements on your system.
Examples
See the examples directory for complete working examples:
basic.rs- Basic function exportsasync_echo.rs- Async function handlingarena_usage.rs- Arena allocation patternsnested_calls.rs- Module-to-module communication
Integration with memlink-runtime
Modules built with memlink-msdk can be loaded dynamically:
use ;
use ModuleRef;
let runtime = with_local_resolver;
let handle = runtime.load?;
let result = runtime.call?;
Troubleshooting
Common Issues
"First parameter must be &CallContext"
// Wrong
// Correct
"Return type must be Result"
// Wrong
// Correct
License
Licensed under the Apache License 2.0. See LICENSE-APACHE for details.
Contributing
Contributions are welcome! Please submit issues and pull requests to the main repository.