[crate announcement] nodyn: A Macro for Wrapper Enums with Trait and Method Delegation
ā Rust š 2025-06-13 š¤ surdeus šļø 17Hello Rustaceans,
Iām thrilled to announce nodyn, a new crate that makes it easy to create wrapper enums for a fixed set of types with automatic From, TryFrom, and delegated methods or traits. The nodyn::nodyn! macro is designed for type-safe, zero-cost polymorphism using enums without the boilerplate.
Features
- Delegates methods and traits to wrapped types.
- Generates enums with variants for specified types (paths, references, arrays, slices, tuples).
- Implements
From<T>for each variant type andTryFrom<Enum> for Tfor non-reference types. - Provides introspection methods:
count(),types(), andtype_name().
Example
nodyn::nodyn! {
#[derive(Debug)]
pub enum Article {
NewsArticle,
SocialPost,
}
impl Summary {
fn summarize(&self) -> String;
}
}
pub trait Summary {
fn summarize(&self) -> String;
}
pub struct NewsArticle { /* fields */ }
impl Summary for NewsArticle {
fn summarize(&self) -> String { "News summary".to_string() }
}
pub struct SocialPost { /* fields */ }
impl Summary for SocialPost {
fn summarize(&self) -> String { "Post summary".to_string() }
}
fn main() {
let article = Article::NewsArticle(NewsArticle {});
assert_eq!(article.summarize(), "News summary");
}
Links
Iād appreciate feedback, use cases, or suggestions for nodyn.
1 post - 1 participant
š·ļø rust_feed