nautilus_core/ffi/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! C foreign function interface (FFI) from [cbindgen](https://github.com/mozilla/cbindgen).
17//!
18//! All exported functions route through `abort_on_panic` so that any panic inside the
19//! Rust implementation aborts immediately instead of unwinding across the foreign boundary.
20//! Unwinding into C/Python is undefined behaviour, so this keeps the existing fail-fast
21//! semantics while avoiding subtle stack corruption during debugging.
22
23#![allow(unsafe_code)]
24#![allow(unsafe_attr_outside_unsafe)]
25
26pub mod cvec;
27pub mod datetime;
28pub mod parsing;
29pub mod string;
30pub mod uuid;
31
32use std::{
33    panic::{self, AssertUnwindSafe},
34    process,
35};
36
37/// Executes `f`, aborting the process if it panics.
38///
39/// FFI exports always call this helper so a panic never unwinds across the
40/// `extern "C"` boundary. Unwinding into C/Python is undefined behaviour and
41/// can silently corrupt the foreign stack; aborting instead preserves the
42/// fail-fast guarantee with effectively no debugging downside (the panic
43/// message is still logged before the abort).
44#[inline]
45pub(crate) fn abort_on_panic<F, R>(f: F) -> R
46where
47    F: FnOnce() -> R,
48{
49    match panic::catch_unwind(AssertUnwindSafe(f)) {
50        Ok(result) => result,
51        Err(_) => process::abort(),
52    }
53}