eclipse-cyclonedds 0.0.1

Rust binding for Eclipse Cyclone DDS
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
//! Event metadata types delivered to DDS listener callbacks.
//!
//! Each type corresponds to a status condition defined in the DCPS
//! specification and carries event-specific detail such as counts and instance
//! handles. See the [`listener`](crate::listener) module for how to register
//! callbacks that receive these types.

pub(crate) mod bitflags {
    bitflags::bitflags! {
        /// Flags for specifying the set of statuses that are of interest.
        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
        pub struct Status: u32 {
            /// Another topic exists with the same name but with different
            /// characteristics. Also see the [`crate::status::InconsistentTopic`] metadata struct.
            const InconsistentTopic =
                1 << cyclonedds_sys::dds_status_id_DDS_INCONSISTENT_TOPIC_STATUS_ID;
            /// The deadline that the writer has committed through its
            /// [`Deadline`](crate::qos::policy::Deadline) policy was not
            /// respected for a specific instance. Also see the [`crate::status::OfferedDeadlineMissed`] metadata struct.
            const OfferedDeadlineMissed =
                1 << cyclonedds_sys::dds_status_id_DDS_OFFERED_DEADLINE_MISSED_STATUS_ID;
            /// The deadline that the reader was expecting through its
            /// [`Deadline`](crate::qos::policy::Deadline) policy was not
            /// respected for a specific instance. Also see the [`crate::status::RequestedDeadlineMissed`] metadata struct.
            const RequestedDeadlineMissed =
                1 << cyclonedds_sys::dds_status_id_DDS_REQUESTED_DEADLINE_MISSED_STATUS_ID;
            /// A [`QoS`](crate::QoS) policy setting was incompatible with what
            /// was requested. Also see the [`crate::status::OfferedIncompatibleQoS`] metadata struct.
            const OfferedIncompatibleQoS =
                1 << cyclonedds_sys::dds_status_id_DDS_OFFERED_INCOMPATIBLE_QOS_STATUS_ID;
            /// A [`QoS`](crate::QoS) policy setting was incompatible with what
            /// is offered. Also see the [`crate::status::RequestedIncompatibleQoS`] metadata struct.
            const RequestedIncompatibleQoS =
                1 << cyclonedds_sys::dds_status_id_DDS_REQUESTED_INCOMPATIBLE_QOS_STATUS_ID;
            /// A sample has been lost (never received). Also see the [`crate::status::SampleLost`] metadata struct.
            const SampleLost =
                1 << cyclonedds_sys::dds_status_id_DDS_SAMPLE_LOST_STATUS_ID;
            /// A received sample has been rejected. Also see the [`crate::status::SampleRejected`] metadata struct.
            const SampleRejected =
                1 << cyclonedds_sys::dds_status_id_DDS_SAMPLE_REJECTED_STATUS_ID;
            /// New information is available in some of the data readers of a
            /// subscriber.
            const DataOnReaders =
                1 << cyclonedds_sys::dds_status_id_DDS_DATA_ON_READERS_STATUS_ID;
            /// New information is available in a data reader.
            const DataAvailable =
                1 << cyclonedds_sys::dds_status_id_DDS_DATA_AVAILABLE_STATUS_ID;
            /// The liveliness that the writer has committed through its
            /// [`Liveliness`](crate::qos::policy::Liveliness) policy was not
            /// respected; thus readers will consider the writer as no longer
            /// "alive". Also see the [`crate::status::LivelinessLost`] metadata struct.
            const LivelinessLost =
                1 << cyclonedds_sys::dds_status_id_DDS_LIVELINESS_LOST_STATUS_ID;
            /// The liveliness of one or more writers, that were writing instances
            /// read through the readers has changed. Some writers have become
            /// "alive" or "not alive". Also see the [`crate::status::LivelinessChanged`] metadata struct.
            const LivelinessChanged =
                1 << cyclonedds_sys::dds_status_id_DDS_LIVELINESS_CHANGED_STATUS_ID;
            /// The writer has found a reader that matches the topic and has a
            /// compatible [`QoS`](crate::QoS). Also see the [`crate::status::PublicationMatched`] metadata struct.
            const PublicationMatched =
                1 << cyclonedds_sys::dds_status_id_DDS_PUBLICATION_MATCHED_STATUS_ID;
            /// The reader has found a writer that matches the topic and has a
            /// compatible [`QoS`](crate::QoS). Also see the [`crate::status::SubscriptionMatched`] metadata struct.
            const SubscriptionMatched =
                1 << cyclonedds_sys::dds_status_id_DDS_SUBSCRIPTION_MATCHED_STATUS_ID;
        }
    }
}

