eclipse-cyclonedds 0.0.2

Rust binding for Eclipse Cyclone DDS
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
//! The base of the DDS entity hierarchy.
//!
//! Most DDS objects ([`Participant`](crate::Participant),
//! [`Topic`](crate::Topic), [`Reader`](crate::Reader),
//! [`Writer`](crate::Writer), and others) are entities. See the
//! [implementors of `Entity`](Entity#implementors) for the full list. This
//! module provides the [`Entity`] trait with the common methods available to
//! all entities.

use crate::internal::ffi;
use crate::{Result, Status};

/// A unique opaque handle identifying an instance.
///
/// For keyed topics this corresponds to a specific key value, but applications
/// should treat it as an opaque DDS handle.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct InstanceHandle {
    pub(crate) inner: cyclonedds_sys::dds_instance_handle_t,
}

/// A raw entity ID for an entity.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct EntityId {
    pub(crate) inner: cyclonedds_sys::dds_entity_t,
}

// TODO should the Entity trait be sealed?
/// Common interface implemented by all members of the DDS entity hierarchy.
///
/// - [`Participant`](crate::Participant): the root entity representing
///   membership in a domain.
///   - [`WaitSet`](crate::WaitSet): blocks until one or more attached
///     conditions are triggered.
///   - [`GuardCondition`](crate::GuardCondition): a manually triggered
///     condition for use with a [`WaitSet`](crate::WaitSet).
///   - [`Topic<T>`](crate::Topic): names and types a data channel for a
///     specific payload type `T`.
///   - [`Publisher`](crate::Publisher): groups [`Writers`](crate::Writer) and
///     controls their shared [`QoS`](crate::QoS).
///     - [`Writer<T>`](crate::Writer): publishes samples of type `T` to a
///       [`Topic`](crate::Topic).
///   - [`Subscriber`](crate::Subscriber): groups [`Readers`](crate::Reader) and
///     controls their shared [`QoS`](crate::QoS).
///     - [`Reader<T>`](crate::Reader): receives samples of type `T` from a
///       [`Topic`](crate::Topic).
///       - [`ReadCondition<T>`](crate::ReadCondition): filters
///         [`Reader`](crate::Reader) samples by
///         [`sample`](crate::state::sample), [`view`](crate::state::view), and
///         [`instance`](crate::state::instance) state.
///       - [`QueryCondition<T, F>`](crate::QueryCondition): filters
///         [`Reader`](crate::Reader) samples by [`sample state`](crate::State)
///         and a predicate.
pub trait Entity {
    /// Returns the [`EntityId`] of this entity.
    ///
    /// # Examples
    ///
    /// ```
    /// use cyclonedds::entity::Entity;
    /// use cyclonedds::{Reader, Topic, Writer};
    ///
    /// # #[derive(
    /// #     cyclonedds::Topicable, serde::Serialize, serde::Deserialize, Clone, Debug, Default,
    /// # )]
    /// # struct Data {
    /// #     x: i32,
    /// # }
    /// # let domain = cyclonedds::Domain::default();
    /// # let participant = cyclonedds::Participant::new(&domain)?;
    /// let topic = Topic::<Data>::new(&participant, "Example")?;
    /// let reader = Reader::new(&topic)?;
    /// let writer = Writer::new(&topic)?;
    ///
    /// // The reader and the writer have distinct IDs.
    /// assert_ne!(reader.id(), writer.id());
    ///
    /// # Ok::<_, cyclonedds::Error>(())
    /// ```
    fn id(&self) -> EntityId;

