Is there an automated approach to have two enums (one used for indexing and other as tagged union) paired?

I currently have this setup in my code:

pub enum Scene {
    Intro(SceneIntro),
    Menu(SceneMenu),
    Map(SceneMap),
    Options(SceneOptions),
    Credits(SceneCredits),
    Close(SceneClose),
}
pub enum SceneName {
    Intro,
    Menu,
    Map,
    Options,
    Credits,
    Close,
}
array_of_scenes: [Scene; 6]
// Passing the scene name to a function and working with the actual scene.
fn system_do_thing_with_scene(&mut self, scene_name: SceneName) {
    match self.array_of_scenes[scene_name as usize] {
        SceneIntro(scene_intro) => {...}
        Scene::Menu(scene_menu) => {...}
        Scene::Map(scene_map) => {...}
        Scene::Options(scene_options) => {...}
        Scene::Credits(scene_credits) => {...}
        Scene::Close(scene_close) => {...}
    }
}

As you can see, I need SceneName and Scene with "equivalent" (paired) variants, which I currently do manually. Is there an standardized way of doing this automated?

You could certainly write a #[derive] or an attribute-like procedural macro to process the variants and output an "indexing" enum based on that. I think it's technically even doable with a macro_rules! macro, albeit probably a lot messier.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.