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 =
20    unsafe extern "C" fn(status: i32, error: *mut c_char, user_data: *mut c_void);
21
22/// One-shot unit completion callback used by [`crate::completion::UnitCompletion`].
23///
24/// `context` must remain the exact live context pointer for the callback's
25/// single invocation. `error` may be null and is only read when `success` is
26/// false.
27pub type UnitCompletionCallback =
28    unsafe extern "C" fn(context: *mut c_void, success: bool, error: *const c_char);
29
30/// Fire-and-forget callback with no payload other than the `user_data` pointer.
31pub type SimpleCallback = unsafe extern "C" fn(user_data: *mut c_void);
32
33/// Drop notification — fires when the Swift side releases the bridged context.
34/// Use this to clean up Rust state (typically `Box::from_raw(user_data)`).
35pub type DropCallback = unsafe extern "C" fn(user_data: *mut c_void);
36
37/// Stream-style callback that fires for each event in an ongoing subscription.
38///
39/// `event_json` is a heap-owned JSON-encoded event payload (caller-owned).
40pub type StreamEventCallback =
41    unsafe extern "C" fn(event_json: *mut c_char, user_data: *mut c_void);
42
43/// Async callback variant used by avassetwriter / weatherkit / webkit bridges
44/// that deliver a (`status`, `result_json`, `error`, `user_data`) tuple.
45pub type AsyncCb = unsafe extern "C" fn(
46    status: i32,
47    result_json: *mut c_char,
48    error: *mut c_char,
49    user_data: *mut c_void,
50);