Skip to main content

ffi_string_owned

Function ffi_string_owned 

Source
pub unsafe fn ffi_string_owned<F, Free>(
    ffi_call: F,
    free_fn: Free,
) -> Option<String>
where F: FnOnce() -> *mut i8, Free: FnOnce(*mut i8),
Expand description

Retrieves a string from an FFI function that returns an owned C string pointer.

This is more efficient than buffer-based retrieval as it avoids pre-allocation. The FFI function allocates the string (typically via Swift’s strdup) and this helper takes ownership and frees it using the caller-supplied free_fn.

§Arguments

  • ffi_call - A closure that returns an owned C string pointer (or null)
  • free_fn - A function that frees the pointer returned by ffi_call. Each bridging crate passes its own _free_string extern "C" here (e.g. apple_cf::ffi::acf_free_string, screencapturekit::ffi::sc_free_string).

§Returns

  • Some(String) if the pointer was non-null and the string was non-empty
  • None if the pointer was null or the string was empty

§Safety

The caller must ensure the returned pointer was allocated by the Swift / Objective-C side using a strategy that free_fn correctly releases. The pointer is freed via an RAII guard, so a panic in from_utf8_lossy (extremely rare — only OOM) does not leak the bridge-allocated buffer.