/// Identifies a DDS [`QoS`](crate::QoS) policy.
///
/// Used in incompatible [`QoS`](crate::QoS) status events to report which
/// policy caused the mismatch between a reader and writer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QoSPolicyId {
    /// No valid policy.
    Invalid,
    /// Attaches application-specific data to an entity. See
    /// [`UserData`](crate::qos::policy::UserData).
    UserData,
    /// Controls whether data is stored for late-joining readers. See
    /// [`Durability`](crate::qos::policy::Durability).
    Durability,
    /// Controls the scope and order of data presentation to subscribers. See
    /// [`Presentation`](crate::qos::policy::Presentation).
    Presentation,
    /// The maximum time between successive writes for a given instance. See
    /// [`Deadline`](crate::qos::policy::Deadline).
    Deadline,
    /// The acceptable delay between writing and receiving a sample. See
    /// [`LatencyBudget`](crate::qos::policy::LatencyBudget).
    LatencyBudget,
    /// Controls whether ownership of an instance is shared or exclusive. See
    /// [`Ownership`](crate::qos::policy::Ownership).
    Ownership,
    /// The strength of an exclusive ownership claim.
    OwnershipStrength,
    /// How the system determines whether a writer is still alive. See
    /// [`Liveliness`](crate::qos::policy::Liveliness).
    Liveliness,
    /// Filters samples based on the minimum time between delivery to a reader.
    /// See [`TimeBasedFilter`](crate::qos::policy::TimeBasedFilter).
    TimeBasedFilter,
    /// Restricts communication to a named logical channel within a domain. See
    /// [`Partition`](crate::qos::policy::Partition).
    Partition,
    /// Delivery guarantee: best-effort or reliable. See
    /// [`Reliability`](crate::qos::policy::Reliability).
    Reliability,
    /// The order in which samples are delivered to a reader. See
    /// [`DestinationOrder`](crate::qos::policy::DestinationOrder).
    DestinationOrder,
    /// How many samples are stored per instance. See
    /// [`History`](crate::qos::policy::History).
    History,
    /// Caps on the number of instances, samples, and samples-per-instance. See
    /// [`ResourceLimits`](crate::qos::policy::ResourceLimits).
    ResourceLimits,
    /// Controls whether child entities are automatically enabled on creation.
    /// See [`EntityFactory`](crate::qos::policy::EntityFactory).
    EntityFactory,
    /// Controls how a writer handles unregistered instances on deletion. See
    /// [`WriterDataLifecycle`](crate::qos::policy::WriterDataLifecycle).
    WriterDataLifecycle,
    /// Controls how a reader handles instances when matched writers disappear.
    /// See [`ReaderDataLifecycle`](crate::qos::policy::ReaderDataLifecycle).
    ReaderDataLifecycle,
    /// Attaches application-specific data to a topic. See
    /// [`TopicData`](crate::qos::policy::TopicData).
    TopicData,
    /// Attaches application-specific data to a publisher or subscriber. See
    /// [`GroupData`](crate::qos::policy::GroupData).
    GroupData,
    /// A hint to the transport layer about relative priority. See
    /// [`TransportPriority`](crate::qos::policy::TransportPriority).
    TransportPriority,
    /// The maximum duration a sample remains valid. See
    /// [`Lifespan`](crate::qos::policy::Lifespan).
    Lifespan,
    /// Configures the durability service's history and resource limits. See
    /// [`DurabilityService`](crate::qos::policy::DurabilityService).
    DurabilityService,
    /// Attaches key-value properties to an entity.
    Property,
    /// Controls type compatibility checking between readers and writers.
    TypeConsistencyEnforcement,
    /// The data representation format used for serialization.
    DataRepresentation,
}

