cyclonedds 3.0.0-alpha.3

Safe Rust wrapper for Eclipse CycloneDDS
Documentation
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use crate::{
    entity::{DdsEntity, OwnedEntity, OwnedHandle},
    write_arena::WriteArena,
    xtypes::TopicDescriptor,
    DdsError, DdsResult, Qos,
};
use cyclonedds_rust_sys::*;
use std::ffi::c_void;
use std::ffi::CString;
use std::marker::PhantomData;
use std::sync::Arc;

/// A topic without compile-time type information.
pub struct UntypedTopic {
    inner: Arc<OwnedEntity>,
}

impl UntypedTopic {
    pub(crate) fn adopt(entity: dds_entity_t, parents: Vec<Arc<OwnedEntity>>) -> Self {
        Self {
            inner: OwnedEntity::new(entity, "UntypedTopic", None, parents),
        }
    }

    /// Attach a parent to a topic that was just built without one.
    ///
    /// The `Arc` has to still be unique, which it is for every crate-internal
    /// caller: they call this on the value returned by a constructor, before it
    /// reaches anyone else. If that ever stopped holding, the topic would go
    /// back to not owning its participant — no worse than before this change,
    /// but wrong, so `debug_assert` makes it loud in tests rather than silent.
    pub(crate) fn retaining(mut self, parent: Arc<OwnedEntity>) -> Self {
        match Arc::get_mut(&mut self.inner) {
            Some(inner) => inner.push_parent(parent),
            None => debug_assert!(false, "retaining() on a shared UntypedTopic"),
        }
        self
    }

    /// # Safety
    ///
    /// Takes a raw participant handle and does not hold it alive. Prefer
    /// [`DomainParticipant::create_topic_from_descriptor`], which does.
    ///
    /// [`DomainParticipant::create_topic_from_descriptor`]: crate::DomainParticipant::create_topic_from_descriptor
    pub unsafe fn from_descriptor(
        participant: dds_entity_t,
        name: &str,
        descriptor: &TopicDescriptor,
    ) -> DdsResult<Self> {
        unsafe { Self::from_descriptor_with_qos(participant, name, descriptor, None) }
    }

    /// # Safety
    ///
    /// `participant` must identify a live domain participant and must outlive
    /// the returned topic. `descriptor` and `qos`, when present, must be valid
    /// for the duration of the CycloneDDS creation call.
    pub unsafe fn from_descriptor_with_qos(
        participant: dds_entity_t,
        name: &str,
        descriptor: &TopicDescriptor,
        qos: Option<&Qos>,
    ) -> DdsResult<Self> {
        Self::create(participant, name, descriptor, qos, Vec::new())
    }

    pub(crate) fn create(
        participant: dds_entity_t,
        name: &str,
        descriptor: &TopicDescriptor,
        qos: Option<&Qos>,
        parents: Vec<Arc<OwnedEntity>>,
    ) -> DdsResult<Self> {
        let topic_name = CString::new(name)
            .map_err(|_| DdsError::BadParameter("topic name contains null".into()))?;
        unsafe {
            let handle = dds_create_topic(
                participant,
                descriptor.as_ptr(),
                topic_name.as_ptr(),
                qos.map_or(std::ptr::null(), |q| q.as_ptr()),
                std::ptr::null(),
            );
            crate::error::check_entity(handle)?;
            Ok(Self {
                inner: OwnedEntity::new(handle, "UntypedTopic", None, parents),
            })
        }
    }
}

impl DdsEntity for UntypedTopic {
    fn entity(&self) -> dds_entity_t {
        self.inner.handle()
    }
}

impl OwnedHandle for UntypedTopic {
    fn owned(&self) -> &Arc<OwnedEntity> {
        &self.inner
    }
}

pub struct Topic<T> {
    inner: Arc<OwnedEntity>,
    _holder: Arc<DescriptorHolder>,
    _marker: PhantomData<T>,
}

