use std::ffi::c_void;
pub type CFStringRef = *const c_void;
const ENCODING_UTF8: u32 = 0x0800_0100;
#[link(name = "CoreFoundation", kind = "framework")]
extern "C" {
fn CFStringCreateWithBytes(
alloc: *const c_void,
bytes: *const u8,
num_bytes: isize,
encoding: u32,
is_external_representation: u8,
) -> CFStringRef;
fn CFRelease(cf: *const c_void);
}
#[must_use]
pub fn create_string(text: &str) -> CFStringRef {
unsafe {
CFStringCreateWithBytes(
core::ptr::null(),
text.as_ptr(),
text.len() as isize,
ENCODING_UTF8,
0,
)
}
}
pub unsafe fn release(cf: *const c_void) {
unsafe { CFRelease(cf) };
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn create_string_returns_a_live_object_that_releases() {
let cfstr = create_string("Tympan Minimal Loopback");
assert!(!cfstr.is_null(), "CFStringCreateWithBytes should succeed");
unsafe { release(cfstr) };
}
#[test]
fn create_string_handles_an_empty_string() {
let cfstr = create_string("");
assert!(
!cfstr.is_null(),
"an empty CFString is still a valid object"
);
unsafe { release(cfstr) };
}
#[test]
fn create_string_handles_non_ascii() {
let cfstr = create_string("Tympan — 鼓膜 — loopback");
assert!(!cfstr.is_null());
unsafe { release(cfstr) };
}
}