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