#collection #macro #variant #supporting #experiment #trick #type

yanked breakdance

Experimental macro for creating compile-time heterogeneously-typed collections

3 releases

0.0.3 Sep 28, 2023
0.0.2 Sep 28, 2023
0.0.1 Sep 28, 2023

#8 in #trick

MIT license

13KB
193 lines

breakdance

This is an experiment with macros to trick Rust collections into supporting heterogeneous types.

This macro provides support for heterogeneous lists by storing types within a Breakdown enum. This enum is procedurally generated by parsing occurrences of breakdown!() from the source code and creating an enum variant to reflect the type. The inner value can be retrieved with the .inner(), .inner_ref(), and .inner_mut() methods for Breakdown.

As of right now, you still need to specify the expected type when accessing the inner, so iterating over inner values is not possible unless they all share a trait. Trait implementation for Breakdown variants is possible but difficult at this time; future development may see the expansion of such additional features, but for now I am focusing on debugging.

This is just a toy project to give myself some more familiarity with the full set of Rust tools, but if anyone would like to give it a try in code please do! If you run into trouble getting it to work, please leave an issue on the GitHub repo (https://github.com/j-stach/breakdance).

Any other questions or suggestions are welcome too! Send me a message on GitHub.

How to use

Add it to your project:

cargo add breakdancer

And it should work like this:

use breakdance::*;

#[breakdance]
// submod declarations

fn main() {
    let num = breakdown!(42u8);
    let tru = breakdown!(true);

    let vec: Vec<Breakdown> = vec![num, tru];

    let num: &u8 = vec[0].inner_ref();
    println![ "{}", num ];    // prints 42
    let tru: &mut bool = vec[1].inner_mut();
    *tru = false;
    println![ "{}", tru ];    // prints false

}

Dependencies

~280–760KB
~17K SLoC