    /// Returns the [`InstanceHandle`] of this entity.
    ///
    /// # Errors
    ///
    /// Returns an [`Error`](crate::Error) specifying the reason if the instance
    /// handle fails to be retrieved.
    ///
    /// # Examples
    ///
    /// ```
    /// use cyclonedds::entity::Entity;
    /// use cyclonedds::{Reader, Topic, Writer};
    ///
    /// # #[derive(
    /// #     cyclonedds::Topicable, serde::Serialize, serde::Deserialize, Clone, Debug, Default,
    /// # )]
    /// # struct Data {
    /// #     x: i32,
    /// # }
    /// # let domain = cyclonedds::Domain::default();
    /// # let participant = cyclonedds::Participant::new(&domain)?;
    /// let topic = Topic::<Data>::new(&participant, "Example")?;
    /// let reader = Reader::new(&topic)?;
    /// let writer = Writer::new(&topic)?;
    ///
    /// // The reader and the writer have distinct instance handles.
    /// assert_ne!(reader.instance_handle()?, writer.instance_handle()?);
    ///
    /// // Instance handles can be used to identify entities across various API
    /// // calls. For example, the writer's handle appears in the set of matched
    /// // publications.
    /// let matched = reader.matched_publications()?;
    /// assert_eq!(matched[0], writer.instance_handle()?);
    /// # Ok::<_, cyclonedds::Error>(())
    /// ```
    fn instance_handle(&self) -> Result<InstanceHandle> {
        let entity = self.id();
        let inner = ffi::dds_get_instance_handle(entity.inner)?;
        Ok(InstanceHandle { inner })
    }

    /// Returns the set of status flags that have changed since they were last
    /// [`read`](crate::Reader::read) or [`taken`](crate::Reader::take).
    ///
    /// # Errors
    ///
    /// - Returns an [`Error`](crate::Error) if the status bits of the
    ///   corresponding entity could not be retrieved (e.g. the entity no longer
    ///   exists).
    ///
    /// - Returns [`BadParameter`](crate::Error::BadParameter) if the retrieved
    ///   bits do not correspond to a valid [`Status`].
    ///
    /// # Examples
    ///
    /// ```
    /// use cyclonedds::entity::Entity;
    /// use cyclonedds::{Reader, Status, Topic, Writer};
    ///
    /// # #[derive(
    /// #     cyclonedds::Topicable, serde::Serialize, serde::Deserialize, Clone, Debug, Default,
    /// # )]
    /// # struct Data {
    /// #     x: i32,
    /// # }
    /// # let domain = cyclonedds::Domain::default();
    /// # let participant = cyclonedds::Participant::new(&domain)?;
    /// let topic = Topic::<Data>::new(&participant, "Example")?;
    /// let reader = Reader::new(&topic)?;
    ///
    /// // The reader has been created but nothing in particular has happened in
    /// // terms of status changes.
    /// let changed = reader.status_changes()?;
    /// assert_eq!(changed, Status::empty());
    ///
    /// // The writer that is created will match with the reader.
    /// let writer = Writer::new(&topic)?;
    ///
    /// // After a writer matches, the reader reports a status change.
    /// let changed = reader.status_changes()?;
    /// assert!(changed.contains(Status::SubscriptionMatched));
    /// # Ok::<_, cyclonedds::Error>(())
    /// ```
    fn status_changes(&self) -> Result<Status> {
        let entity = self.id();
        let status = ffi::dds_get_status_changes(entity.inner)?;
        Status::from_bits(status).ok_or(crate::error::Error::BadParameter)
    }