impl From<u32> for QoSPolicyId {
    fn from(value: u32) -> Self {
        // dds_qos_policy_id_t is an enum whose values are signed under Windows
        // and unsigned otherwise (probably?). This is due to the default value
        // being used to represented an enum in `clang` and is propagated by
        // `bindgen`. We want to stabilize these enums to unsigned but need to
        // cast back to the whatever type is actually being exported based on
        // the platform.
        #[allow(clippy::cast_possible_wrap)]
        match value as cyclonedds_sys::dds_qos_policy_id_t {
            cyclonedds_sys::dds_qos_policy_id_DDS_INVALID_QOS_POLICY_ID => Self::Invalid,
            cyclonedds_sys::dds_qos_policy_id_DDS_USERDATA_QOS_POLICY_ID => Self::UserData,
            cyclonedds_sys::dds_qos_policy_id_DDS_DURABILITY_QOS_POLICY_ID => Self::Durability,
            cyclonedds_sys::dds_qos_policy_id_DDS_PRESENTATION_QOS_POLICY_ID => Self::Presentation,
            cyclonedds_sys::dds_qos_policy_id_DDS_DEADLINE_QOS_POLICY_ID => Self::Deadline,
            cyclonedds_sys::dds_qos_policy_id_DDS_LATENCYBUDGET_QOS_POLICY_ID => {
                Self::LatencyBudget
            }
            cyclonedds_sys::dds_qos_policy_id_DDS_OWNERSHIP_QOS_POLICY_ID => Self::Ownership,
            cyclonedds_sys::dds_qos_policy_id_DDS_OWNERSHIPSTRENGTH_QOS_POLICY_ID => {
                Self::OwnershipStrength
            }
            cyclonedds_sys::dds_qos_policy_id_DDS_LIVELINESS_QOS_POLICY_ID => Self::Liveliness,
            cyclonedds_sys::dds_qos_policy_id_DDS_TIMEBASEDFILTER_QOS_POLICY_ID => {
                Self::TimeBasedFilter
            }
            cyclonedds_sys::dds_qos_policy_id_DDS_PARTITION_QOS_POLICY_ID => Self::Partition,
            cyclonedds_sys::dds_qos_policy_id_DDS_RELIABILITY_QOS_POLICY_ID => Self::Reliability,
            cyclonedds_sys::dds_qos_policy_id_DDS_DESTINATIONORDER_QOS_POLICY_ID => {
                Self::DestinationOrder
            }
            cyclonedds_sys::dds_qos_policy_id_DDS_HISTORY_QOS_POLICY_ID => Self::History,
            cyclonedds_sys::dds_qos_policy_id_DDS_RESOURCELIMITS_QOS_POLICY_ID => {
                Self::ResourceLimits
            }
            cyclonedds_sys::dds_qos_policy_id_DDS_ENTITYFACTORY_QOS_POLICY_ID => {
                Self::EntityFactory
            }
            cyclonedds_sys::dds_qos_policy_id_DDS_WRITERDATALIFECYCLE_QOS_POLICY_ID => {
                Self::WriterDataLifecycle
            }
            cyclonedds_sys::dds_qos_policy_id_DDS_READERDATALIFECYCLE_QOS_POLICY_ID => {
                Self::ReaderDataLifecycle
            }
            cyclonedds_sys::dds_qos_policy_id_DDS_TOPICDATA_QOS_POLICY_ID => Self::TopicData,
            cyclonedds_sys::dds_qos_policy_id_DDS_GROUPDATA_QOS_POLICY_ID => Self::GroupData,
            cyclonedds_sys::dds_qos_policy_id_DDS_TRANSPORTPRIORITY_QOS_POLICY_ID => {
                Self::TransportPriority
            }
            cyclonedds_sys::dds_qos_policy_id_DDS_LIFESPAN_QOS_POLICY_ID => Self::Lifespan,
            cyclonedds_sys::dds_qos_policy_id_DDS_DURABILITYSERVICE_QOS_POLICY_ID => {
                Self::DurabilityService
            }
            cyclonedds_sys::dds_qos_policy_id_DDS_PROPERTY_QOS_POLICY_ID => Self::Property,
            cyclonedds_sys::dds_qos_policy_id_DDS_TYPE_CONSISTENCY_ENFORCEMENT_QOS_POLICY_ID => {
                Self::TypeConsistencyEnforcement
            }
            cyclonedds_sys::dds_qos_policy_id_DDS_DATA_REPRESENTATION_QOS_POLICY_ID => {
                Self::DataRepresentation
            }
            value => unreachable!(
                "unsupported value: {value} in conversion to {}",
                std::any::type_name::<Self>()
            ),
        }
    }
}

/// A cumulative status event counter with a per-notification delta.
///
/// Appears in status types to report both the running total of an event and
/// how many times it occurred since the last time the status was read or taken.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Counter {
    /// Total number of times the event has occurred.
    pub count: u32,
    /// Change in `count` since the status was last read or taken.
    pub delta: i32,
}

/// Delivered to the
/// [`with_inconsistent_topic`](crate::listener::TopicListener::with_inconsistent_topic)
/// callback when a remote topic is discovered with the same name but an
/// incompatible type or [`QoS`](crate::QoS).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InconsistentTopic {
    /// Running count of inconsistent topic discoveries.
    pub total: Counter,
}

