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
13pub(crate) fn cstring_until_nul(value: &str) -> std::ffi::CString {
14 let prefix = value.split('\0').next().unwrap_or_default();
15 std::ffi::CString::new(prefix).unwrap_or_default()
16}
17
18/// Retrieves a string from an FFI function that returns an owned C string
19/// pointer allocated by Swift (typically via `strdup`), freeing it with
20/// `acf_free_string`.
21///
22/// This is a thin convenience over
23/// [`doom_fish_utils::ffi_string::ffi_string_owned`].
24///
25/// # Safety
26/// The caller must ensure the returned pointer was allocated by the
27/// `apple-cf` Swift bridge (so that `acf_free_string` correctly releases
28/// it). See the underlying helper for full safety contract.
29pub unsafe fn ffi_string_owned<F>(ffi_call: F) -> Option<String>
30where
31 F: FnOnce() -> *mut i8,
32{
33 unsafe {
34 doom_fish_utils::ffi_string::ffi_string_owned(ffi_call, |ptr| {
35 crate::ffi::acf_free_string(ptr);
36 })
37 }
38}
39
40/// Same as [`crate::utils::ffi_string::ffi_string_owned`] but returns an empty
41/// string on failure.
42///
43/// # Safety
44/// Same requirements as [`crate::utils::ffi_string::ffi_string_owned`].
45pub unsafe fn ffi_string_owned_or_empty<F>(ffi_call: F) -> String
46where
47 F: FnOnce() -> *mut i8,
48{
49 unsafe { ffi_string_owned(ffi_call) }.unwrap_or_default()
50}