    /// Takes and clears the status flags matching `mask`, or all flags if
    /// `mask` is `None`.
    ///
    /// Unlike [`read_status`](Entity::read_status), this clears the returned
    /// flags on the entity.
    ///
    /// # Errors
    ///
    /// - Returns an [`Error`](crate::Error) if the status bits of the
    ///   corresponding entity could not be retrieved (e.g. the entity no longer
    ///   exists or the status mask contains entries that do not apply to the
    ///   entity type).
    ///
    /// - Returns [`BadParameter`](crate::Error::BadParameter) if the retrieved
    ///   bits do not correspond to a valid [`Status`].
    ///
    /// # Examples
    ///
    /// ```
    /// use cyclonedds::entity::Entity;
    /// use cyclonedds::{Reader, Status, Topic, Writer};
    ///
    /// # #[derive(
    /// #     cyclonedds::Topicable, serde::Serialize, serde::Deserialize, Clone, Debug, Default,
    /// # )]
    /// # struct Data {
    /// #     x: i32,
    /// # }
    /// # let domain = cyclonedds::Domain::default();
    /// # let participant = cyclonedds::Participant::new(&domain)?;
    /// let topic = Topic::<Data>::new(&participant, "Example")?;
    /// let reader = Reader::new(&topic)?;
    /// let writer = Writer::new(&topic)?;
    ///
    /// // The reader has matched with the writer, so its status should have
    /// // updated.
    /// let status = reader.take_status(Some(Status::SubscriptionMatched))?;
    /// assert!(status.contains(Status::SubscriptionMatched));
    ///
    /// // The flag has been cleared; a second take returns empty.
    /// let cleared = reader.take_status(Some(Status::SubscriptionMatched))?;
    /// assert!(cleared.is_empty());
    /// # Ok::<_, cyclonedds::Error>(())
    /// ```
    fn take_status(&self, mask: Option<Status>) -> Result<Status> {
        let entity = self.id();
        let mask = mask.unwrap_or(Status::all()).bits();
        let status = ffi::dds_take_status(entity.inner, mask)?;
        Status::from_bits(status).ok_or(crate::error::Error::BadParameter)
    }

    /// Reads the status flags matching `mask` without clearing them, or all
    /// flags if `mask` is `None`.
    ///
    /// # Errors
    ///
    /// - Returns an [`Error`](crate::Error) if the status bits of the
    ///   corresponding entity could not be retrieved (e.g. the entity no longer
    ///   exists).
    ///
    /// - Returns [`BadParameter`](crate::Error::BadParameter) if the retrieved
    ///   bits do not correspond to a valid [`Status`].
    ///
    /// # Examples
    ///
    /// ```
    /// use cyclonedds::entity::Entity;
    /// use cyclonedds::{Reader, Status, Topic, Writer};
    ///
    /// # #[derive(
    /// #     cyclonedds::Topicable, serde::Serialize, serde::Deserialize, Clone, Debug, Default,
    /// # )]
    /// # struct Data {
    /// #     x: i32,
    /// # }
    /// # let domain = cyclonedds::Domain::default();
    /// # let participant = cyclonedds::Participant::new(&domain)?;
    /// let topic = Topic::<Data>::new(&participant, "Example")?;
    /// let reader = Reader::new(&topic)?;
    /// let writer = Writer::new(&topic)?;
    ///
    /// // The reader has matched with the writer, so its status should have
    /// // updated.
    /// let status = reader.read_status(Some(Status::SubscriptionMatched))?;
    /// assert!(status.contains(Status::SubscriptionMatched));
    ///
    /// // The flag is preserved; a second read returns the same value.
    /// let same = reader.read_status(Some(Status::SubscriptionMatched))?;
    /// assert_eq!(status, same);
    /// # Ok::<_, cyclonedds::Error>(())
    /// ```
    fn read_status(&self, mask: Option<Status>) -> Result<Status> {
        let entity = self.id();
        let mask = mask.unwrap_or(Status::all()).bits();
        let status = ffi::dds_read_status(entity.inner, mask)?;
        Status::from_bits(status).ok_or(crate::error::Error::BadParameter)
    }

    /// Returns the status mask enabled on the entity.
    ///
    /// # Errors
    ///
    /// - Returns an [`Error`](crate::Error) if the status mask of the
    ///   corresponding entity could not be retrieved (e.g. the entity no longer
    ///   exists).
    ///
    /// - Returns [`BadParameter`](crate::Error::BadParameter) if the retrieved
    ///   bits do not correspond to a valid [`Status`].
    ///
    /// # Examples
    /// ```
    /// use cyclonedds::entity::Entity;
    /// use cyclonedds::{Status, Topic, Writer};
    ///
    /// # #[derive(
    /// #     cyclonedds::Topicable, serde::Serialize, serde::Deserialize, Clone, Debug, Default,
    /// # )]
    /// # struct Data {
    /// #     x: i32,
    /// # }
    /// # let domain = cyclonedds::Domain::default();
    /// # let participant = cyclonedds::Participant::new(&domain)?;
    /// let topic = Topic::<Data>::new(&participant, "Example")?;
    /// let writer = Writer::new(&topic)?;
    ///
    /// // Get the initial active status mask.
    /// assert_eq!(
    ///     writer.status_mask()?,
    ///     Status::OfferedDeadlineMissed
    ///         | Status::OfferedIncompatibleQoS
    ///         | Status::LivelinessLost
    ///         | Status::PublicationMatched
    /// );
    /// # Ok::<_, cyclonedds::Error>(())
    /// ```
    fn status_mask(&self) -> Result<Status> {
        let entity = self.id();
        let mask = ffi::dds_get_status_mask(entity.inner)?;
        Status::from_bits(mask).ok_or(crate::error::Error::BadParameter)
    }

