1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! Serde-based DDS sample wrapper.
//!
//! When the `serde` feature is enabled, [`SerdeSample<T>`] provides a
//! [`DdsType`] implementation for any `T: Serialize + DeserializeOwned`.
//! The sample is serialized to a compact binary format using ` postcard`
//! and transmitted as a variable-length byte sequence.
//!
//! # Interoperability Note
//!
//! `SerdeSample` uses ` postcard` encoding, **not** OMG CDR/XCDR.
//! This means it can only communicate with other Rust nodes using the
//! same wrapper, or with a bridge that translates the format.
//!
//! # Example
//!
//! ```no_run
//! use cyclonedds::{serde_type_name, SerdeSample};
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Serialize, Deserialize, Clone, Debug)]
//! struct MyMessage {
//! id: u32,
//! text: String,
//! }
//!
//! serde_type_name!(MyMessage, "example::MyMessage");
//!
//! let participant = cyclonedds::DomainParticipant::new(0).unwrap();
//! let topic = participant
//! .create_topic::<SerdeSample<MyMessage>>("MyTopic")
//! .unwrap();
//! ```
use crate::;
use c_void;
/// The DDS type name announced for `SerdeSample<T>`.
///
/// A DDS type name is part of the contract between peers: two participants match
/// a topic on it, so it has to be the same string in every process that talks
/// about the same `T`, including processes built from a different revision of the
/// code by a different compiler.
///
/// That rules out deriving it from Rust internals. `stringify!(T)` in a generic
/// impl expands to the literal `"T"`, which is what shipped: every
/// `SerdeSample<X>` announced the same name, so unrelated payload types matched
/// each other on the wire and each decoded the other's bytes as its own.
/// `std::any::type_name` is no better — it carries crate paths and has no
/// stability guarantee across compilations.
///
/// So the name is supplied, not inferred. Use [`serde_type_name!`](crate::serde_type_name) for the
/// common case:
///
/// ```
/// use cyclonedds::{serde_type_name, SerdeSample};
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize)]
/// struct Telemetry { id: u32 }
///
/// serde_type_name!(Telemetry, "acme::Telemetry");
/// ```
///
/// Choose the name the way you would choose it in IDL — scoped, and versioned if
/// the shape of `T` may change — because that is exactly what it is.
/// Give a type the DDS type name its [`SerdeSample`] will announce.
///
/// See [`SerdeTypeName`] for why this cannot be inferred.
/// A DDS sample that wraps any serde-compatible type.
///
/// `SerdeSample<T>` stores the serialized payload internally and
/// implements [`DdsType`] so it can be used with [`Topic`](crate::Topic),
/// [`DataWriter`](crate::DataWriter) and [`DataReader`](crate::DataReader).
/// Wire representation of a [`SerdeSample`]: what the `ADR | SEQ | 1BY` op in
/// [`SerdeSample::ops`] actually describes.
///
/// This exists because the previous implementation used `Native = Self` and
/// handed the Rust `SerdeSample` straight to CycloneDDS. That is unsound three
/// ways over: the ops declare a DDS byte sequence, so the C side reads the
/// buffer as `dds_sequence_t` (`{u32 _maximum, u32 _length, *mut u8 _buffer,
/// bool _release}`) while the memory actually held a `Vec<u8>`; `Vec`'s field
/// order is `repr(Rust)` and explicitly not guaranteed, so even the sizes
/// matching was luck; and `clone_out` did `ptr::read`, taking Rust ownership of
/// a buffer allocated by `dds_alloc`, which Rust would then free with its own
/// allocator.
///
/// `DdsSequence<u8>` is the layout CycloneDDS expects, and the conversions below
/// mirror exactly what `#[derive(DdsTypeDerive)]` generates for a `Vec<u8>`
/// field.
unsafe