/// Delivered to the
/// [`with_liveliness_lost`](crate::listener::WriterListener::with_liveliness_lost)
/// callback when the writer fails to meet its
/// [`Liveliness`](crate::qos::policy::Liveliness) policy and is considered
/// inactive by matched readers.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LivelinessLost {
    /// Running count of liveliness violations.
    pub total: Counter,
}

/// Delivered to the
/// [`with_offered_deadline_missed`](crate::listener::WriterListener::with_offered_deadline_missed)
/// callback when the writer fails to write a new sample within its offered
/// [`Deadline`](crate::qos::policy::Deadline) period for one or more
/// instances.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OfferedDeadlineMissed {
    /// Running count of deadline violations.
    pub total: Counter,
    /// Instance handle of the last instance that missed its deadline.
    pub last_instance_handle: crate::entity::InstanceHandle,
}

/// Delivered to the
/// [`with_offered_incompatible_qos`](crate::listener::WriterListener::with_offered_incompatible_qos)
/// callback when a reader is discovered whose requested [`QoS`](crate::QoS) is
/// incompatible with this writer's offered [`QoS`](crate::QoS).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OfferedIncompatibleQoS {
    /// Running count of incompatible [`QoS`](crate::QoS) discoveries.
    pub total: Counter,
    /// The policy that caused the most recent incompatibility.
    pub last_policy_id: QoSPolicyId,
}

/// Delivered to the
/// [`with_publication_matched`](crate::listener::WriterListener::with_publication_matched)
/// callback when a reader matching this writer's topic and [`QoS`](crate::QoS)
/// is discovered or lost.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PublicationMatched {
    /// Running count of reader matches over the lifetime of the writer.
    pub total: Counter,
    /// Current number of matched readers.
    pub current: Counter,
    /// Instance handle of the last reader that matched or unmatched.
    pub last_subscription_handle: crate::entity::InstanceHandle,
}

/// Delivered to the
/// [`with_sample_lost`](crate::listener::ReaderListener::with_sample_lost)
/// callback when a sample is lost before being received by the reader.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SampleLost {
    /// Running count of lost samples.
    pub total: Counter,
}

/// Delivered to the
/// [`with_sample_rejected`](crate::listener::ReaderListener::with_sample_rejected)
/// callback when an incoming sample is rejected due to [`resource
/// limits`](crate::qos::policy::ResourceLimits).
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SampleRejectedReason {
    // TODO How can sample not rejected be a valid status for the sample
    // rejected status? should this be modeled as an Option in
    // samplerejected?
    /// The sample was not rejected.
    NotRejected,
    /// Rejected because the maximum number of instances has been reached.
    RejectedByInstancesLimit,
    /// Rejected because the maximum number of samples has been reached.
    RejectedBySamplesLimit,
    /// Rejected because the maximum number of samples per instance has been
    /// reached.
    RejectedBySamplesPerInstanceLimit,
}

impl From<cyclonedds_sys::dds_sample_rejected_status_kind> for SampleRejectedReason {
    fn from(reason: cyclonedds_sys::dds_sample_rejected_status_kind) -> Self {
        match reason {
            cyclonedds_sys::dds_sample_rejected_status_kind_DDS_NOT_REJECTED => Self::NotRejected,
            cyclonedds_sys::dds_sample_rejected_status_kind_DDS_REJECTED_BY_INSTANCES_LIMIT => Self::RejectedByInstancesLimit,
            cyclonedds_sys::dds_sample_rejected_status_kind_DDS_REJECTED_BY_SAMPLES_LIMIT => Self::RejectedBySamplesLimit,
            cyclonedds_sys::dds_sample_rejected_status_kind_DDS_REJECTED_BY_SAMPLES_PER_INSTANCE_LIMIT => Self::RejectedBySamplesPerInstanceLimit,
            value => unreachable!("unsupported value: {value} in conversion to {}", std::any::type_name::<Self>())
        }
    }
}

/// Delivered to the
/// [`with_sample_rejected`](crate::listener::ReaderListener::with_sample_rejected)
/// callback.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SampleRejected {
    /// Running count of rejected samples.
    pub total: Counter,
    /// The reason the most recent sample was rejected.
    pub last_reason: SampleRejectedReason,
    /// Instance handle of the instance whose sample was most recently rejected.
    pub last_instance_handle: crate::entity::InstanceHandle,
}

