Skip to main content

apple_cf/utils/
ffi_string.rs

1//! FFI string utilities — back-compat shim over `doom_fish_utils::ffi_string`.
2//!
3//! Re-exports the generic, framework-agnostic helpers and adds the thin
4//! wrappers [`crate::utils::ffi_string::ffi_string_owned`] and
5//! [`crate::utils::ffi_string::ffi_string_owned_or_empty`]. They bake in
6//! `crate::ffi::acf_free_string` as the deallocator so existing call sites
7//! that used the apple-cf-specific helpers compile unchanged.
8
9pub use doom_fish_utils::ffi_string::{
10    ffi_string_from_buffer, ffi_string_from_buffer_or_empty, DEFAULT_BUFFER_SIZE, SMALL_BUFFER_SIZE,
11};
12
13/// Retrieves a string from an FFI function that returns an owned C string
14/// pointer allocated by Swift (typically via `strdup`), freeing it with
15/// `acf_free_string`.
16///
17/// This is a thin convenience over
18/// [`doom_fish_utils::ffi_string::ffi_string_owned`].
19///
20/// # Safety
21/// The caller must ensure the returned pointer was allocated by the
22/// `apple-cf` Swift bridge (so that `acf_free_string` correctly releases
23/// it). See the underlying helper for full safety contract.
24pub unsafe fn ffi_string_owned<F>(ffi_call: F) -> Option<String>
25where
26    F: FnOnce() -> *mut i8,
27{
28    unsafe {
29        doom_fish_utils::ffi_string::ffi_string_owned(ffi_call, |ptr| {
30            crate::ffi::acf_free_string(ptr);
31        })
32    }
33}
34
35/// Same as [`crate::utils::ffi_string::ffi_string_owned`] but returns an empty
36/// string on failure.
37///
38/// # Safety
39/// Same requirements as [`crate::utils::ffi_string::ffi_string_owned`].
40pub unsafe fn ffi_string_owned_or_empty<F>(ffi_call: F) -> String
41where
42    F: FnOnce() -> *mut i8,
43{
44    unsafe { ffi_string_owned(ffi_call) }.unwrap_or_default()
45}