Skip to content

bengreenier/enum-pipeline

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

enum-pipeline

Provides a way to use enums to describe and execute ordered data pipelines. 🦀🐾

CI Crates.io docs.rs dependency status

I needed a succinct way to describe 2d pixel map operations for a game I'm working on. I wanted callers to be able to easily determine all possible operations (hence enum), with per-operation data (hence variants), and their operation-specific logic. This is what I came up with!

Quickstart

Some quick examples to get you started. For more information see docs.rs/enum_pipeline and docs.rs/enum_pipeline_derive.

Derive

#[derive(Default)]
struct MacroMutRefData {
    a_count: i32,
    b_count: i32,
}

#[derive(ExecuteWithMut)]
#[execute_with(MacroMutRefData)]
enum MacroMutRefPipeline {
    #[handler(handle_a)]
    A(i32),
    #[handler(handle_b)]
    B,
}

impl MacroMutRefPipeline {
    fn handle_a(i: i32, arg: &mut MacroMutRefData) {
        arg.a_count += 1;
    }

    fn handle_b(arg: &mut MacroMutRefData) {
        arg.b_count += 1;
    }
}

Then create and execute some pipelines:

let mut arg = MacroMutRefData::default();
vec![MacroMutRefPipeline::A(23), MacroMutRefPipeline::B].execute_with_mut(&mut arg);

Manual

#[derive(Default)]
struct MutRefData {
    a_count: i32,
    b_count: i32,
}

enum MutRefPipeline {
    A(i32),
    B,
}

impl ExecuteWithMut<MutRefData> for MutRefPipeline {
    fn execute_with_mut(self, arg: &mut MutRefData) {
        match self {
            MutRefPipeline::A(i) => arg.a_count += 1,
            MutRefPipeline::B => arg.b_count += 1,
        }
    }
}

Then create and execute some pipelines:

let mut arg = MutRefData::default();
vec![MutRefPipeline::A(23), MutRefPipeline::B].execute_with_mut(&mut arg);

License

MIT

About

Provides a way to use enums to describe and execute ordered data pipelines. 🦀🐾

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages