A union with discriminator values without a member

A union whose discriminator has four values, two of which select no member (IDL 4.2 §7.4.1.4.4.4), derived in Rust in two forms and checked against the IDL route (GitHub #33).

What it shows

  • idl/message_payload.idl is the union as the reporter of GitHub #33 wrote it: HEARTBEAT and ACKNOWLEDGEMENT select no member, and a sample with one of them is the discriminator alone.
  • MessagePayload has one unit variant with #[dds(case = …)] per such value; MessagePayloadSignal has one #[dds(no_member)] variant that holds them all. The enum carries extensibility = "final" like the IDL's @final enum.
  • build.rs compiles the IDL through zerodds-build, and the run checks that both forms and the IDL route have one TypeIdentifier, TypeObject and set of dependencies, and write the same octets in XCDR2 and XCDR1, little and big endian, each read back by the other two.
  • The no_member variant refuses Signal(Position), a value a case with a member takes.
  • The last lines are the TypeInformation a peer receives: the minimal and complete TypeIdentifiers with the sizes of their TypeObjects and their dependencies.
#[derive(DdsType)]
#[dds(
    name = "MessagePayload",
    extensibility = "final",
    discriminator = "MessagePayloadDiscriminator"
)]
enum MessagePayload {
    #[dds(case = MessagePayloadDiscriminator::Position, name = "position")]
    Position(Vec3F32),
    #[dds(case = MessagePayloadDiscriminator::Velocity, name = "velocity")]
    Velocity(Vec3F32),
    #[dds(case = MessagePayloadDiscriminator::Heartbeat)]
    Heartbeat,
    #[dds(case = MessagePayloadDiscriminator::Acknowledgement)]
    Acknowledgement,
}

Build and run

cargo run

Output

exported IDL:
@final enum MessagePayloadDiscriminator { @value(0) MESSAGE_PAYLOAD_DISCRIMINATOR_POSITION, MESSAGE_PAYLOAD_DISCRIMINATOR_VELOCITY, MESSAGE_PAYLOAD_DISCRIMINATOR_HEARTBEAT, MESSAGE_PAYLOAD_DISCRIMINATOR_ACKNOWLEDGEMENT };
@final struct Vec3F32 { float x; float y; float z; };
@final union MessagePayload switch (::MessagePayloadDiscriminator) { case ::MESSAGE_PAYLOAD_DISCRIMINATOR_POSITION: ::Vec3F32 position; case ::MESSAGE_PAYLOAD_DISCRIMINATOR_VELOCITY: ::Vec3F32 velocity; };
both forms and the IDL route: one TypeIdentifier, TypeObject and dependencies

samples:
Position(Vec3F32 { x: 1.0, y: 2.0, z: 3.0 }) / Position(Vec3F32 { x: 1.0, y: 2.0, z: 3.0 })
  Xcdr2Le: header 00 07 00 00 | body 00 00 00 00 00 00 80 3f 00 00 00 40 00 00 40 40
  Xcdr2Be: header 00 06 00 00 | body 00 00 00 00 3f 80 00 00 40 00 00 00 40 40 00 00
  Xcdr1Le: header 00 01 00 00 | body 00 00 00 00 00 00 80 3f 00 00 00 40 00 00 40 40
  Xcdr1Be: header 00 00 00 00 | body 00 00 00 00 3f 80 00 00 40 00 00 00 40 40 00 00
Velocity(Vec3F32 { x: 0.5, y: -0.25, z: 9.75 }) / Velocity(Vec3F32 { x: 0.5, y: -0.25, z: 9.75 })
  Xcdr2Le: header 00 07 00 00 | body 01 00 00 00 00 00 00 3f 00 00 80 be 00 00 1c 41
  Xcdr2Be: header 00 06 00 00 | body 00 00 00 01 3f 00 00 00 be 80 00 00 41 1c 00 00
  Xcdr1Le: header 00 01 00 00 | body 01 00 00 00 00 00 00 3f 00 00 80 be 00 00 1c 41
  Xcdr1Be: header 00 00 00 00 | body 00 00 00 01 3f 00 00 00 be 80 00 00 41 1c 00 00
Heartbeat / Signal(Heartbeat)
  Xcdr2Le: header 00 07 00 00 | body 02 00 00 00
  Xcdr2Be: header 00 06 00 00 | body 00 00 00 02
  Xcdr1Le: header 00 01 00 00 | body 02 00 00 00
  Xcdr1Be: header 00 00 00 00 | body 00 00 00 02
Acknowledgement / Signal(Acknowledgement)
  Xcdr2Le: header 00 07 00 00 | body 03 00 00 00
  Xcdr2Be: header 00 06 00 00 | body 00 00 00 03
  Xcdr1Le: header 00 01 00 00 | body 03 00 00 00
  Xcdr1Be: header 00 00 00 00 | body 00 00 00 03

refused: Signal(Position): at 0: expected a discriminator that no case and no default takes, for a union without a member

type information, 196 octets:
  minimal f1 d1 07 9f c3 33 2f 88 48 36 49 ba 91 4c 0f (124 octets)
    depends on f1 a8 27 5b 6c 5a 1f d3 a8 a7 d2 67 27 c5 56 (102 octets)
    depends on f1 bd b3 4b 07 97 70 d5 44 4c da 2d 20 b7 83 (71 octets)
  complete f2 9f bd fa 3b 11 47 bc 33 f0 8c 06 be f6 ed (171 octets)
    depends on f2 77 15 bd 98 8b 77 c6 47 af f4 4f dc d1 f7 (316 octets)
    depends on f2 29 cb 48 09 5f 39 6b bd 72 b6 b6 c2 00 47 (96 octets)

Output of a run on the Linux test host.

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

Eine Union mit Diskriminatorwerten ohne Member

Eine Union, deren Diskriminator vier Werte hat, von denen zwei kein Member wählen (IDL 4.2 §7.4.1.4.4.4), in Rust in zwei Formen abgeleitet und gegen den IDL-Weg geprüft (GitHub #33).

Was es zeigt

  • idl/message_payload.idl ist die Union, wie sie der Melder von GitHub #33 geschrieben hat: HEARTBEAT und ACKNOWLEDGEMENT wählen kein Member, ein Sample mit einem von ihnen ist allein der Diskriminator.
  • MessagePayload hat für jeden solchen Wert eine Unit-Variante mit #[dds(case = …)]; MessagePayloadSignal hat eine #[dds(no_member)]-Variante, die alle aufnimmt. Das Enum trägt extensibility = "final" wie das @final enum der IDL.
  • build.rs übersetzt die IDL mit zerodds-build, und der Lauf prüft, dass beide Formen und der IDL-Weg einen TypeIdentifier, ein TypeObject und dieselben Abhängigkeiten haben und in XCDR2 und XCDR1, Little und Big Endian, dieselben Oktette schreiben, die jeweils die beiden anderen zurücklesen.
  • Die no_member-Variante lehnt Signal(Position) ab, einen Wert, den ein Case mit Member nimmt.
  • Die letzten Zeilen sind die TypeInformation, die ein Gegenüber erhält: die minimalen und vollständigen TypeIdentifier mit den Größen ihrer TypeObjects und ihren Abhängigkeiten.
#[derive(DdsType)]
#[dds(
    name = "MessagePayload",
    extensibility = "final",
    discriminator = "MessagePayloadDiscriminator"
)]
enum MessagePayload {
    #[dds(case = MessagePayloadDiscriminator::Position, name = "position")]
    Position(Vec3F32),
    #[dds(case = MessagePayloadDiscriminator::Velocity, name = "velocity")]
    Velocity(Vec3F32),
    #[dds(case = MessagePayloadDiscriminator::Heartbeat)]
    Heartbeat,
    #[dds(case = MessagePayloadDiscriminator::Acknowledgement)]
    Acknowledgement,
}

Bauen und ausführen

cargo run

Ausgabe

exported IDL:
@final enum MessagePayloadDiscriminator { @value(0) MESSAGE_PAYLOAD_DISCRIMINATOR_POSITION, MESSAGE_PAYLOAD_DISCRIMINATOR_VELOCITY, MESSAGE_PAYLOAD_DISCRIMINATOR_HEARTBEAT, MESSAGE_PAYLOAD_DISCRIMINATOR_ACKNOWLEDGEMENT };
@final struct Vec3F32 { float x; float y; float z; };
@final union MessagePayload switch (::MessagePayloadDiscriminator) { case ::MESSAGE_PAYLOAD_DISCRIMINATOR_POSITION: ::Vec3F32 position; case ::MESSAGE_PAYLOAD_DISCRIMINATOR_VELOCITY: ::Vec3F32 velocity; };
both forms and the IDL route: one TypeIdentifier, TypeObject and dependencies

samples:
Position(Vec3F32 { x: 1.0, y: 2.0, z: 3.0 }) / Position(Vec3F32 { x: 1.0, y: 2.0, z: 3.0 })
  Xcdr2Le: header 00 07 00 00 | body 00 00 00 00 00 00 80 3f 00 00 00 40 00 00 40 40
  Xcdr2Be: header 00 06 00 00 | body 00 00 00 00 3f 80 00 00 40 00 00 00 40 40 00 00
  Xcdr1Le: header 00 01 00 00 | body 00 00 00 00 00 00 80 3f 00 00 00 40 00 00 40 40
  Xcdr1Be: header 00 00 00 00 | body 00 00 00 00 3f 80 00 00 40 00 00 00 40 40 00 00
Velocity(Vec3F32 { x: 0.5, y: -0.25, z: 9.75 }) / Velocity(Vec3F32 { x: 0.5, y: -0.25, z: 9.75 })
  Xcdr2Le: header 00 07 00 00 | body 01 00 00 00 00 00 00 3f 00 00 80 be 00 00 1c 41
  Xcdr2Be: header 00 06 00 00 | body 00 00 00 01 3f 00 00 00 be 80 00 00 41 1c 00 00
  Xcdr1Le: header 00 01 00 00 | body 01 00 00 00 00 00 00 3f 00 00 80 be 00 00 1c 41
  Xcdr1Be: header 00 00 00 00 | body 00 00 00 01 3f 00 00 00 be 80 00 00 41 1c 00 00
Heartbeat / Signal(Heartbeat)
  Xcdr2Le: header 00 07 00 00 | body 02 00 00 00
  Xcdr2Be: header 00 06 00 00 | body 00 00 00 02
  Xcdr1Le: header 00 01 00 00 | body 02 00 00 00
  Xcdr1Be: header 00 00 00 00 | body 00 00 00 02
Acknowledgement / Signal(Acknowledgement)
  Xcdr2Le: header 00 07 00 00 | body 03 00 00 00
  Xcdr2Be: header 00 06 00 00 | body 00 00 00 03
  Xcdr1Le: header 00 01 00 00 | body 03 00 00 00
  Xcdr1Be: header 00 00 00 00 | body 00 00 00 03

refused: Signal(Position): at 0: expected a discriminator that no case and no default takes, for a union without a member

type information, 196 octets:
  minimal f1 d1 07 9f c3 33 2f 88 48 36 49 ba 91 4c 0f (124 octets)
    depends on f1 a8 27 5b 6c 5a 1f d3 a8 a7 d2 67 27 c5 56 (102 octets)
    depends on f1 bd b3 4b 07 97 70 d5 44 4c da 2d 20 b7 83 (71 octets)
  complete f2 9f bd fa 3b 11 47 bc 33 f0 8c 06 be f6 ed (171 octets)
    depends on f2 77 15 bd 98 8b 77 c6 47 af f4 4f dc d1 f7 (316 octets)
    depends on f2 29 cb 48 09 5f 39 6b bd 72 b6 b6 c2 00 47 (96 octets)

Ausgabe eines Laufs auf dem Linux-Testhost.

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