    /// Sets and enables a status mask on the entity.
    ///
    /// Only status flags included in `mask` will trigger listener callbacks or
    /// be reported via [`status_changes`](Entity::status_changes).
    ///
    /// # Errors
    ///
    /// - Returns an [`Error`](crate::Error) if the status mask of the
    ///   corresponding entity could not be set (e.g. the entity no longer
    ///   exists).
    ///
    /// # Examples
    /// ```
    /// use cyclonedds::entity::Entity;
    /// use cyclonedds::{Status, Topic, Writer};
    ///
    /// # #[derive(
    /// #     cyclonedds::Topicable, serde::Serialize, serde::Deserialize, Clone, Debug, Default,
    /// # )]
    /// # struct Data {
    /// #     x: i32,
    /// # }
    /// # let domain = cyclonedds::Domain::default();
    /// # let participant = cyclonedds::Participant::new(&domain)?;
    /// let topic = Topic::<Data>::new(&participant, "Example")?;
    /// let writer = Writer::new(&topic)?;
    ///
    /// // Set the active status mask.
    /// writer.set_status_mask(Status::PublicationMatched)?;
    /// // Get the active status mask.
    /// assert_eq!(writer.status_mask()?, Status::PublicationMatched);
    /// # Ok::<_, cyclonedds::Error>(())
    /// ```
    fn set_status_mask(&self, mask: Status) -> Result<()> {
        let entity = self.id();
        let mask = mask.bits();
        ffi::dds_set_status_mask(entity.inner, mask)
    }
}

macro_rules! impl_entity {
    ($ty:ty) => {
        impl Entity for $ty {
            fn id(&self) -> EntityId {
                EntityId { inner: self.inner }
            }
        }
    };
    ($ty:ty where $($bounds:tt)*) => {
        impl<$($bounds)*> Entity for $ty {
            fn id(&self) -> EntityId {
                EntityId { inner: self.inner }
            }
        }
    };
}

