memlink-msdk-macros
Procedural macros for the memlink SDK. Provides automatic code generation for exporting Rust functions as memlink module methods with serialization, FFI bindings, and panic isolation.
Features
#[memlink_export]Macro: Automatic wrapper generation for module methods- Compile-Time Hashing: FNV-1a method hash computation at compile time
- Automatic Serialization: MessagePack serialization for arguments and return values
- FFI Export Generation: C-compatible extern functions with panic isolation
- Async Support: Handles both sync and async functions
- Custom Method Names: Optional
nameattribute for method hash customization
Quick Start
Basic Usage
use *;
Async Functions
pub async
Custom Method Names
Arena Allocation
Nested Module Calls
pub async
What the Macro Generates
When you annotate a function with #[memlink_export], the macro generates:
- Args Struct: Serialization struct for function parameters
- Wrapper Function: Handles serialization/deserialization
- FFI Export: C-compatible extern function with panic isolation
- Registration Code: Method dispatch table registration
Example Expansion
// Your code
// Macro generates (simplified):
pub unsafe extern "C"
Function Requirements
Functions annotated with #[memlink_export] must:
- First Parameter:
&CallContextorCallContext - Return Type:
Result<T>whereT: Serialize + DeserializeOwned - Sync or Async: Both are supported
Supported Types
The macro supports any type that implements serde::Serialize and serde::de::DeserializeOwned:
- Primitives:
u8,u16,u32,u64,i8,i16,i32,i64,f32,f64,bool,char - Strings:
String,&str - Collections:
Vec<T>,Option<T>,HashMap<K, V> - Tuples:
(T1, T2, ...)where each T implements the traits - Custom structs: Derive
SerializeandDeserialize
use ;
Error Handling
The macro automatically handles:
- Serialization Errors: Converted to
ModuleError::Serialize - Panic Isolation: Panics caught and converted to
ModuleError::Panic - FFI Safety: Null pointer checks and buffer validation
Performance
| Operation | Overhead |
|---|---|
| Macro expansion | Compile-time only |
| Serialization | ~100-500 ns per KB |
| FFI boundary | ~50 ns |
| Panic isolation | ~10 ns |
Integration with memlink-msdk
The macros are re-exported, so you can use them directly:
use *;
// From memlink-msdk-macros
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
Testing
Test your exported functions using the SDK's test utilities:
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.