1#![allow(clippy::missing_safety_doc)]
26#![allow(missing_docs)]
28
29pub mod condition;
30pub mod entity;
31pub mod listener;
32pub mod rpc;
33pub mod stream;
34pub mod types;
35
36use std::cell::RefCell;
37use std::ffi::{CStr, CString, c_char};
38use std::panic::{AssertUnwindSafe, catch_unwind};
39
40pub type Status = i32;
42
43pub const OK: Status = 0;
45pub const ERROR: Status = 1;
47pub const UNSUPPORTED: Status = 2;
49pub const BAD_PARAMETER: Status = 3;
51pub const PRECONDITION_NOT_MET: Status = 4;
53pub const OUT_OF_RESOURCES: Status = 5;
55pub const ALREADY_DELETED: Status = 9;
57pub const TIMEOUT: Status = 10;
59pub const NO_DATA: Status = 11;
61
62pub const ABI_VERSION: u32 = 1;
64
65#[unsafe(no_mangle)]
68pub extern "C" fn rdds_abi_version() -> u32 {
69 ABI_VERSION
70}
71
72thread_local! {
73 static LAST_ERROR: RefCell<CString> = RefCell::new(CString::default());
74}
75
76#[unsafe(no_mangle)]
79pub extern "C" fn rdds_last_error_message() -> *const c_char {
80 LAST_ERROR.with(|e| e.borrow().as_ptr())
81}
82
83#[derive(Debug)]
85pub(crate) struct Fail(pub Status, pub String);
86
87pub(crate) type Res<T = ()> = std::result::Result<T, Fail>;
88
89pub(crate) fn bad(msg: impl Into<String>) -> Fail {
90 Fail(BAD_PARAMETER, msg.into())
91}
92
93impl From<rustorb_dds::Error> for Fail {
94 fn from(e: rustorb_dds::Error) -> Fail {
95 use rustorb_dds::Error as E;
96 let code = match &e {
97 E::Serialize(_) | E::Dynamic(_) => BAD_PARAMETER,
98 E::AlreadyDeleted => ALREADY_DELETED,
99 E::OutOfResources => OUT_OF_RESOURCES,
100 E::Timeout => TIMEOUT,
101 E::PreconditionNotMet => PRECONDITION_NOT_MET,
102 E::Unsupported(_) => UNSUPPORTED,
103 _ => ERROR,
104 };
105 Fail(code, e.to_string())
106 }
107}
108
109impl From<rustorb_xtypes::Error> for Fail {
110 fn from(e: rustorb_xtypes::Error) -> Fail {
111 bad(e.to_string())
112 }
113}
114
115fn set_error(msg: &str) {
116 let c = CString::new(msg.replace('\0', " ")).unwrap_or_default();
117 LAST_ERROR.with(|e| *e.borrow_mut() = c);
118}
119
120pub(crate) fn guard(f: impl FnOnce() -> Res) -> Status {
122 match catch_unwind(AssertUnwindSafe(f)) {
123 Ok(Ok(())) => OK,
124 Ok(Err(Fail(code, msg))) => {
125 set_error(&msg);
126 code
127 }
128 Err(p) => {
129 let msg = p
130 .downcast_ref::<&str>()
131 .map(|s| s.to_string())
132 .or_else(|| p.downcast_ref::<String>().cloned())
133 .unwrap_or_else(|| "panic".into());
134 set_error(&format!("internal error: {msg}"));
135 ERROR
136 }
137 }
138}
139
140pub(crate) fn guard_value<T>(default: T, f: impl FnOnce() -> T) -> T {
143 catch_unwind(AssertUnwindSafe(f)).unwrap_or(default)
144}
145
146pub(crate) unsafe fn obj<'a, T>(p: *const T, what: &str) -> Res<&'a T> {
148 unsafe { p.as_ref() }.ok_or_else(|| bad(format!("{what} is NULL")))
150}
151
152pub(crate) unsafe fn put<T>(out: *mut *mut T, v: T) -> Res {
154 if out.is_null() {
155 return Err(bad("the output pointer is NULL"));
156 }
157 unsafe { *out = Box::into_raw(Box::new(v)) };
159 Ok(())
160}
161
162pub(crate) unsafe fn set<T>(out: *mut T, v: T) -> Res {
164 if out.is_null() {
165 return Err(bad("the output pointer is NULL"));
166 }
167 unsafe { out.write(v) };
169 Ok(())
170}
171
172pub(crate) unsafe fn cstr<'a>(p: *const c_char, what: &str) -> Res<&'a str> {
174 if p.is_null() {
175 return Err(bad(format!("{what} is NULL")));
176 }
177 unsafe { CStr::from_ptr(p) }
179 .to_str()
180 .map_err(|_| bad(format!("{what} is not UTF-8")))
181}
182
183pub(crate) unsafe fn slice<'a, T>(p: *const T, n: usize) -> Res<&'a [T]> {
185 if n == 0 {
186 return Ok(&[]);
187 }
188 if p.is_null() {
189 return Err(bad("an array pointer is NULL"));
190 }
191 Ok(unsafe { std::slice::from_raw_parts(p, n) })
193}
194
195pub(crate) unsafe fn release<T>(p: *mut T) {
197 if !p.is_null() {
198 let _ = catch_unwind(AssertUnwindSafe(|| {
199 drop(unsafe { Box::from_raw(p) })
201 }));
202 }
203}