/// Delivered to the
/// [`with_requested_deadline_missed`](crate::listener::ReaderListener::with_requested_deadline_missed)
/// callback when a sample is not received within the
/// [`Deadline`](crate::qos::policy::Deadline) period offered by a matched
/// writer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RequestedDeadlineMissed {
    /// Running count of deadline misses across all instances.
    pub total: Counter,
    /// Instance handle of the last instance that missed its deadline.
    pub last_instance_handle: crate::entity::InstanceHandle,
}

/// Delivered to the
/// [`with_requested_incompatible_qos`](crate::listener::ReaderListener::with_requested_incompatible_qos)
/// callback when a writer is discovered whose offered [`QoS`](crate::QoS) is
/// incompatible with this reader's requested [`QoS`](crate::QoS).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RequestedIncompatibleQoS {
    /// Running count of incompatible [`QoS`](crate::QoS) discoveries.
    pub total: Counter,
    /// The policy that caused the most recent incompatibility.
    pub last_policy_id: QoSPolicyId,
}

/// Delivered to the
/// [`with_subscription_matched`](crate::listener::ReaderListener::with_subscription_matched)
/// callback when a writer matching this reader's topic and [`QoS`](crate::QoS)
/// is discovered.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SubscriptionMatched {
    /// Running count of writer matches over the lifetime of the reader.
    pub total: Counter,
    /// Current number of matched writers.
    pub current: Counter,
    /// Instance handle of the last writer that matched or unmatched.
    pub last_publication_handle: crate::entity::InstanceHandle,
}

/// Delivered to the
/// [`with_liveliness_changed`](crate::listener::ReaderListener::with_liveliness_changed)
/// callback when a matched writer transitions between active and inactive.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LivelinessChanged {
    /// Running count of matched writers that are currently active.
    pub alive: Counter,
    /// Running count of matched writers that are currently inactive.
    pub not_alive: Counter,
    /// Instance handle of the last writer whose liveliness changed.
    pub last_publication_handle: crate::entity::InstanceHandle,
}

impl From<cyclonedds_sys::dds_inconsistent_topic_status> for InconsistentTopic {
    fn from(status: cyclonedds_sys::dds_inconsistent_topic_status) -> Self {
        let total = Counter {
            count: status.total_count,
            delta: status.total_count_change,
        };

        Self { total }
    }
}

impl From<cyclonedds_sys::dds_liveliness_lost_status_t> for LivelinessLost {
    fn from(status: cyclonedds_sys::dds_liveliness_lost_status_t) -> Self {
        let total = Counter {
            count: status.total_count,
            delta: status.total_count_change,
        };
        Self { total }
    }
}

impl From<cyclonedds_sys::dds_offered_deadline_missed_status_t> for OfferedDeadlineMissed {
    fn from(status: cyclonedds_sys::dds_offered_deadline_missed_status_t) -> Self {
        let total = Counter {
            count: status.total_count,
            delta: status.total_count_change,
        };
        let last_instance_handle = crate::entity::InstanceHandle {
            inner: status.last_instance_handle,
        };

        Self {
            total,
            last_instance_handle,
        }
    }
}

impl From<cyclonedds_sys::dds_offered_incompatible_qos_status_t> for OfferedIncompatibleQoS {
    fn from(status: cyclonedds_sys::dds_offered_incompatible_qos_status_t) -> Self {
        let total = Counter {
            count: status.total_count,
            delta: status.total_count_change,
        };
        let last_policy_id = status.last_policy_id.into();

        Self {
            total,
            last_policy_id,
        }
    }
}

impl From<cyclonedds_sys::dds_publication_matched_status_t> for PublicationMatched {
    fn from(status: cyclonedds_sys::dds_publication_matched_status_t) -> Self {
        let total = Counter {
            count: status.total_count,
            delta: status.total_count_change,
        };
        let current = Counter {
            count: status.current_count,
            delta: status.current_count_change,
        };
        let last_subscription_handle = crate::entity::InstanceHandle {
            inner: status.last_subscription_handle,
        };
        Self {
            total,
            current,
            last_subscription_handle,
        }
    }
}

impl From<cyclonedds_sys::dds_sample_lost_status_t> for SampleLost {
    fn from(status: cyclonedds_sys::dds_sample_lost_status_t) -> Self {
        let total = Counter {
            count: status.total_count,
            delta: status.total_count_change,
        };
        Self { total }
    }
}

impl From<cyclonedds_sys::dds_sample_rejected_status_t> for SampleRejected {
    fn from(status: cyclonedds_sys::dds_sample_rejected_status_t) -> Self {
        let total = Counter {
            count: status.total_count,
            delta: status.total_count_change,
        };
        let last_reason = status.last_reason.into();
        let last_instance_handle = crate::entity::InstanceHandle {
            inner: status.last_instance_handle,
        };
        Self {
            total,
            last_reason,
            last_instance_handle,
        }
    }
}

