Nodyn
Easy polymorphism with enums
nodyn provides a Rust macro for creating wrapper enums that
encapsulate a fixed set of types with automatic implementations
for From, TryFrom, and delegated methods or traits. This is
ideal for scenarios where you need to store values of different
types in a type-safe, zero-cost way, as an alternative to trait
objects.
"This is a perfectly good solution when our interchangeable items are a fixed set of types that we know when our code is compiled."
The Rust Programming Language
Quick Start
Create a simple enum wrapper for i32, String, and f64:
nodyn!
let values: = vec!;
for value in values
Features
- Automatic Variant Creation: Generates enum variants for types
like
i32,String, or custom structs with CamelCase naming. - Type Conversions: Implements
From<T>for each variant andTryFrom<Enum>with theimpl TryIntodirective. - Method and Trait Delegation: Delegates methods or entire traits to underlying types.
- Type Introspection: Provides
count,types, andtype_namemethods withimpl introspection. - Polymorphic Vec: Generates a
Vec<Enum>wrapper with avec!-like macro and variant-specific methods (e.g.,first_i32,count_string) viavec. - Customizable Variants: Allows overriding default variant names.
- Supported Types: Handles path types, references, arrays, and tuples.
Use impl directives to enable features explicitly (e.g., impl TryInto is_as). Cargo features (try_into, is_as, introspection) are
deprecated but supported for backward compatibility. See the Feature Flags section
for details.
Vec Wrapper Example
Use the vec feature to create a polymorphic Vec with variant-specific
methods:
nodyn!
let mut inventory = inventory!;
// Add more gold
inventory.push;
// Check for weapons in the inventory
assert!;
// Total gold coins
let total_gold = inventory.iter_i32.;
assert_eq!;
// Get a potion
if let Some = inventory.first_f64
See the Polymorphic Vec section in the Documentation
Method Delegation Example
nodyn!
let mut container: Container = "hello".to_string.into;
assert_eq!;
assert!;
container.clear;
assert!;
Trait Implementation Example
Delegate entire traits when all wrapped types implement them:
use ;
// All wrapped types implement Display
nodyn!
let values = displayables!;
for val in values
JSON Example
This example creates a JSON-like data structure with nested arrays, showcasing trait delegation and Polymorphic Vec features:
use fmt;
;
;
nodyn!
let mut values = default;
values.push;
values.push;
values.push;
values.push;
values.push;
for val in &values
## Installation
Add `nodyn` to your `Cargo.toml`:
```toml
nodyn = "0.2.0"
Comparison
| Feature | nodyn | enum_dispatch | sum_type | Box |
|---|---|---|---|---|
| Runtime Cost | Zero | Zero | Zero | Heap allocation |
| Trait Delegation | ✅ Yes | ✅ Scoped only | ❌ No | ✅ Yes |
| Method Delegation | ✅ Yes | ❌ No | ❌ No | ❌ No |
| Type Introspection | ✅ Built-in | ❌ No | ❌ No | ❌ No |
| Vec Wrapper | ✅ Yes | ❌ No | ❌ No | ❌ No |
| Compile-Time Known | Required | Required | Required | Not required |
| Memory Overhead | Discriminant only | Discriminant only | Discriminant only | Pointer + vtable |
- enum_dispatch: Optimizes dynamic dispatch with zero-cost enums but lacks method delegation and Vec wrappers.
- sum_type: Simplifies enum creation but lacks advanced features like delegation or introspection.
Documentation
Contributing
Contributions are welcome! Check out the GitHub repository for issues, feature requests, or to submit pull requests.
License
This project is licensed under the MIT License - see the LICENSE file for details.