Skip to content

Commit

Permalink
Use manual CoreFoundation bindings instead of core-foundation-sys
Browse files Browse the repository at this point in the history
This allows the Apple backend to remain no_std compatible.
  • Loading branch information
complexspaces committed Nov 6, 2022
1 parent f4dee46 commit 6717e56
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ license = "MIT OR Apache-2.0"
[target.'cfg(target_os = "android")'.dependencies]
libc = "0.2"

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
core-foundation-sys = "0.8"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winnls"] }

Expand Down
49 changes: 44 additions & 5 deletions src/apple.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
use alloc::{string::String, vec::Vec};
use core_foundation_sys::{
array::{CFArrayGetCount, CFArrayGetValueAtIndex, CFArrayRef},
base::{Boolean, CFIndex, CFRange, CFRelease},
string::{kCFStringEncodingUTF8, CFStringGetBytes, CFStringGetLength, CFStringRef},
};
use core::ffi::c_void;

type CFIndex = isize;
type Boolean = u8;
type CFStringEncoding = u32;

#[allow(non_upper_case_globals)]
const kCFStringEncodingUTF8: CFStringEncoding = 0x08000100;

#[repr(C)]
#[derive(Clone, Copy)]
struct CFRange {
pub location: CFIndex,
pub length: CFIndex,
}

type CFTypeRef = *const c_void;

#[repr(C)]
struct __CFArray(c_void);
type CFArrayRef = *const __CFArray;

#[repr(C)]
struct __CFString(c_void);
type CFStringRef = *const __CFString;

// Most of these definitions come from `core-foundation-sys`, but we want this crate
// to be `no_std` and `core-foundation-sys` isn't currently.
#[link(name = "CoreFoundation", kind = "framework")]
extern "C" {
fn CFArrayGetCount(theArray: CFArrayRef) -> CFIndex;
fn CFArrayGetValueAtIndex(theArray: CFArrayRef, idx: CFIndex) -> *const c_void;

fn CFStringGetLength(theString: CFStringRef) -> CFIndex;
fn CFStringGetBytes(
theString: CFStringRef,
range: CFRange,
encoding: CFStringEncoding,
lossByte: u8,
isExternalRepresentation: Boolean,
buffer: *mut u8,
maxBufLen: CFIndex,
usedBufLen: *mut CFIndex,
) -> CFIndex;

fn CFRelease(cf: CFTypeRef);

fn CFLocaleCopyPreferredLanguages() -> CFArrayRef;
}

Expand Down

0 comments on commit 6717e56

Please sign in to comment.