impl From<cyclonedds_sys::dds_liveliness_changed_status_t> for LivelinessChanged {
    fn from(status: cyclonedds_sys::dds_liveliness_changed_status_t) -> Self {
        let alive = Counter {
            count: status.alive_count,
            delta: status.alive_count_change,
        };
        let not_alive = Counter {
            count: status.not_alive_count,
            delta: status.not_alive_count_change,
        };

        let last_publication_handle = crate::entity::InstanceHandle {
            inner: status.last_publication_handle,
        };
        Self {
            alive,
            not_alive,
            last_publication_handle,
        }
    }
}

impl From<cyclonedds_sys::dds_requested_deadline_missed_status_t> for RequestedDeadlineMissed {
    fn from(status: cyclonedds_sys::dds_requested_deadline_missed_status_t) -> Self {
        let total = Counter {
            count: status.total_count,
            delta: status.total_count_change,
        };
        let last_instance_handle = crate::entity::InstanceHandle {
            inner: status.last_instance_handle,
        };
        Self {
            total,
            last_instance_handle,
        }
    }
}

impl From<cyclonedds_sys::dds_requested_incompatible_qos_status_t> for RequestedIncompatibleQoS {
    fn from(status: cyclonedds_sys::dds_requested_incompatible_qos_status_t) -> Self {
        let total = Counter {
            count: status.total_count,
            delta: status.total_count_change,
        };
        let last_policy_id = status.last_policy_id.into();
        Self {
            total,
            last_policy_id,
        }
    }
}

