A checked Rust type for an IDL type

rust_type in build.rs holds an IDL declaration as a Rust type of your own (GitHub #33): an IDL string as a date-time group that cannot hold an invalid one, an unsigned long key as NonZeroU32.

What it shows

  • build.rs calls zerodds_build::Config::new().rust_type("app::Dtg", "crate::dtg::Dtg").rust_type("app::Track.id", "core::num::NonZeroU32"); a path that names no typedef, member or union branch is a build error.
  • Dtg implements zerodds_wire::WireRepr with type Idl = str: it writes through Display straight into the sample's buffer and reads from the &str the received sample lends.
  • A C or C++ peer built from idl/track.idl sees the string and the uint32 and reads the same octets.
  • Two samples are rejected with their reasons: a time that is no date-time group, and a key of zero.

Build and run

cargo run

Output

83 octets: 070000000d0000003035313433305a4e4f5632360000000029000000020000000d0000003035313430305a4e4f563236000000000d0000003035313431355a4e4f5632360000000007000000414c5048413100
read back: 7 at 051430ZNOV26
rejected: at 4: expected a date-time group DDHHMMZMONYY
rejected: at 0: expected a value other than zero, which a `NonZero<u32>` holds

Output of a run on the Linux test host with ci/run-examples.sh.

The crates of GitHub #33 are newer than the latest crates.io release: the example reaches them through path dependencies into a ZeroDDS source tree that holds the snippets repository, until RC8 publishes them.

See also

Ein geprüfter Rust-Typ für einen IDL-Typ

rust_type in build.rs hält eine IDL-Deklaration als eigenen Rust-Typ (GitHub #33): einen IDL-string als Datum-Zeit-Gruppe, die keine ungültige halten kann, einen Key vom Typ unsigned long als NonZeroU32.

Was es zeigt

  • build.rs ruft zerodds_build::Config::new().rust_type("app::Dtg", "crate::dtg::Dtg").rust_type("app::Track.id", "core::num::NonZeroU32") auf; ein Pfad, der keinen Typedef, kein Member und keinen Union-Zweig nennt, ist ein Build-Fehler.
  • Dtg implementiert zerodds_wire::WireRepr mit type Idl = str: Es schreibt über Display direkt in den Puffer des Samples und liest aus dem &str, den das empfangene Sample leiht.
  • Ein C- oder C++-Gegenüber, aus idl/track.idl gebaut, sieht den string und den uint32 und liest dieselben Oktette.
  • Zwei Samples werden mit ihrem Grund abgelehnt: eine Zeit, die keine Datum-Zeit-Gruppe ist, und ein Key von null.

Bauen und ausführen

cargo run

Ausgabe

83 octets: 070000000d0000003035313433305a4e4f5632360000000029000000020000000d0000003035313430305a4e4f563236000000000d0000003035313431355a4e4f5632360000000007000000414c5048413100
read back: 7 at 051430ZNOV26
rejected: at 4: expected a date-time group DDHHMMZMONYY
rejected: at 0: expected a value other than zero, which a `NonZero<u32>` holds

Ausgabe eines Laufs auf dem Linux-Testhost mit ci/run-examples.sh.

Die Crates zu GitHub #33 sind neuer als das letzte Release auf crates.io: Das Beispiel erreicht sie über Pfadabhängigkeiten in einen ZeroDDS-Quellbaum, der das Snippets-Repository enthält, bis RC8 sie veröffentlicht.

Verwandt