| Crates.io | hydro2-operator-derive |
| lib.rs | hydro2-operator-derive |
| version | 0.1.0 |
| created_at | 2025-02-28 06:01:02.512791+00 |
| updated_at | 2025-02-28 06:01:02.512791+00 |
| description | Procedural macro that derives implementations of hydro2-operator's Operator trait, including port enumeration and bridging code for up to four inputs/outputs. |
| homepage | |
| repository | https://github.com/klebs6/klebs-general |
| max_upload_size | |
| id | 1572491 |
| size | 195,540 |
hydro2-operator-derive is a procedural macro crate that auto-generates the boilerplate needed to implement the Operator trait from [hydro2-operator]. By simply annotating your structs with #[derive(Operator)] and an #[operator(...)] attribute, you can specify:
execute="...").opcode="...").input0="...", input1="...", etc.).output0="...", output1="...", etc.).From this specification, the macro generates:
enum (named YourStructIO) to represent each of the operator's input and output variants.PortTryFromN, PortTryIntoN, etc.) for robust type conversions on each port.Operator<YourStructIO> trait itself, including an asynchronous execute(...) method that matches inputs, calls your specified function, and packages outputs into the appropriate enum variants.OperatorSignature, mapping your input/output types to each port index.use hydro2_operator::Operator;
#[operator(execute="process_fn", opcode="BasicOpCode::AddOp", input0="i32", output0="i32")]
#[derive(Operator)]
pub struct AddOperator {
// your fields here
}
&[T]), optional inputs (Option<T>), or no inputs/outputs at all.#[operator(...)]) are caught at compile time, providing clear diagnostic messages.#[operator(
execute="run_logic",
opcode="BasicOpCode::MyCustomOp",
input0="&[u8]",
output0="Vec<u8>",
output1="Option<String>"
)]
#[derive(Operator)]
pub struct MyCustomOperator {
factor: usize,
}
impl MyCustomOperator {
async fn run_logic(&self, data: &[u8]) -> hydro2_operator::NetResult<(Vec<u8>, Option<String>)> {
// do something with `data` and `self.factor`
Ok((data.to_vec(), Some(format!("Factor is {}", self.factor))))
}
}
This macro invocation generates:
enum MyCustomOperatorIO with variants:
Input0(&[u8])Output0(Vec<u8>)Output1(Option<String>)PortTryFrom0<&[u8]> for MyCustomOperatorIO::Input0(...)PortTryInto0<Vec<u8>> for MyCustomOperatorIO::Output0(...)Operator<MyCustomOperatorIO> trait impl for MyCustomOperator, mapping execute([...]) to call run_logic().All of these generated artifacts allow Hydro2’s runtime or other tooling to safely execute your operator within a multi-operator dataflow.
Include this in your Cargo dependencies:
[dependencies]
hydro2-operator-derive = "0.1"
You will also want to depend on hydro2-operator to use the Operator trait and related definitions.
Distributed under the OGPv1 License (see ogp-license-text crate for more details).