dynpatch 0.1.0

Safe live code reloading for Rust - hot patch functions, services, and configs at runtime
Documentation
//! # dynpatch
//!
//! Safe live code reloading for Rust applications.
//!
//! `dynpatch` enables runtime hot-patching of Rust code with strong safety guarantees,
//! transactional semantics, and automatic rollback on failure.

//! ## Features
//!
//! - **Runtime hot patching**: Update code without restarting
//! - **Safety first**: Strong ABI/type compatibility checks
//! - **Transactional**: All-or-nothing activation with automatic rollback
//! - **State continuity**: Optional state migration between versions
//! - **Minimal overhead**: Lock-free implementation swapping
//! - **Developer friendly**: Macros, CLI, file watching, and introspection
//!
//! ## Quick Start
//!
//! ### Basic Usage
//!
//! ```rust,ignore
//! use dynpatch::prelude::*;
//!
//! // Initialize the runtime
//! init();
//!
//! // Load a patch
//! reload("path/to/patch.so")?;
//!
//! // Check active patch
//! if let Some(info) = active_patch_info() {
//!     println!("Active: {} v{}", info.name, info.version);
//! }
//!
//! // Rollback if needed
//! rollback()?;
//! ```
//!
//! ### Advanced Configuration
//!
//! ```rust,ignore
//! use dynpatch::prelude::*;
//! use dynpatch_interface::Version;
//!
//! // Configure the runtime with specific requirements
//! RuntimeBuilder::new()
//!     .interface_version(Version::new(1, 0, 0))
//!     .type_hash(0x1234567890abcdef)
//!     .init();
//! ```
//!
//! ### Mark code as patchable
//!
//! ```rust,ignore
//! use dynpatch::prelude::*;
//!
//! #[patch_trait]
//! pub trait Handler {
//!     fn handle(&self, request: Request) -> Response;
//! }
//!
//! #[patchable]
//! fn process_data(data: &str) -> String {
//!     data.to_uppercase()
//! }
//! ```
//!
//! ### Hot-reload configuration
//!
//! ```rust,ignore
//! 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();
//!     
//!     loop {
//!         let config = watcher.get();
//!         println!("Max connections: {}", config.max_connections);
//!         std::thread::sleep(std::time::Duration::from_secs(1));
//!     }
//! }
//! ```
//!
//! ## Architecture
//!
//! dynpatch consists of several components:
//!
//! - **dynpatch-core**: Runtime engine for loading and managing patches
//! - **dynpatch-macro**: Procedural macros for marking patchable code
//! - **dynpatch-interface**: Shared types for host-patch communication
//! - **dynpatch-watcher**: File watching and config reloading
//! - **dynpatch-cli**: Command-line tool for building and managing patches
//!
//! ## 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 on failure
//! - **Thread safety**: Lock-free RCU-like publication using `arc-swap`
//! - **Panic safety**: Catch initialization panics and rollback
//!
//! ## Optional Features
//!
//! - `watcher` (default) - File watching and config reloading
//! - `abi_stable` - Enhanced ABI checks using `abi_stable` crate
//! - `metrics` - Performance and operation metrics
//! - `signing` - Cryptographic patch signature verification
//! - `sandbox` - Process isolation for untrusted patches
//! - `json`, `yaml`, `toml` - Config format support

// Re-export core functionality
pub use dynpatch_core::{
    active_patch_info, history, init, reload, rollback, PatchInfo, PatchRecord, RuntimeBuilder,
};

// Re-export interface types
pub use dynpatch_interface::{
    compute_type_hash, PatchError, PatchInterface, PatchMetadata, StateMigration, TypeLayout,
    Version,
};

// Re-export macros
pub use dynpatch_macro::{patch_entry, patch_impl, patch_trait, patchable, HotConfig};

// Re-export watcher functionality (if enabled)
#[cfg(feature = "watcher")]
pub mod config {
    pub use dynpatch_watcher::config::*;
}

#[cfg(feature = "watcher")]
pub use dynpatch_watcher::watch;

/// Prelude module for convenient imports
///
/// This module re-exports the most commonly used items for easy access.
///
/// # Example
///
/// ```rust,ignore
/// use dynpatch::prelude::*;
///
/// init();
/// reload("patch.so")?;
/// ```
pub mod prelude {
    // Core runtime functions
    pub use crate::{
        active_patch_info, history, init, reload, rollback, 
        PatchInfo, PatchRecord, RuntimeBuilder, Version,
    };
    
    // Macros for defining patchable code
    pub use dynpatch_macro::{patch_entry, patch_impl, patch_trait, patchable, HotConfig};

    // Interface utilities
    pub use crate::compute_type_hash;

    // Config watching (if feature enabled)
    #[cfg(feature = "watcher")]
    pub use crate::config::ConfigWatcher;
    
    #[cfg(feature = "watcher")]
    pub use crate::watch;
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_init() {
        init();
        // Should not panic
    }

    #[test]
    fn test_version() {
        let v1 = Version::new(1, 0, 0);
        let v2 = Version::new(1, 1, 0);
        assert!(v1.is_compatible(&v2));
    }

    #[test]
    fn test_type_hash() {
        let hash1 = compute_type_hash("MyType", 8, 8);
        let hash2 = compute_type_hash("MyType", 8, 8);
        assert_eq!(hash1, hash2);
    }
}