polyhorn_ios_sys/foundation/
string.rs1use objc::runtime::*;
2use objc::*;
3
4use crate::{IntoRaw, Raw};
5
6pub struct NSString {
8 object: *mut Object,
9}
10
11const NS_UTF8_STRING_ENCODING: usize = 4;
12
13impl From<&str> for NSString {
14 fn from(value: &str) -> Self {
15 unsafe {
16 let mut object: *mut Object = msg_send![class!(NSString), alloc];
17 object = msg_send![object, initWithBytes: value.as_ptr()
18 length: value.len() as usize
19 encoding: NS_UTF8_STRING_ENCODING];
20 NSString::from_raw(object)
21 }
22 }
23}
24
25impl IntoRaw for &String {
26 type Raw = NSString;
27
28 fn into_raw(self) -> Self::Raw {
29 self.as_str().into()
30 }
31}
32
33impl Raw for NSString {
34 unsafe fn from_raw(object: *mut Object) -> Self {
35 NSString { object }
36 }
37
38 unsafe fn as_raw(&self) -> *mut Object {
39 self.object
40 }
41}
42
43impl Drop for NSString {
44 fn drop(&mut self) {
45 unsafe { objc_release(self.object) }
46 }
47}