tagged_dispatch
Memory-efficient trait dispatch using tagged pointers. Like enum_dispatch, but your enums are only 8 bytes on 64-bit systems, regardless of the variant size!
Features
- 🎯 8-byte enums - Constant size regardless of variant types
- ⚡ Zero-cost dispatch - Inlined, no vtable overhead
- 📦 Familiar API - Works like
enum_dispatch - 🔧 No allocator required - Works with
no_std(bring your own allocator) - 🚀 Cache-friendly - Better locality than fat enums
- 🏗️ Arena allocation support - Optional arena allocation for even better performance
Installation
Add this to your Cargo.toml:
[]
= "0.1"
# Optional: Enable arena allocation support
= { = "0.1", = ["allocator-bumpalo"] }
Quick Example
use tagged_dispatch;
// Define your trait
// Create an enum with variants that implement the trait
// Implement the trait for each variant
When to Use
Use tagged_dispatch when:
- ✅ You have many instances and memory usage is critical (8 bytes vs potentially hundreds)
- ✅ Your variants are large or vary significantly in size
- ✅ You can accept the heap allocation overhead
- ✅ You want better cache locality for collections
Use enum_dispatch when:
- ✅ You want stack allocation and no heap overhead
- ✅ Your variants are similarly sized or small
- ✅ You have fewer instances
- ✅ You need the absolute fastest dispatch (no pointer indirection)
Use trait objects when:
- ✅ You need open sets of types (not known at compile time)
- ✅ You're okay with 16-byte fat pointers
- ✅ You need to work with external types you don't control
Advanced Features
Arena Allocation
For high-performance scenarios, use arena allocation to get Copy types and eliminate individual allocations:
Multiple Trait Dispatch
Dispatch multiple traits through the same enum:
Default Implementations
Traits with default implementations work as expected:
Non-Dispatched Methods
Mark trait methods that shouldn't be dispatched with #[no_dispatch]:
Architecture Requirements
This crate requires x86-64 or AArch64 architectures where the top 7 bits of 64-bit pointers are unused (standard on modern Linux, macOS, and Windows systems).
Limitations
- ⚠️ Supports up to 128 variant types (7-bit tag)
- ⚠️ Generic traits are not yet supported
- ⚠️ Requires heap allocation for variants (or arena allocation)
- ⚠️ Only works on x86-64 and AArch64 architectures
Safety
This crate uses unsafe code for tagged pointer manipulation. All unsafe operations are carefully documented and tested. The safety invariants are:
- Pointers are always valid and properly aligned
- Tags are always within the valid range (0-127)
- Proper cleanup via
Dropimplementation - Type safety enforced at compile time
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.
Contribution
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.