Infiltrait
A Rust procedural macro that automatically generates trait definitions from implementation blocks.
Overview
The #[infiltrait] attribute macro allows you to define a trait and its implementation simultaneously by writing only the impl block. This eliminates the need to separately define the trait interface, reducing code duplication and keeping related code together.
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
use infiltrait;
;
// The macro automatically generates:
// trait Arithmetic {
// fn add(&self, a: i32, b: i32) -> i32;
// fn multiply(&self, a: i32, b: i32) -> i32;
// }
// Now you can use the trait normally:
Features
- Zero Runtime Overhead: Pure compile-time code generation
- Full Feature Support: Methods, associated types, and associated constants
- Visibility Control: Respects
pub,pub(crate), and other visibility modifiers - Safety Preservation: Maintains
unsafemarkers where appropriate - Clear Error Messages: Helpful compile-time diagnostics
Examples
Basic Methods
use infiltrait;
;
Associated Types and Constants
use infiltrait;
;
Public Traits
use infiltrait;
;
pub
// The generated trait will also be public:
// pub trait PublicApi { ... }
Generic Implementations
use infiltrait;
Unsafe Traits
use infiltrait;
;
unsafe
Use Cases
Rapid Prototyping
When exploring API designs, infiltrait lets you focus on the implementation without getting bogged down in trait definitions:
use infiltrait;
;
Library Development
Create clean APIs where the trait and implementation are defined together:
use infiltrait;
;
pub
Testing and Mocking
Generate traits that can be easily mocked for testing:
use infiltrait;
;
// Easy to create a mock for testing:
;
Limitations
- Trait Names: Cannot contain lifetimes or generic parameters in the trait name itself
- Implementation Only: Only works with trait implementations, not inherent impls
- Single Trait: Each
#[infiltrait]attribute generates exactly one trait
Error Handling
The macro provides clear error messages for common mistakes:
use infiltrait;
;
// ❌ Error: Please name a trait to implement
// ❌ Error: Trait may not contain lifetimes or generics
How It Works
The infiltrait macro:
- Parses the implementation block and extracts the trait name
- Converts each implementation item into its corresponding trait item:
- Method implementations → Method signatures
- Associated constants → Associated constant declarations
- Associated types → Associated type declarations
- Generates both the trait definition and preserves the original implementation
- Applies the same visibility and safety modifiers to the generated trait
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.