struct DescriptorHolder {
    _ops: Vec<u32>,
    _typename: CString,
    _key_names: Vec<CString>,
    _keys: Vec<dds_key_descriptor>,
    _meta: CString,
}

// Segurança: o holder é imutável após a criação do tópico. Os ponteiros crus em
// `_keys` apontam para os buffers das `CString` em `_key_names` (mesma heap, sem
// realocação depois de publicado), e o CycloneDDS copia o que precisa em
// `dds_create_topic`. Compartilhar entre threads é seguro (somente leitura).
unsafe impl Send for DescriptorHolder {}
unsafe impl Sync for DescriptorHolder {}

pub struct TopicKeyDescriptor {
    pub name: String,
    pub offset: u32,
    pub index: u32,
}

pub const OP_RTS: u32 = dds_stream_opcode_DDS_OP_RTS;
pub const OP_DLC: u32 = dds_stream_opcode_DDS_OP_DLC;
pub const OP_ADR: u32 = dds_stream_opcode_DDS_OP_ADR;
pub const OP_JEQ4: u32 = dds_stream_opcode_DDS_OP_JEQ4;

pub const VAL_1BY: u32 = dds_stream_typecode_DDS_OP_VAL_1BY;
pub const VAL_2BY: u32 = dds_stream_typecode_DDS_OP_VAL_2BY;
pub const VAL_4BY: u32 = dds_stream_typecode_DDS_OP_VAL_4BY;
pub const VAL_8BY: u32 = dds_stream_typecode_DDS_OP_VAL_8BY;
pub const VAL_STR: u32 = dds_stream_typecode_DDS_OP_VAL_STR;
pub const VAL_BST: u32 = dds_stream_typecode_DDS_OP_VAL_BST;
pub const VAL_SEQ: u32 = dds_stream_typecode_DDS_OP_VAL_SEQ;
pub const VAL_BSQ: u32 = dds_stream_typecode_DDS_OP_VAL_BSQ;
pub const VAL_ARR: u32 = dds_stream_typecode_DDS_OP_VAL_ARR;

pub const TYPE_1BY: u32 = VAL_1BY << 16;
pub const TYPE_2BY: u32 = VAL_2BY << 16;
pub const TYPE_4BY: u32 = VAL_4BY << 16;
pub const TYPE_8BY: u32 = VAL_8BY << 16;
pub const TYPE_STR: u32 = VAL_STR << 16;
pub const TYPE_BST: u32 = VAL_BST << 16;
pub const TYPE_SEQ: u32 = VAL_SEQ << 16;
pub const TYPE_BSQ: u32 = VAL_BSQ << 16;
pub const TYPE_ARR: u32 = VAL_ARR << 16;
pub const TYPE_ENU: u32 = dds_stream_typecode_primary_DDS_OP_TYPE_ENU;
pub const TYPE_EXT: u32 = dds_stream_typecode_primary_DDS_OP_TYPE_EXT;
pub const TYPE_UNI: u32 = dds_stream_typecode_primary_DDS_OP_TYPE_UNI;

pub const SUBTYPE_1BY: u32 = dds_stream_typecode_subtype_DDS_OP_SUBTYPE_1BY;
pub const SUBTYPE_2BY: u32 = dds_stream_typecode_subtype_DDS_OP_SUBTYPE_2BY;
pub const SUBTYPE_4BY: u32 = dds_stream_typecode_subtype_DDS_OP_SUBTYPE_4BY;
pub const SUBTYPE_8BY: u32 = dds_stream_typecode_subtype_DDS_OP_SUBTYPE_8BY;
pub const SUBTYPE_STR: u32 = dds_stream_typecode_subtype_DDS_OP_SUBTYPE_STR;
pub const SUBTYPE_BST: u32 = dds_stream_typecode_subtype_DDS_OP_SUBTYPE_BST;
pub const SUBTYPE_SEQ: u32 = dds_stream_typecode_subtype_DDS_OP_SUBTYPE_SEQ;
pub const SUBTYPE_BSQ: u32 = dds_stream_typecode_subtype_DDS_OP_SUBTYPE_BSQ;
pub const SUBTYPE_STU: u32 = dds_stream_typecode_subtype_DDS_OP_SUBTYPE_STU;
pub const SUBTYPE_ENU: u32 = dds_stream_typecode_subtype_DDS_OP_SUBTYPE_ENU;
pub const OP_FLAG_SZ_SHIFT: u32 = DDS_OP_FLAG_SZ_SHIFT;
pub const DDS_OP_MASK_CONST: u32 = DDS_OP_MASK;
pub const DDS_OP_TYPE_MASK_CONST: u32 = DDS_OP_TYPE_MASK;
pub const DDS_OP_SUBTYPE_MASK_CONST: u32 = DDS_OP_SUBTYPE_MASK;

