#operator #wire #generics #parameters #attributes #bridging #automatic

macro hydro2-network-wire-derive

A procedural macro providing #[derive(NetworkWire)] for bridging Hydro2 operator wires and enumerating operator IO variants. It automatically handles generics, type parameters, and attribute parsing to unify wire and operator definitions.

1 unstable release

new 0.1.0 Feb 28, 2025

#11 in #bridging


Used in 4 crates (2 directly)

Custom license

305KB
5K SLoC

hydro2-network-wire-derive

This crate provides a procedural macro named #[derive(NetworkWire)], which auto-generates the internal bridging and execution logic for a "wire" type in the Hydro2 operator framework. By combining information about the wire’s generics and a list of available operators (via an #[available_operators(op="...")] attribute), it automatically creates:

  • An IO enum (e.g., MyWireIO) to mirror each referenced operator’s types.
  • Operator trait implementations and port handling methods, ensuring that inputs/outputs can be safely mapped into the correct Rust types at runtime.
  • Support for reusing or minting new generic parameters when bridging types, helping unify operator-specific and wire-specific type/lifetime/const generics.

Key Features

  1. #[derive(NetworkWire)]: Attach to a struct to generate:
    • A companion enum (like MyWireIO) containing zero or more operator-IO variants.
    • Operator trait implementations bridging between that enum and each operator’s internal types.
  2. Operator attribute: Use #[available_operators(op="FooOp", op="BarOp<Z>", ...)] on the wire struct to specify the set of operators the wire can handle.
  3. Generics Unification: Automatically recognizes matching lifetimes, type parameters, and const parameters in both the wire struct and operator types. It will reuse or mint new parameters (e.g., OpTy0, OPC1) as needed.
  4. Flexible Where-Clauses: Merges where-clauses from the wire struct and newly introduced operator parameters.

Example Overview

When a struct uses:

#[available_operators(op="AddOp", op="ConstantOp<T>")]
#[derive(NetworkWire)]
pub struct MyWire<T> {
    // ...
}

this macro will produce:

  • An enum MyWireIO<T> with variants like AddOpIO(AddOpIO<T>), ConstantOpIO(ConstantOpIO<T>), and None.
  • Implementations of the required operator and port-try-into traits, ensuring correct input/output type conversion.

You can then rely on MyWireIO for operator graph constructions involving the listed operators.

Be sure to declare this crate in your Cargo.toml dependencies as:

[dependencies]
hydro2-network-wire-derive = "0.1"

Then, import and use the macro in your Rust code as needed.

License

Distributed under the OGPv1 License (see ogp-license-text crate for more details).

Dependencies

~12–23MB
~311K SLoC