Skip to content

Commit e4c018e

Browse files
committed
Refactor CFString struct
1 parent 3f180f5 commit e4c018e

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

objc2-foundation/src/__string_macro.rs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//! generating a pure `NSString`. We don't support that yet (since I don't
1010
//! know the use-case), but we definitely could!
1111
//! See: <https://github.com/llvm/llvm-project/blob/release/13.x/clang/lib/CodeGen/CGObjCMac.cpp#L2007-L2068>
12+
use core::ffi::c_void;
1213

1314
use objc2::runtime::Class;
1415

@@ -35,50 +36,32 @@ const FLAGS_ASCII: usize = 0x07_C8;
3536
const FLAGS_UTF16: usize = 0x07_D0;
3637

3738
#[repr(C)]
38-
pub struct CFStringAscii {
39+
pub struct CFConstString {
3940
isa: &'static Class,
4041
flags: usize,
41-
data: *const u8,
42+
data: *const c_void,
4243
len: usize,
4344
}
4445

4546
// Required to place in a `static`.
46-
unsafe impl Sync for CFStringAscii {}
47+
unsafe impl Sync for CFConstString {}
4748

48-
impl CFStringAscii {
49-
pub const unsafe fn new(isa: &'static Class, data: &'static [u8]) -> Self {
49+
impl CFConstString {
50+
pub const unsafe fn new_ascii(isa: &'static Class, data: &'static [u8]) -> Self {
5051
Self {
5152
isa,
5253
flags: FLAGS_ASCII,
53-
data: data.as_ptr(),
54+
data: data.as_ptr().cast(),
5455
// The length does not include the trailing NUL.
5556
len: data.len() - 1,
5657
}
5758
}
5859

59-
#[inline]
60-
pub const fn as_nsstring(&self) -> &NSString {
61-
unsafe { &*(self as *const Self as *const NSString) }
62-
}
63-
}
64-
65-
#[repr(C)]
66-
pub struct CFStringUtf16 {
67-
isa: &'static Class,
68-
flags: usize,
69-
data: *const u16,
70-
len: usize,
71-
}
72-
73-
// Required to place in a `static`.
74-
unsafe impl Sync for CFStringUtf16 {}
75-
76-
impl CFStringUtf16 {
77-
pub const unsafe fn new(isa: &'static Class, data: &'static [u16]) -> Self {
60+
pub const unsafe fn new_utf16(isa: &'static Class, data: &'static [u16]) -> Self {
7861
Self {
7962
isa,
8063
flags: FLAGS_UTF16,
81-
data: data.as_ptr(),
64+
data: data.as_ptr().cast(),
8265
// The length does not include the trailing NUL.
8366
len: data.len() - 1,
8467
}
@@ -304,8 +287,8 @@ macro_rules! ns_string {
304287
};
305288

306289
#[link_section = "__DATA,__cfstring"]
307-
static CFSTRING: $crate::__string_macro::CFStringAscii = unsafe {
308-
$crate::__string_macro::CFStringAscii::new(
290+
static CFSTRING: $crate::__string_macro::CFConstString = unsafe {
291+
$crate::__string_macro::CFConstString::new_ascii(
309292
&$crate::__string_macro::__CFConstantStringClassReference,
310293
&ASCII,
311294
)
@@ -353,8 +336,8 @@ macro_rules! ns_string {
353336
};
354337

355338
#[link_section = "__DATA,__cfstring"]
356-
static CFSTRING: $crate::__string_macro::CFStringUtf16 = unsafe {
357-
$crate::__string_macro::CFStringUtf16::new(
339+
static CFSTRING: $crate::__string_macro::CFConstString = unsafe {
340+
$crate::__string_macro::CFConstString::new_utf16(
358341
&$crate::__string_macro::__CFConstantStringClassReference,
359342
&UTF16,
360343
)

0 commit comments

Comments
 (0)