A DDS topic type defined in Rust

#[derive(DdsType)] defines the topic type in Rust and exports its IDL for C, C++ and every other vendor (GitHub #33): a date-time group, a NonZeroU32 key, a union from an enum with data, a generic pair and a field that borrows.

What it shows

  • Track carries #[dds(name = "app::Track", extensibility = "final")], #[dds(key)] on id and #[dds(bound = …)] on its strings and sequence; Track::idl() is the IDL printed first.
  • enum Position with data is an IDL union on a short discriminator; Pair<f64> is app::Pair_double.
  • build.rs compiles the exported IDL through zerodds-build with rust_type — the route of rust-type — and the run checks that both routes give the same octets, TypeIdentifier and TypeObject.
  • TrackView<'a> is the same IDL type with callsign: &'a str: decoding it lends the call sign out of the sample instead of copying it.
  • peer/run.sh generates a C codec from idl/track.idl with zerodds-idlc and reads the sample the Rust side wrote.

Build and run

cargo run
sh peer/run.sh   # the C peer, generated by zerodds-idlc from idl/track.idl

Output

exported IDL:
module app { @final struct Pair_double { double first; double second; }; };
module app { @final union Position switch (short) { case 1: string grid; case 2: ::app::Pair_double latlon; }; };
module app { @final struct Track { @key unsigned long id; string<12> at; sequence<string, 8> seen; string<16> callsign; ::app::Position position; }; };
104 octets: 070000000d0000003035313433305a4e4f5632360000000029000000020000000d0000003035313430305a4e4f563236000000000d0000003035313431355a4e4f5632360000000007000000414c5048413100000200000048e17a14aea74940f6285c8fc2f52240
the IDL route writes the same octets, TypeIdentifier and TypeObject
read back: 7 at 051430ZNOV26, LatLon(Pair { first: 51.31, second: 9.48 })
borrowed: ALPHA1 lent from the sample
rejected: at 69: expected a date-time group DDHHMMZMONYY

$ sh peer/run.sh
C peer: id 7 at 051430ZNOV26 callsign ALPHA1
C peer: position 2 latlon 51.31 9.48
C peer: seen 051400ZNOV26
C peer: seen 051415ZNOV26

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 DDS-Topic-Typ, in Rust definiert

#[derive(DdsType)] definiert den Topic-Typ in Rust und exportiert seine IDL für C, C++ und jeden anderen Vendor (GitHub #33): eine Datum-Zeit-Gruppe, einen NonZeroU32-Key, eine Union aus einem Enum mit Daten, ein generisches Paar und ein Feld, das leiht.

Was es zeigt

  • Track trägt #[dds(name = "app::Track", extensibility = "final")], #[dds(key)] an id und #[dds(bound = …)] an seinen Strings und der Sequenz; Track::idl() ist die zuerst ausgegebene IDL.
  • enum Position mit Daten ist eine IDL-union auf einem short-Diskriminator; Pair<f64> ist app::Pair_double.
  • build.rs übersetzt die exportierte IDL mit zerodds-build und rust_type — der Weg von rust-type —, und der Lauf prüft, dass beide Wege dieselben Oktette, denselben TypeIdentifier und dasselbe TypeObject ergeben.
  • TrackView<'a> ist derselbe IDL-Typ mit callsign: &'a str: Beim Decodieren wird das Rufzeichen aus dem Sample geliehen statt kopiert.
  • peer/run.sh erzeugt mit zerodds-idlc einen C-Codec aus idl/track.idl und liest das Sample, das die Rust-Seite geschrieben hat.

Bauen und ausführen

cargo run
sh peer/run.sh   # the C peer, generated by zerodds-idlc from idl/track.idl

Ausgabe

exported IDL:
module app { @final struct Pair_double { double first; double second; }; };
module app { @final union Position switch (short) { case 1: string grid; case 2: ::app::Pair_double latlon; }; };
module app { @final struct Track { @key unsigned long id; string<12> at; sequence<string, 8> seen; string<16> callsign; ::app::Position position; }; };
104 octets: 070000000d0000003035313433305a4e4f5632360000000029000000020000000d0000003035313430305a4e4f563236000000000d0000003035313431355a4e4f5632360000000007000000414c5048413100000200000048e17a14aea74940f6285c8fc2f52240
the IDL route writes the same octets, TypeIdentifier and TypeObject
read back: 7 at 051430ZNOV26, LatLon(Pair { first: 51.31, second: 9.48 })
borrowed: ALPHA1 lent from the sample
rejected: at 69: expected a date-time group DDHHMMZMONYY

$ sh peer/run.sh
C peer: id 7 at 051430ZNOV26 callsign ALPHA1
C peer: position 2 latlon 51.31 9.48
C peer: seen 051400ZNOV26
C peer: seen 051415ZNOV26

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