#![allow(clippy::missing_safety_doc)]
#![allow(missing_docs)]
pub mod condition;
pub mod entity;
pub mod listener;
pub mod rpc;
pub mod stream;
pub mod types;
use std::cell::RefCell;
use std::ffi::{CStr, CString, c_char};
use std::panic::{AssertUnwindSafe, catch_unwind};
pub type Status = i32;
pub const OK: Status = 0;
pub const ERROR: Status = 1;
pub const UNSUPPORTED: Status = 2;
pub const BAD_PARAMETER: Status = 3;
pub const PRECONDITION_NOT_MET: Status = 4;
pub const OUT_OF_RESOURCES: Status = 5;
pub const ALREADY_DELETED: Status = 9;
pub const TIMEOUT: Status = 10;
pub const NO_DATA: Status = 11;
pub const ABI_VERSION: u32 = 1;
#[unsafe(no_mangle)]
pub extern "C" fn rdds_abi_version() -> u32 {
ABI_VERSION
}
thread_local! {
static LAST_ERROR: RefCell<CString> = RefCell::new(CString::default());
}
#[unsafe(no_mangle)]
pub extern "C" fn rdds_last_error_message() -> *const c_char {
LAST_ERROR.with(|e| e.borrow().as_ptr())
}
#[derive(Debug)]
pub(crate) struct Fail(pub Status, pub String);
pub(crate) type Res<T = ()> = std::result::Result<T, Fail>;
pub(crate) fn bad(msg: impl Into<String>) -> Fail {
Fail(BAD_PARAMETER, msg.into())
}
impl From<rustorb_dds::Error> for Fail {
fn from(e: rustorb_dds::Error) -> Fail {
use rustorb_dds::Error as E;
let code = match &e {
E::Serialize(_) | E::Dynamic(_) => BAD_PARAMETER,
E::AlreadyDeleted => ALREADY_DELETED,
E::OutOfResources => OUT_OF_RESOURCES,
E::Timeout => TIMEOUT,
E::PreconditionNotMet => PRECONDITION_NOT_MET,
E::Unsupported(_) => UNSUPPORTED,
_ => ERROR,
};
Fail(code, e.to_string())
}
}
impl From<rustorb_xtypes::Error> for Fail {
fn from(e: rustorb_xtypes::Error) -> Fail {
bad(e.to_string())
}
}
fn set_error(msg: &str) {
let c = CString::new(msg.replace('\0', " ")).unwrap_or_default();
LAST_ERROR.with(|e| *e.borrow_mut() = c);
}
pub(crate) fn guard(f: impl FnOnce() -> Res) -> Status {
match catch_unwind(AssertUnwindSafe(f)) {
Ok(Ok(())) => OK,
Ok(Err(Fail(code, msg))) => {
set_error(&msg);
code
}
Err(p) => {
let msg = p
.downcast_ref::<&str>()
.map(|s| s.to_string())
.or_else(|| p.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "panic".into());
set_error(&format!("internal error: {msg}"));
ERROR
}
}
}
pub(crate) fn guard_value<T>(default: T, f: impl FnOnce() -> T) -> T {
catch_unwind(AssertUnwindSafe(f)).unwrap_or(default)
}
pub(crate) unsafe fn obj<'a, T>(p: *const T, what: &str) -> Res<&'a T> {
unsafe { p.as_ref() }.ok_or_else(|| bad(format!("{what} is NULL")))
}
pub(crate) unsafe fn put<T>(out: *mut *mut T, v: T) -> Res {
if out.is_null() {
return Err(bad("the output pointer is NULL"));
}
unsafe { *out = Box::into_raw(Box::new(v)) };
Ok(())
}
pub(crate) unsafe fn set<T>(out: *mut T, v: T) -> Res {
if out.is_null() {
return Err(bad("the output pointer is NULL"));
}
unsafe { out.write(v) };
Ok(())
}
pub(crate) unsafe fn cstr<'a>(p: *const c_char, what: &str) -> Res<&'a str> {
if p.is_null() {
return Err(bad(format!("{what} is NULL")));
}
unsafe { CStr::from_ptr(p) }
.to_str()
.map_err(|_| bad(format!("{what} is not UTF-8")))
}
pub(crate) unsafe fn slice<'a, T>(p: *const T, n: usize) -> Res<&'a [T]> {
if n == 0 {
return Ok(&[]);
}
if p.is_null() {
return Err(bad("an array pointer is NULL"));
}
Ok(unsafe { std::slice::from_raw_parts(p, n) })
}
pub(crate) unsafe fn release<T>(p: *mut T) {
if !p.is_null() {
let _ = catch_unwind(AssertUnwindSafe(|| {
drop(unsafe { Box::from_raw(p) })
}));
}
}