/// ADR flags (low 8 bits of the ADR opcode)
pub const OP_FLAG_KEY: u32 = 1u32 << 0;
pub const OP_FLAG_FP: u32 = DDS_OP_FLAG_FP;
pub const OP_FLAG_SGN: u32 = 1u32 << 2;
pub const OP_FLAG_EXT: u32 = DDS_OP_FLAG_EXT;
pub const OP_FLAG_MU: u32 = DDS_OP_FLAG_MU;
/// Set on a `TYPE_UNI` header when the union declares a default case.
///
/// `dds_opcodes.h`: `[ADR, UNI, d, z] [offset] [alen] [next-insn, cases]`, where
/// `z` is this flag.
pub const OP_FLAG_DEF: u32 = DDS_OP_FLAG_DEF;
pub const OP_FLAG_OPT: u32 = DDS_OP_FLAG_OPT;

/// Key Offset Format opcode.
/// Format: `[KOF | n, adr_ops_index_0, ..., adr_ops_index_(n-1)]`
/// where n = number of ADR opcode indices that contribute to this key path.
/// For a flat key field (single member), n=1: `[KOF | 1, adr_index]`.
/// KOF entries are placed AFTER OP_RTS in the ops array.
pub const OP_KOF: u32 = 0x07 << 24;
pub const OP_MID: u32 = dds_stream_opcode_DDS_OP_MID;

