idlc in build.rs

The runnable companion to the zerodds-idlc handbook's build-script section: a build.rs that shells out to the CLI and a lib.rs that includes the generated module.

What it shows

  • idl/Robot.idl defines an @appendable, @key-ed Robot::Pose; build.rs runs the CLI and src/lib.rs include!s the result from $OUT_DIR.
  • It documents a real doc gap it found: the generated file carries file-level #![allow(...)] inner attributes because it is designed to stand alone, and include! splices those into the including file where an inner attribute is only legal at the very top. Both the doc and this companion now strip those lines and re-apply the allows as outer attributes on the wrapper module.
  • cargo test proves the roundtrip on the library target; the demo binary encodes and decodes a Robot::Pose.

Build and run

cargo test                       # roundtrip unit test on the lib target
cargo run --bin idlc-buildrs-demo

Output

idlc-buildrs-demo: 40 bytes on the wire, roundtripped Pose { robot_id: "R2D2", x: 1.0, y: 2.0, theta: 0.5 }

Taken verbatim from the example's own README.

See also

idlc in build.rs

Der lauffähige Begleiter zum Build-Script-Abschnitt des zerodds-idlc-Handbuchs: ein build.rs, das die CLI aufruft, und ein lib.rs, das das generierte Modul einbindet.

Was es zeigt

  • idl/Robot.idl definiert ein @appendable-Struct Robot::Pose mit @key; build.rs ruft die CLI auf und src/lib.rs bindet das Ergebnis aus $OUT_DIR per include! ein.
  • Es hält eine echte Lücke der Dokumentation fest: Die generierte Datei trägt Datei-Attribute #![allow(...)], weil sie allein stehen soll, und include! schiebt diese in die einbindende Datei, wo ein inneres Attribut nur ganz oben zulässig ist. Dokument und Begleiter entfernen diese Zeilen jetzt und setzen dieselben Allows als äußere Attribute an das Wrapper-Modul.
  • cargo test weist den Roundtrip auf dem Library-Target nach; das Demo-Binary codiert und decodiert eine Robot::Pose.

Bauen und ausführen

cargo test                       # Roundtrip-Unit-Test auf dem Lib-Target
cargo run --bin idlc-buildrs-demo

Ausgabe

idlc-buildrs-demo: 40 bytes on the wire, roundtripped Pose { robot_id: "R2D2", x: 1.0, y: 2.0, theta: 0.5 }

Wörtlich aus dem README des Beispiels übernommen.

Verwandt