impl From<cyclonedds_sys::dds_subscription_matched_status_t> for SubscriptionMatched {
    fn from(status: cyclonedds_sys::dds_subscription_matched_status_t) -> Self {
        let total = Counter {
            count: status.total_count,
            delta: status.total_count_change,
        };
        let current = Counter {
            count: status.current_count,
            delta: status.current_count_change,
        };
        let last_publication_handle = crate::entity::InstanceHandle {
            inner: status.last_publication_handle,
        };

        Self {
            total,
            current,
            last_publication_handle,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_qos_policy_id_conversion() {
        let result =
            QoSPolicyId::from(cyclonedds_sys::dds_qos_policy_id_DDS_INVALID_QOS_POLICY_ID as u32);
        assert_eq!(result, QoSPolicyId::Invalid);
        let result =
            QoSPolicyId::from(cyclonedds_sys::dds_qos_policy_id_DDS_USERDATA_QOS_POLICY_ID as u32);
        assert_eq!(result, QoSPolicyId::UserData);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_DURABILITY_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::Durability);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_PRESENTATION_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::Presentation);
        let result =
            QoSPolicyId::from(cyclonedds_sys::dds_qos_policy_id_DDS_DEADLINE_QOS_POLICY_ID as u32);
        assert_eq!(result, QoSPolicyId::Deadline);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_LATENCYBUDGET_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::LatencyBudget);
        let result =
            QoSPolicyId::from(cyclonedds_sys::dds_qos_policy_id_DDS_OWNERSHIP_QOS_POLICY_ID as u32);
        assert_eq!(result, QoSPolicyId::Ownership);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_OWNERSHIPSTRENGTH_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::OwnershipStrength);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_LIVELINESS_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::Liveliness);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_TIMEBASEDFILTER_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::TimeBasedFilter);
        let result =
            QoSPolicyId::from(cyclonedds_sys::dds_qos_policy_id_DDS_PARTITION_QOS_POLICY_ID as u32);
        assert_eq!(result, QoSPolicyId::Partition);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_RELIABILITY_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::Reliability);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_DESTINATIONORDER_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::DestinationOrder);
        let result =
            QoSPolicyId::from(cyclonedds_sys::dds_qos_policy_id_DDS_HISTORY_QOS_POLICY_ID as u32);
        assert_eq!(result, QoSPolicyId::History);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_RESOURCELIMITS_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::ResourceLimits);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_ENTITYFACTORY_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::EntityFactory);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_WRITERDATALIFECYCLE_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::WriterDataLifecycle);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_READERDATALIFECYCLE_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::ReaderDataLifecycle);
        let result =
            QoSPolicyId::from(cyclonedds_sys::dds_qos_policy_id_DDS_TOPICDATA_QOS_POLICY_ID as u32);
        assert_eq!(result, QoSPolicyId::TopicData);
        let result =
            QoSPolicyId::from(cyclonedds_sys::dds_qos_policy_id_DDS_GROUPDATA_QOS_POLICY_ID as u32);
        assert_eq!(result, QoSPolicyId::GroupData);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_TRANSPORTPRIORITY_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::TransportPriority);
        let result =
            QoSPolicyId::from(cyclonedds_sys::dds_qos_policy_id_DDS_LIFESPAN_QOS_POLICY_ID as u32);
        assert_eq!(result, QoSPolicyId::Lifespan);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_DURABILITYSERVICE_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::DurabilityService);
        let result =
            QoSPolicyId::from(cyclonedds_sys::dds_qos_policy_id_DDS_PROPERTY_QOS_POLICY_ID as u32);
        assert_eq!(result, QoSPolicyId::Property);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_TYPE_CONSISTENCY_ENFORCEMENT_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::TypeConsistencyEnforcement);
        let result = QoSPolicyId::from(
            cyclonedds_sys::dds_qos_policy_id_DDS_DATA_REPRESENTATION_QOS_POLICY_ID as u32,
        );
        assert_eq!(result, QoSPolicyId::DataRepresentation);
    }

    #[test]
    #[should_panic = "internal error: entered unreachable code: unsupported value"]
    fn test_qos_policy_id_conversion_out_of_range() {
        let _ = QoSPolicyId::from(u32::MAX);
    }

    #[test]
    fn test_sample_rejected_reason_conversion() {
        let result = SampleRejectedReason::from(
            cyclonedds_sys::dds_sample_rejected_status_kind_DDS_NOT_REJECTED,
        );
        assert_eq!(result, SampleRejectedReason::NotRejected);
        let result = SampleRejectedReason::from(
            cyclonedds_sys::dds_sample_rejected_status_kind_DDS_REJECTED_BY_INSTANCES_LIMIT,
        );
        assert_eq!(result, SampleRejectedReason::RejectedByInstancesLimit);
        let result = SampleRejectedReason::from(
            cyclonedds_sys::dds_sample_rejected_status_kind_DDS_REJECTED_BY_SAMPLES_LIMIT,
        );
        assert_eq!(result, SampleRejectedReason::RejectedBySamplesLimit);
        let result = SampleRejectedReason::from(cyclonedds_sys::dds_sample_rejected_status_kind_DDS_REJECTED_BY_SAMPLES_PER_INSTANCE_LIMIT);
        assert_eq!(
            result,
            SampleRejectedReason::RejectedBySamplesPerInstanceLimit
        );
    }

    #[test]
    #[should_panic = "internal error: entered unreachable code: unsupported value"]
    fn test_sample_rejected_reason_conversion_out_of_range() {
        let _ = SampleRejectedReason::from(cyclonedds_sys::dds_sample_rejected_status_kind::MAX);
    }

    #[test]
    fn test_inconsistent_topic_conversion() {
        let total_count = 10;
        let total_count_change = 20;

        let status = InconsistentTopic::from(cyclonedds_sys::dds_inconsistent_topic_status {
            total_count,
            total_count_change,
        });

        assert_eq!(
            (status.total.count, status.total.delta),
            (total_count, total_count_change)
        );
    }

    #[test]
    fn test_liveliness_lost_conversion() {
        let total_count = 10;
        let total_count_change = 20;
        let status = LivelinessLost::from(cyclonedds_sys::dds_liveliness_lost_status {
            total_count,
            total_count_change,
        });

        assert_eq!(
            (status.total.count, status.total.delta),
            (total_count, total_count_change)
        );
    }

    #[test]
    fn test_offered_deadline_missed_conversion() {
        let total_count = 10;
        let total_count_change = 20;
        let last_instance_handle = 30;

        let status =
            OfferedDeadlineMissed::from(cyclonedds_sys::dds_offered_deadline_missed_status {
                total_count,
                total_count_change,
                last_instance_handle,
            });

        assert_eq!(
            (
                status.total.count,
                status.total.delta,
                status.last_instance_handle.inner
            ),
            (total_count, total_count_change, last_instance_handle)
        );
    }

    #[test]
    fn test_offered_incompatible_qos_conversion() {
        let total_count = 10;
        let total_count_change = 20;
        let last_policy_id = cyclonedds_sys::dds_qos_policy_id_DDS_DURABILITY_QOS_POLICY_ID as u32;

        let status =
            OfferedIncompatibleQoS::from(cyclonedds_sys::dds_offered_incompatible_qos_status {
                total_count,
                total_count_change,
                last_policy_id,
            });

        assert_eq!(
            (
                status.total.count,
                status.total.delta,
                status.last_policy_id
            ),
            (total_count, total_count_change, last_policy_id.into())
        );
    }

    #[test]
    fn test_publication_matched_conversion() {
        let total_count = 10;
        let total_count_change = 20;
        let current_count = 30;
        let current_count_change = 40;
        let last_subscription_handle = 50;

        let status = PublicationMatched::from(cyclonedds_sys::dds_publication_matched_status {
            total_count,
            total_count_change,
            current_count,
            current_count_change,
            last_subscription_handle,
        });

        assert_eq!(
            (
                status.total.count,
                status.total.delta,
                status.current.count,
                status.current.delta,
                status.last_subscription_handle.inner
            ),
            (
                total_count,
                total_count_change,
                current_count,
                current_count_change,
                last_subscription_handle
            )
        );
    }

    #[test]
    fn test_sample_lost_conversion() {
        let total_count = 10;
        let total_count_change = 20;

        let status = SampleLost::from(cyclonedds_sys::dds_sample_lost_status {
            total_count,
            total_count_change,
        });

        assert_eq!(
            (status.total.count, status.total.delta),
            (total_count, total_count_change)
        );
    }

    #[test]
    fn test_sample_rejected_conversion() {
        let total_count = 10;
        let total_count_change = 20;
        let last_reason =
            cyclonedds_sys::dds_sample_rejected_status_kind_DDS_REJECTED_BY_INSTANCES_LIMIT;
        let last_instance_handle = 40;

        let status = SampleRejected::from(cyclonedds_sys::dds_sample_rejected_status {
            total_count,
            total_count_change,
            last_reason,
            last_instance_handle,
        });

        assert_eq!(
            (
                status.total.count,
                status.total.delta,
                status.last_reason,
                status.last_instance_handle.inner
            ),
            (
                total_count,
                total_count_change,
                last_reason.into(),
                last_instance_handle
            )
        );
    }

    #[test]
    fn test_liveliness_changed_conversion() {
        let alive_count = 10;
        let alive_count_change = 20;
        let not_alive_count = 30;
        let not_alive_count_change = 40;
        let last_publication_handle = 50;

        let status = LivelinessChanged::from(cyclonedds_sys::dds_liveliness_changed_status {
            alive_count,
            alive_count_change,
            not_alive_count,
            not_alive_count_change,
            last_publication_handle,
        });

        assert_eq!(
            (
                status.alive.count,
                status.alive.delta,
                status.not_alive.count,
                status.not_alive.delta,
                status.last_publication_handle.inner
            ),
            (
                alive_count,
                alive_count_change,
                not_alive_count,
                not_alive_count_change,
                last_publication_handle
            )
        );
    }

    #[test]
    fn test_requested_deadline_missed_conversion() {
        let total_count = 10;
        let total_count_change = 20;
        let last_instance_handle = 30;

        let status =
            RequestedDeadlineMissed::from(cyclonedds_sys::dds_requested_deadline_missed_status {
                total_count,
                total_count_change,
                last_instance_handle,
            });

        assert_eq!(
            (
                status.total.count,
                status.total.delta,
                status.last_instance_handle.inner
            ),
            (total_count, total_count_change, last_instance_handle)
        );
    }

    #[test]
    fn test_requested_incompatible_qos_conversion() {
        let total_count = 10;
        let total_count_change = 20;
        let last_policy_id = cyclonedds_sys::dds_qos_policy_id_DDS_DURABILITY_QOS_POLICY_ID as u32;

        let status =
            RequestedIncompatibleQoS::from(cyclonedds_sys::dds_requested_incompatible_qos_status {
                total_count,
                total_count_change,
                last_policy_id,
            });

        assert_eq!(
            (
                status.total.count,
                status.total.delta,
                status.last_policy_id
            ),
            (total_count, total_count_change, last_policy_id.into())
        );
    }

    #[test]
    fn test_subscription_matched_conversion() {
        let total_count = 10;
        let total_count_change = 20;
        let current_count = 30;
        let current_count_change = 40;
        let last_publication_handle = 50;

        let status = SubscriptionMatched::from(cyclonedds_sys::dds_subscription_matched_status {
            total_count,
            total_count_change,
            current_count,
            current_count_change,
            last_publication_handle,
        });

        assert_eq!(
            (
                status.total.count,
                status.total.delta,
                status.current.count,
                status.current.delta,
                status.last_publication_handle.inner
            ),
            (
                total_count,
                total_count_change,
                current_count,
                current_count_change,
                last_publication_handle
            )
        );
    }
}