/// Describes the native layout consumed by CycloneDDS for a Rust sample type.
///
/// Implementing this contract requires proving layout, initialization and
/// clone-out invariants. A normal safe implementation must therefore be
/// rejected by the compiler:
///
/// ```compile_fail
/// use cyclonedds::DdsType;
///
/// struct Invalid;
///
/// impl DdsType for Invalid {
///     type Native = String;
///
///     fn type_name() -> &'static str { "Invalid" }
///     fn ops() -> Vec<u32> { Vec::new() }
/// }
/// ```
///
/// # Safety
///
/// `Native` must match the size, alignment and opcode layout announced to
/// CycloneDDS, its all-zero bit pattern must be valid, `write_to_native` must
/// return that representation, and `clone_out` must produce an owned `Self`
/// from a valid native sample without retaining borrowed DDS memory.
pub unsafe trait DdsType: Sized + Send + 'static {
    /// The DDS wire-compatible representation of this type — what CycloneDDS's
    /// topic descriptor (`m_size`/`m_ops`, see [`DdsType::descriptor_size`]) and
    /// the loan APIs ([`crate::DataWriter::request_loan`]) actually operate on.
    ///
    /// For types with no heap-allocated fields this is typically `Self`. For
    /// types with `String`/`Vec` fields (translated by `#[derive(DdsTypeDerive)]`
    /// to `DdsString`/`DdsSequence`), `Native` is a distinct, smaller,
    /// wire-compatible struct — `size_of::<Native>()` is what
    /// `descriptor_size()` reports and what CycloneDDS actually allocates for a
    /// loan, which is *not* the same as `size_of::<Self>()` in that case.
    /// Getting this wrong was the root cause of a real out-of-bounds write in
    /// `request_loan()` (fixed together with this associated type — see
    /// `tese/src/rust`'s `OPTIMIZATION_PLAN.md` Fase 4 for the full writeup).
    type Native: Sized;

    fn type_name() -> &'static str;
    fn ops() -> Vec<u32>;
    /// Size CycloneDDS allocates for one sample of this type.
    ///
    /// This is the size of [`DdsType::Native`], not of `Self` — they differ for
    /// any type with `String`/`Vec` fields, where the wire representation uses
    /// `DdsString` (8 bytes) / `DdsSequence`. The default used to be
    /// `size_of::<Self>()`, which is only correct when `Native = Self`: a manual
    /// `impl` that declared a smaller `Native` and relied on the default
    /// reintroduced the heap overflow fixed in 2.0.0, since `dds_request_loan`
    /// allocates this many bytes and `request_loan` then zero-initializes
    /// `size_of::<Native>()` of them.
    fn descriptor_size() -> u32 {
        std::mem::size_of::<Self::Native>() as u32
    }
    /// Alignment CycloneDDS uses for one sample. See [`DdsType::descriptor_size`].
    fn descriptor_align() -> u32 {
        std::mem::align_of::<Self::Native>() as u32
    }
    /// # Safety
    ///
    /// `ptr` must point to a valid sample instance of `Self` produced by
    /// CycloneDDS for the current topic descriptor. Implementations must return
    /// an owned Rust value that remains valid after any associated DDS loan is
    /// returned.
    ///
    /// Returns `Err` when the sample cannot be represented as `Self` — the case
    /// that matters is a union carrying a discriminator outside its declared
    /// set, which arrives from the network and so is remote input. That used to
    /// be a `panic!`, contained at the FFI boundary by `catch_unwind` but still
    /// enough for a peer built from a different IDL revision to make
    /// `reader.take()` unwind on the caller's thread. An undecodable sample is
    /// now a discarded error instead.
    unsafe fn clone_out(ptr: *const Self) -> DdsResult<Self> {
        Ok(std::ptr::read(ptr))
    }
    fn write_to_native<'a>(&'a self, _arena: &'a mut WriteArena) -> DdsResult<*const c_void> {
        Ok(self as *const Self as *const c_void)
    }
    fn key_count() -> usize {
        0
    }
    fn keys() -> Vec<KeyDescriptor> {
        Vec::new()
    }
    fn flagset() -> u32 {
        0
    }
    fn post_key_ops() -> Vec<u32> {
        Vec::new()
    }
    /// XCDR2-serialized (TypeInformation, TypeMapping) blobs for XTypes/SEDP.
    ///
    /// Types generated from the canonical IDL carry the blobs produced by the
    /// C `idlc`; returning `Some` makes the topic descriptor set
    /// `DDS_TOPIC_XTYPES_METADATA` and announce type information, which
    /// type-enforcing peers (Python/C++) require for matching.
    fn type_metadata_blobs() -> Option<(&'static [u8], &'static [u8])> {
        None
    }
}

/// Value-level conversion from a Rust sample to its [`DdsType::Native`] layout.
///
/// [`DdsType::write_to_native`] produces a *pointer* into a [`WriteArena`], which
/// is all a top-level write needs. Building the element buffer of a
/// `sequence<Struct>` needs the value itself, because the elements have to sit
/// contiguously with the stride CycloneDDS was told about — and that stride is
/// `size_of::<Native>()`, not `size_of::<Self>()`, for any element type with
/// `String`/`Vec` fields.
///
/// This is a separate trait, and not another method on [`DdsType`], so that the
/// manual `impl DdsType` blocks that already exist for POD types keep compiling.
/// The derive implements it for every type it generates; a hand-written type
/// used as a composite element without it fails with a missing-bound error at
/// compile time rather than mis-serializing at run time.
pub trait DdsNativeValue: DdsType {
    fn to_native_value(&self, arena: &mut WriteArena) -> DdsResult<Self::Native>;
}

pub trait DdsEnumType: Sized + Copy + Send + 'static {
    fn max_discriminant() -> u32;
    fn enum_op_flags() -> u32 {
        2u32 << OP_FLAG_SZ_SHIFT
    }
}

