avmnif-rs
Safe Rust bindings and utilities for building AtomVM Native Implemented Functions (NIFs) and ports.
Overview
avmnif-rs provides a type-safe, memory-safe foundation for integrating Rust code with AtomVM. It handles the low-level details of AtomVM's term format, atom management, and FFI boundaries, allowing you to focus on your application logic.
Features
- Memory-safe term conversion between Rust types and AtomVM terms
- Generic atom table operations that work with any AtomVM context
- Tagged ADT serialization for type-safe Erlang-Rust data exchange
- NIF collection macros for easy function registration
- Port communication utilities for building custom port drivers
- Comprehensive error handling with proper Erlang error propagation
- No-std compatible core functionality
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
[]
= ["cdylib"]
Basic NIF Example
use ;
nif_collection!;
Tagged ADT Example
use ;
Architecture
Core Components
term- AtomVM term representation and conversion utilitiesatom- Generic atom table operations and managementtagged- Type-safe ADT serialization with discriminator atomsports- Port communication and lifecycle managementresource- Resource type registration and management
Design Principles
- Generic by design - All operations work with any
AtomTableOpsimplementation - Memory safety - No unsafe code in the public API surface
- Error transparency - All failure modes are explicit and recoverable
- Zero-cost abstractions - Minimal runtime overhead
- Testing-first - Comprehensive test coverage with mock implementations
Error Handling
All operations return explicit Result types with detailed error information:
Errors automatically convert to appropriate Erlang error atoms when crossing the FFI boundary.
Building
The library compiles to a static library (.a) that gets linked into AtomVM:
This produces target/release/libmy_nifs.a which can be linked into AtomVM builds.
Testing
Run the comprehensive test suite:
Tests include:
- Mock atom table implementations for isolated testing
- Round-trip serialization validation
- Error condition coverage
- Memory safety verification
- Integration scenarios
Platform Support
- Primary: Linux x86_64, ARM64
- Secondary: macOS, Windows (via WSL)
- Embedded: Any target supported by AtomVM with
no_std
License
MIT
Contributing
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
Minimum Supported Rust Version (MSRV)
Rust 1.70 or later.