Skip to main content

doom_fish_utils/
ffi_callbacks.rs

1//! Common `extern "C"` callback type aliases used across the doom-fish family.
2//!
3//! These callback signatures recur in many crates' Swift / Obj-C bridges.
4//! Centralising them here eliminates duplicate definitions and makes
5//! cross-crate FFI signatures interchangeable.
6
7use core::ffi::{c_char, c_void};
8
9/// Callback that delivers a JSON-encoded payload as a heap-owned C string.
10///
11/// The receiver is responsible for freeing `json` (typically by passing it back
12/// into a Swift bridge `free_*` helper or via the consuming wrapper's Drop impl).
13pub type JsonCallback = unsafe extern "C" fn(json: *mut c_char, user_data: *mut c_void);
14
15/// Callback that fires when a one-shot async operation completes.
16///
17/// `status` is the operation's exit code (0 = ok, non-zero = error code).
18/// `error` is an optional `NSError` JSON payload (may be null on success).
19pub type AsyncCallback = unsafe extern "C" fn(
20    status: i32,
21    error: *mut c_char,
22    user_data: *mut c_void,
23);
24
25/// Fire-and-forget callback with no payload other than the `user_data` pointer.
26pub type SimpleCallback = unsafe extern "C" fn(user_data: *mut c_void);
27
28/// Drop notification — fires when the Swift side releases the bridged context.
29/// Use this to clean up Rust state (typically `Box::from_raw(user_data)`).
30pub type DropCallback = unsafe extern "C" fn(user_data: *mut c_void);
31
32/// Stream-style callback that fires for each event in an ongoing subscription.
33///
34/// `event_json` is a heap-owned JSON-encoded event payload (caller-owned).
35pub type StreamEventCallback = unsafe extern "C" fn(
36    event_json: *mut c_char,
37    user_data: *mut c_void,
38);
39
40/// Async callback variant used by avassetwriter / weatherkit / webkit bridges
41/// that deliver a (`status`, `result_json`, `error`, `user_data`) tuple.
42pub type AsyncCb = unsafe extern "C" fn(
43    status: i32,
44    result_json: *mut c_char,
45    error: *mut c_char,
46    user_data: *mut c_void,
47);