Crates.io | enum_pipeline |
lib.rs | enum_pipeline |
version | 2.0.0 |
source | src |
created_at | 2021-10-21 23:19:16.514026 |
updated_at | 2021-10-29 23:12:56.952877 |
description | Provides a way to use enums to describe and execute ordered data pipelines. |
homepage | |
repository | |
max_upload_size | |
id | 469103 |
size | 20,748 |
Provides a way to use enums to describe and execute ordered data pipelines. 🦀🐾
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!
Some quick examples to get you started. For more information see docs.rs/enum_pipeline and docs.rs/enum_pipeline_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);
#[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);
MIT