pub trait DdsUnionType: Sized + Send + 'static {
    fn discriminant_type() -> DiscriminantType;
    fn case_count() -> u32;
    fn has_default() -> bool;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiscriminantType {
    Bool,
    U8,
    I8,
    U16,
    I16,
    U32,
    I32,
    U64,
    I64,
}

/// Describes a key field for a keyed topic. `ops_index` is the index into the ops array
/// path that contributes to this key. For flat keys, `ops_path` has one entry.
/// For nested keys, it contains the parent field ADR index followed by the
/// nested key path indices.
pub struct KeyDescriptor {
    pub name: String,
    pub ops_path: Vec<u32>,
}

pub fn adr(typecode: u32, offset: u32) -> Vec<u32> {
    vec![OP_ADR | typecode, offset]
}

pub fn adr_key(typecode: u32, offset: u32) -> Vec<u32> {
    vec![OP_ADR | OP_FLAG_KEY | typecode, offset]
}

pub fn adr_bst(offset: u32, bound: u32) -> Vec<u32> {
    vec![OP_ADR | TYPE_BST, offset, bound]
}

pub fn rebase_ops(mut ops: Vec<u32>, base_offset: u32) -> Vec<u32> {
    let mut i = 0usize;
    while i < ops.len() {
        let op = ops[i];
        if (op & DDS_OP_MASK) != OP_ADR {
            i += 1;
            continue;
        }

        if i + 1 >= ops.len() {
            break;
        }
        ops[i + 1] = ops[i + 1].saturating_add(base_offset);

        let primary = op & DDS_OP_TYPE_MASK;
        let subtype = op & DDS_OP_SUBTYPE_MASK;
        i += match primary {
            TYPE_BST => 3,
            TYPE_SEQ if subtype == SUBTYPE_BST => 3,
            TYPE_SEQ => 2,
            TYPE_BSQ if subtype == SUBTYPE_BST => 4,
            TYPE_BSQ => 3,
            _ => 2,
        };
    }
    ops
}

impl<T: DdsType> Topic<T> {
    /// Create a topic on `participant`.
    ///
    /// Takes the participant by reference rather than as a raw
    /// `dds_entity_t`. The handle-based form let a temporary supply it --
    /// `Topic::new(DomainParticipant::new(0)?.entity(), "x")` compiles, deletes
    /// the participant at the end of the statement, and leaves the topic on a
    /// handle CycloneDDS is free to recycle. See [`Topic::from_entity`] for the
    /// escape hatch when the handle genuinely comes from elsewhere.
    pub fn new(participant: &crate::DomainParticipant, name: &str) -> DdsResult<Self> {
        Self::with_qos(participant, name, None)
    }

    pub fn with_qos(
        participant: &crate::DomainParticipant,
        name: &str,
        qos: Option<&Qos>,
    ) -> DdsResult<Self> {
        Self::create(
            participant.entity(),
            name,
            qos,
            vec![participant.owned().clone()],
        )
    }

    /// Create a topic from a raw participant handle.
    ///
    /// # Safety
    ///
    /// Escape hatch for handles obtained outside this crate (FFI interop).
    /// Unlike [`Topic::new`], the returned topic does **not** hold the
    /// participant alive: the caller guarantees the handle is a live
    /// participant and outlives the topic.
    pub unsafe fn from_entity(participant: dds_entity_t, name: &str) -> DdsResult<Self> {
        unsafe { Self::with_qos_from_entity(participant, name, None) }
    }

    /// Create a topic from a raw participant handle with QoS.
    ///
    /// # Safety
    ///
    /// The requirements of [`Topic::from_entity`] apply unchanged.
    pub unsafe fn with_qos_from_entity(
        participant: dds_entity_t,
        name: &str,
        qos: Option<&Qos>,
    ) -> DdsResult<Self> {
        Self::create(participant, name, qos, Vec::new())
    }