impl_entity!(crate::Participant<'_>);
impl_entity!(crate::Topic<'_, '_, T> where T: crate::Topicable);
impl_entity!(crate::Publisher<'_, '_>);
impl_entity!(crate::Subscriber<'_, '_>);
impl_entity!(crate::Reader<'_, '_, '_, T> where T: crate::Topicable);
impl_entity!(crate::Writer<'_, '_, '_, T> where T: crate::Topicable);
impl_entity!(crate::ReadCondition<'_, '_, '_, '_, T> where T: crate::Topicable);
impl_entity!(crate::QueryCondition<'_, '_, '_, '_, T, F> where T: crate::Topicable, F: Fn(&T) -> bool);
impl_entity!(crate::GuardCondition<'_>);
impl_entity!(crate::WaitSet<'_, '_, '_, A> where A);

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

    #[test]
    fn test_entity_id_all_entity_types() {
        let domain_id = crate::tests::domain::unique_id();
        let domain = crate::Domain::new(domain_id).unwrap();
        let participant = crate::Participant::new(&domain).unwrap();
        let topic_name = crate::tests::topic::unique_name();
        let topic =
            crate::Topic::<crate::tests::topic::Data>::new(&participant, &topic_name).unwrap();
        let publisher = crate::Publisher::new(&participant).unwrap();
        let subscriber = crate::Subscriber::new(&participant).unwrap();
        let reader = crate::Reader::new(&topic).unwrap();
        let writer = crate::Writer::new(&topic).unwrap();
        let read_condition = crate::ReadCondition::new(&reader, crate::state::sample::Any).unwrap();
        let query_condition =
            crate::QueryCondition::new(&reader, crate::State::empty(), |_| true).unwrap();
        let guard_condition = crate::GuardCondition::new(&participant).unwrap();
        let waitset = crate::WaitSet::<()>::new(&participant).unwrap();

        assert_eq!(participant.id().inner, participant.inner);
        assert_eq!(topic.id().inner, topic.inner);
        assert_eq!(publisher.id().inner, publisher.inner);
        assert_eq!(subscriber.id().inner, subscriber.inner);
        assert_eq!(reader.id().inner, reader.inner);
        assert_eq!(writer.id().inner, writer.inner);
        assert_eq!(read_condition.id().inner, read_condition.inner);
        assert_eq!(query_condition.id().inner, query_condition.inner);
        assert_eq!(guard_condition.id().inner, guard_condition.inner);
        assert_eq!(waitset.id().inner, waitset.inner);
    }

    #[test]
    fn test_entity_methods_on_invalid_participant() {
        let domain_id = crate::tests::domain::unique_id();
        let domain = crate::Domain::new(domain_id).unwrap();
        let mut participant = crate::Participant::new(&domain).unwrap();
        let participant_id = participant.inner;
        participant.inner = 0;

        assert_eq!(
            crate::Error::BadParameter,
            participant.instance_handle().unwrap_err()
        );
        assert_eq!(
            crate::Error::BadParameter,
            participant.status_changes().unwrap_err()
        );
        assert_eq!(
            crate::Error::BadParameter,
            participant.take_status(None).unwrap_err()
        );
        assert_eq!(
            crate::Error::BadParameter,
            participant.read_status(None).unwrap_err()
        );
        assert_eq!(
            crate::Error::BadParameter,
            participant.status_mask().unwrap_err()
        );
        assert_eq!(
            crate::Error::BadParameter,
            participant
                .set_status_mask(crate::Status::InconsistentTopic)
                .unwrap_err()
        );

        participant.inner = participant_id;
    }

    #[test]
    fn test_entity_methods_on_participant() {
        let domain_id = crate::tests::domain::unique_id();
        let domain = crate::Domain::new(domain_id).unwrap();
        let participant = crate::Participant::new(&domain).unwrap();

        let result = participant.instance_handle();
        assert!(result.is_ok());
        let status_changes = participant.status_changes().unwrap();
        assert!(status_changes.is_empty());
        let result = participant.set_status_mask(crate::Status::empty());
        assert!(result.is_ok());
        let mask = participant.status_mask().unwrap();
        assert_eq!(mask, crate::Status::empty());
        let status = participant
            .read_status(Some(crate::Status::empty()))
            .unwrap();
        assert!(status.is_empty());
        let status = participant
            .take_status(Some(crate::Status::empty()))
            .unwrap();
        assert!(status.is_empty());
    }

    #[test]
    fn test_entity_methods_on_reader() {
        let domain_id = crate::tests::domain::unique_id();
        let domain = crate::Domain::new(domain_id).unwrap();
        let topic_name = crate::tests::topic::unique_name();
        let participant = crate::Participant::new(&domain).unwrap();
        let topic =
            crate::Topic::<crate::tests::topic::Data>::new(&participant, &topic_name).unwrap();
        let reader = crate::Reader::new(&topic).unwrap();

        let result = reader.instance_handle();
        assert!(result.is_ok());
        let status_changes = reader.status_changes().unwrap();
        assert!(status_changes.is_empty());
        let result = reader.set_status_mask(crate::Status::SubscriptionMatched);
        assert!(result.is_ok());
        let mask = reader.status_mask().unwrap();
        assert_eq!(mask, crate::Status::SubscriptionMatched);
        let status = reader
            .read_status(Some(crate::Status::SubscriptionMatched))
            .unwrap();
        assert!(status.is_empty());
        let status = reader
            .take_status(Some(crate::Status::SubscriptionMatched))
            .unwrap();
        assert!(status.is_empty());
    }
}