1
- use objc_foundation:: { NSString , INSString } ;
2
1
use objc:: { msg_send, sel, sel_impl} ;
3
- use std :: ffi :: { CString , c_void } ;
2
+ use objc_foundation :: { INSString , NSString } ;
4
3
use objc_id:: Id ;
4
+ use std:: ffi:: { c_void, CString } ;
5
5
use std:: os:: raw:: c_char;
6
6
7
7
pub fn leak ( str : & str ) {
@@ -14,7 +14,6 @@ pub fn no_leak(str: &str) {
14
14
nsstring_to_rust_string ( nsstrig) ;
15
15
}
16
16
17
-
18
17
/**
19
18
Function that converts NSString to rust string through CString to prevent a memory leak.
20
19
@@ -32,7 +31,8 @@ pub fn nsstring_to_rust_string(nsstring: Id<NSString>) -> String {
32
31
let string_size: usize = msg_send ! [ nsstring, lengthOfBytesUsingEncoding: 4 ] ;
33
32
// + 1 is because getCString returns null terminated string
34
33
let buffer = libc:: malloc ( string_size + 1 ) as * mut c_char ;
35
- let is_success: bool = msg_send ! [ nsstring, getCString: buffer maxLength: string_size+1 encoding: 4 ] ;
34
+ let is_success: bool =
35
+ msg_send ! [ nsstring, getCString: buffer maxLength: string_size+1 encoding: 4 ] ;
36
36
if is_success {
37
37
// CString will take care of memory from now on
38
38
CString :: from_raw ( buffer) . to_str ( ) . unwrap ( ) . to_owned ( )
@@ -56,4 +56,4 @@ mod tests {
56
56
let nsstrig = NSString :: from_str ( "aaabbb🍺Ыض" ) ;
57
57
assert_eq ! ( nsstring_to_rust_string( nsstrig) , "aaabbb🍺Ыض" ) ;
58
58
}
59
- }
59
+ }
0 commit comments