    pub(crate) fn create(
        participant: dds_entity_t,
        name: &str,
        qos: Option<&Qos>,
        parents: Vec<Arc<OwnedEntity>>,
    ) -> DdsResult<Self> {
        unsafe {
            let type_name = CString::new(T::type_name())
                .map_err(|_| DdsError::BadParameter("type name contains null".into()))?;
            let topic_name = CString::new(name)
                .map_err(|_| DdsError::BadParameter("topic name contains null".into()))?;

            // Ops layout (flat struct, matching idlc output):
            //   [ADR..., ADR..., ..., OP_RTS, KOF|1, adr_idx, KOF|1, adr_idx, ...]
            // No OP_DLC for flat types. KOF entries go AFTER OP_RTS.
            let mut ops = T::ops();
            if ops.last().copied() != Some(OP_RTS) {
                ops.push(OP_RTS);
            }

            let key_defs = T::keys();
            // A key name with an interior NUL is bad input, not a bug: report
            // it like the topic name a few lines above rather than panicking.
            let key_names: Vec<CString> = key_defs
                .iter()
                .map(|k| {
                    CString::new(k.name.as_str())
                        .map_err(|_| DdsError::BadParameter("key name contains null".into()))
                })
                .collect::<DdsResult<_>>()?;
            let mut keys: Vec<dds_key_descriptor> = Vec::with_capacity(key_defs.len());
            for (i, kd) in key_defs.iter().enumerate() {
                let offset = ops.len() as u32;
                ops.push(OP_KOF | (kd.ops_path.len() as u32));
                ops.extend(kd.ops_path.iter().copied());
                keys.push(dds_key_descriptor {
                    m_name: key_names[i].as_ptr(),
                    m_offset: offset,
                    m_idx: i as u32,
                });
            }

            let post_key_ops = T::post_key_ops();
            if !post_key_ops.is_empty() {
                ops.extend(post_key_ops);
            }
            let meta = CString::default();

            // Blobs XTypes (TypeInformation/TypeMapping do idlc) quando o tipo os tem:
            // habilita DDS_TOPIC_XTYPES_METADATA para o SEDP anunciar type info.
            let (flagset, type_information, type_mapping) = match T::type_metadata_blobs() {
                Some((info, map)) => (
                    T::flagset() | DDS_TOPIC_XTYPES_METADATA,
                    dds_type_meta_ser {
                        data: info.as_ptr(),
                        sz: info.len() as u32,
                    },
                    dds_type_meta_ser {
                        data: map.as_ptr(),
                        sz: map.len() as u32,
                    },
                ),
                None => (T::flagset(), std::mem::zeroed(), std::mem::zeroed()),
            };

            let descriptor = dds_topic_descriptor {
                m_size: T::descriptor_size(),
                m_align: T::descriptor_align(),
                m_flagset: flagset,
                m_nkeys: T::key_count() as u32,
                m_typename: type_name.as_ptr(),
                m_keys: if keys.is_empty() {
                    std::ptr::null()
                } else {
                    keys.as_ptr()
                },
                m_nops: ops.len() as u32,
                m_ops: ops.as_ptr(),
                m_meta: meta.as_ptr(),
                type_information,
                type_mapping,
                restrict_data_representation: 0,
            };

            let handle = dds_create_topic(
                participant,
                &descriptor,
                topic_name.as_ptr(),
                qos.map_or(std::ptr::null(), |q| q.as_ptr()),
                std::ptr::null(),
            );
            crate::error::check_entity(handle)?;

            let holder = DescriptorHolder {
                _ops: ops,
                _typename: type_name,
                _key_names: key_names,
                _keys: keys,
                _meta: meta,
            };

            Ok(Topic {
                inner: OwnedEntity::new(handle, "Topic", None, parents),
                _holder: Arc::new(holder),
                _marker: PhantomData,
            })
        }
    }
}

impl<T> DdsEntity for Topic<T> {
    fn entity(&self) -> dds_entity_t {
        self.inner.handle()
    }
}

impl<T> OwnedHandle for Topic<T> {
    fn owned(&self) -> &Arc<OwnedEntity> {
        &self.inner
    }
}