Hijekt
Feature-based compile-time code injection for Rust
Hijekt is a procedural macro that allows you to modify functions and structs at compile-time based on Cargo features. Perfect for creating optimized builds, conditional debugging, platform-specific implementations, and more.
Features
- Zero runtime overhead - all modifications happen at compile-time
- Feature-gated - inject code only when specific features are enabled
- Function modification - add initialization/cleanup, swap implementations
- Struct modification - add/remove fields based on build configuration
- Clean code - keeps your source readable while generating optimized variants
Installation
Add your features to your Cargo.toml like:
[]
= "0.1.0"
[]
= []
= []
= []
= []
Quick Start
use hijekt;
// Add debug logging only when the "debug" feature is enabled
When compiled with --features debug, this expands to:
Function Modifications
Begin/End Injection
Add function calls at the beginning and/or end of your functions:
Implementation Swapping
Replace entire function bodies for different build targets:
Code Removal
Remove debugging code in release builds:
Struct Modifications
Remove Fields
Create minimal structs for embedded or lightweight builds:
Add Fields
Extend structs with additional functionality:
Auto-Generated Field Names
Let Hijekt generate field names from types:
Complex Example
Combine multiple modifications for sophisticated feature management:
Attribute Options
| Option | Description | Example |
|---|---|---|
feat |
Feature name to gate the modifications | feat = "debug" |
begin |
Function to call at the beginning | begin = "init" |
end |
Function to call at the end | end = "cleanup" |
swap |
Replace function body with call to this function | swap = "optimized_impl" |
rm |
Remove functions/fields (single or multiple) | rm = "debug_fn" or rm("field1", "field2") |
add |
Add struct fields (single or multiple) | add = "field: Type" or add("field1: Type1", "Type2") |
Use Cases
- Platform-specific optimizations - CUDA/OpenCL for GPU, SIMD for CPU
- Debug vs Release builds - extensive logging and checks in debug mode
- Feature tiers - basic/standard/premium functionality levels
- Embedded systems - minimal resource usage configurations
- A/B testing - different algorithm implementations
- Profiling builds - add timing and measurement code
Performance
Hijekt operates entirely at compile-time through procedural macros. There is zero runtime overhead - the generated code is identical to what you would write by hand with #[cfg()] attributes.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the MIT License - see the LICENSE file for details.