# dynpatch
[](https://crates.io/crates/dynpatch)
[](https://docs.rs/dynpatch)
[](https://opensource.org/licenses/MIT)
Safe live code reloading for Rust. Hot-patch functions, services, and configurations at runtime with type/ABI validation, transactional swaps, and automatic rollback.
## Features
- 🔥 **Runtime hot patching** - Update code without restarting your application
- 🛡️ **Safety first** - Strong ABI/type compatibility checks before activation
- ⚡ **Transactional** - All-or-nothing activation with automatic rollback on failure
- 🔄 **State continuity** - Optional state migration between patch versions
- 🚀 **Minimal overhead** - Lock-free implementation swapping using RCU patterns
- 🎯 **Developer friendly** - Ergonomic macros, CLI tools, and introspection APIs
## Quick Start
Add dynpatch to your `Cargo.toml`:
```toml
[dependencies]
dynpatch = "0.1"
```
### Mark code as patchable
```rust
use dynpatch::prelude::*;
#[patch_trait]
pub trait Handler {
fn handle(&self, request: &str) -> String;
}
#[patchable]
fn process_data(data: &str) -> String {
data.to_uppercase()
}
```
### Load patches at runtime
```rust
use dynpatch::prelude::*;
fn main() {
// Initialize the runtime
init();
// Your application loop
loop {
// ... application logic ...
// Load a patch when ready
if let Ok(_) = reload("path/to/patch.so") {
println!("Patch applied!");
}
}
}
```
### Hot-reload configuration
```rust
use dynpatch::config::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
struct AppConfig {
max_connections: usize,
timeout_ms: u64,
}
impl HotConfig for AppConfig {}
fn main() {
let watcher = watch::<AppConfig, _>("config.toml").unwrap();
// Config updates automatically when the file changes
let config = watcher.get();
println!("Max connections: {}", config.max_connections);
}
```
## Use Cases
- **Backend services**: Push logic fixes or feature updates without downtime
- **Game engines**: Live-swap gameplay logic, AI, or rendering code during development
- **Long-running daemons**: Apply hotfixes to production systems without restart
- **Embedded systems**: Update firmware modules at runtime
- **ML pipelines**: Test algorithm updates in live data processing
## How It Works
1. **Mark patchable items** using `#[patchable]` and `#[patch_trait]` macros
2. **Define interfaces** as traits that both host and patches implement
3. **Build patches** as dynamic libraries (`.so`/`.dylib`/`.dll`)
4. **Load at runtime** with automatic ABI validation and transactional activation
5. **Rollback** to previous versions if anything goes wrong
## Safety Guarantees
- **ABI validation**: Type layout and signature checking before activation
- **Version compatibility**: Semantic versioning with compatibility rules
- **Transactional swaps**: Atomic activation with automatic rollback
- **Thread safety**: Lock-free RCU-like publication
- **Panic safety**: Initialization failures trigger automatic rollback
## Command-Line Tool
Install the CLI for building and managing patches:
```bash
cargo install dynpatch-cli
```
Commands:
- `dynpatch build` - Compile a patch from source
- `dynpatch apply` - Apply a patch to a running process
- `dynpatch rollback` - Revert to previous version
- `dynpatch inspect` - Show patch status and history
- `dynpatch watch` - Auto-apply patches from a directory
## Features
- `watcher` (default) - File watching and config reloading
- `abi_stable` - Enhanced ABI checks
- `metrics` - Performance metrics
- `signing` - Cryptographic signature verification
- `sandbox` - Process isolation for untrusted patches
- `json`, `yaml`, `toml` - Config format support
## Minimum Supported Rust Version
Rust 1.70.0 or later.
## Platform Support
- Linux (`.so`)
- macOS (`.dylib`)
- Windows (`.dll`)
## Examples
See the `examples/` directory for complete working examples:
- `http_handler` - Hot-patching an HTTP request handler
- `config_reload` - Live config reloading with file watching
- `service_swap` - Trait-based service swapping
## Documentation
- [User Guide](https://gitlab.com/TIVisionOSS/crates/dynpatch/-/tree/main/docs)
- [API Documentation](https://docs.rs/dynpatch)
- [Examples](https://gitlab.com/TIVisionOSS/crates/dynpatch/-/tree/main/examples)
## Contributing
Contributions are welcome! See [CONTRIBUTING.md](../CONTRIBUTING.md) for guidelines.
## License
MIT License - see [LICENSE](../LICENSE) for details.
## Repository
<https://gitlab.com/TIVisionOSS/crates/dynpatch>
## Author
Eshan Roy <m.eshanized@gmail.com>