| Crates.io | blanket_trait |
| lib.rs | blanket_trait |
| version | 0.2.1 |
| created_at | 2025-12-13 14:22:14.592878+00 |
| updated_at | 2026-01-15 03:11:32.274495+00 |
| description | Attribute macro that generates a trait with an inherent blanket implementation. |
| homepage | |
| repository | https://github.com/mintlu8/blanket_trait |
| max_upload_size | |
| id | 1982953 |
| size | 22,239 |
Attribute macro that generates a trait with an inherent blanket implementation.
trait Behavior {
fn name() -> &'static str;
async fn action(&self);
}
#[blanket_trait(impl<T: Behavior> ErasedBehavior for T where T: Send + Sync + Clone + 'static)]
pub trait ErasedBehavior {
fn name(&self) -> &str {
T::name()
}
fn action(&self) -> Pin<Box<dyn Future<Output = ()> + '_>>{
Box::pin(T::action(self))
}
fn dyn_clone(&self) -> Box<dyn ErasedBehavior> {
Box::new(T::clone(self))
}
}
Generates:
pub trait ErasedBehavior {
fn name(&self) -> &str;
fn action(&self) -> Pin<Box<dyn Future<Output = ()> + '_>>;
fn dyn_clone(&self) -> Box<dyn ErasedBehavior>;
}
impl<T: Behavior> ErasedBehavior for T where T: Send + Sync + Clone + 'static {
fn name(&self) -> &str {
T::name()
}
fn action(&self) -> Pin<Box<dyn Future<Output = ()> + '_>>{
Box::pin(T::action(self))
}
fn dyn_clone(&self) -> Box<dyn ErasedBehavior> {
Box::new(self.clone())
}
}
License 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.
Contributions are welcome!
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.