cyclonedds_rs/
dds_domain.rs1use cyclonedds_sys::{dds_error::DDSError, DdsDomainId, DdsEntity};
19use std::convert::From;
20use std::ffi::CString;
21
22pub struct DdsDomain(DdsEntity);
23
24impl DdsDomain {
25 pub fn create(domain: DdsDomainId, config: Option<&str>) -> Result<Self, DDSError> {
27 unsafe {
28 if let Some(cfg) = config {
29 let domain_name = CString::new(cfg).expect("Unable to create new config string");
30 let d = cyclonedds_sys::dds_create_domain(domain, domain_name.as_ptr());
31 if d > 0 {
33 Ok(DdsDomain(DdsEntity::new(d)))
34 } else {
35 Err(DDSError::from(d))
36 }
37 } else {
38 let d = cyclonedds_sys::dds_create_domain(domain, std::ptr::null());
39
40 if d > 0 {
41 Ok(DdsDomain(DdsEntity::new(d)))
42 } else {
43 Err(DDSError::from(d))
44 }
45 }
46 }
47 }
48}
49
50impl PartialEq for DdsDomain {
51 fn eq(&self, other: &Self) -> bool {
52 unsafe { self.0.entity() == other.0.entity() }
53 }
54}
55
56impl Eq for DdsDomain {}
57
58impl Drop for DdsDomain {
59 fn drop(&mut self) {
60 unsafe {
61 let ret: DDSError = cyclonedds_sys::dds_delete(self.0.entity()).into();
62 if DDSError::DdsOk != ret {
63 panic!("cannot delete domain: {}", ret);
64 }
65 }
66 }
67}
68
69#[cfg(test)]
70mod dds_domain_tests {
71 use cyclonedds_sys::{DDSError};
72 use crate::dds_domain::DdsDomain;
73
74 #[test]
75 fn test_create_domain_with_bad_config() {
76 assert!(Err(DDSError::DdsOk) != DdsDomain::create(0, Some("blah")));
77 }
78
79
80
81}