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, aNonZeroU32key, a union from an enum with data, a generic pair and a field that borrows.
- Example:
rust-derive - Repository: zero-dds-snippets
What it shows
Trackcarries#[dds(name = "app::Track", extensibility = "final")],#[dds(key)]onidand#[dds(bound = …)]on its strings and sequence;Track::idl()is the IDL printed first.enum Positionwith data is an IDLunionon ashortdiscriminator;Pair<f64>isapp::Pair_double.build.rscompiles the exported IDL throughzerodds-buildwithrust_type— the route of rust-type — and the run checks that both routes give the same octets,TypeIdentifierandTypeObject.TrackView<'a>is the same IDL type withcallsign: &'a str: decoding it lends the call sign out of the sample instead of copying it.peer/run.shgenerates a C codec fromidl/track.idlwithzerodds-idlcand 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
- rust-type, the IDL route
- derive(DdsType)
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, einenNonZeroU32-Key, eine Union aus einem Enum mit Daten, ein generisches Paar und ein Feld, das leiht.
- Beispiel:
rust-derive - Repository: zero-dds-snippets
Was es zeigt
Trackträgt#[dds(name = "app::Track", extensibility = "final")],#[dds(key)]anidund#[dds(bound = …)]an seinen Strings und der Sequenz;Track::idl()ist die zuerst ausgegebene IDL.enum Positionmit Daten ist eine IDL-unionauf einemshort-Diskriminator;Pair<f64>istapp::Pair_double.build.rsübersetzt die exportierte IDL mitzerodds-buildundrust_type— der Weg von rust-type —, und der Lauf prüft, dass beide Wege dieselben Oktette, denselbenTypeIdentifierund dasselbeTypeObjectergeben.TrackView<'a>ist derselbe IDL-Typ mitcallsign: &'a str: Beim Decodieren wird das Rufzeichen aus dem Sample geliehen statt kopiert.peer/run.sherzeugt mitzerodds-idlceinen C-Codec ausidl/track.idlund 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
- rust-type, der IDL-Weg
- derive(DdsType)