[crate announcement] nodyn: A Macro for Wrapper Enums with Trait and Method Delegation

āš“ rust    šŸ“… 2025-06-13    šŸ‘¤ surdeus    šŸ‘ļø 2      

surdeus

Hello 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 and TryFrom<Enum> for T for non-reference types.
  • Provides introspection methods: count(), types(), and type_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

Read full topic

šŸ·ļø rust_feed