-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to import and use constants #114
Comments
For reference, the implementation should be changed to: #[repr(C)]
pub struct $name {
_private: [u8; 0],
} Not really sure about the usage code you provided though, depends on what your tolerance for UB is? (Concretely, it would double-free if there was a panic between the two message sends, and may be UB depending on how Rust structs are laid out). Happy to answer further questions! |
Thanks a ton for the swift and thorough response. Slightly OT, but are there any resources like guidelines, docs or real-world examples either to help use the objc crate correctly, for people who are also Objective C beginners? It appears most people use this crate for Apple APIs FFI, but everyone seems to use it slightly differently. I can speak for myself and several of the Tauri contributors that we'd need to understand this better. |
I've been working on a few crates to improve the soundness situation, see In particular for this case, I've come up with a macro Case-in-point, I was wrong before, you should use |
Just to be clear, it should be: let ns_dict = class!(NSDictionary);
let ns_number = class!(NSNumber);
let options: Id<NSDictionary<NSObject, NSObject>> = unsafe {
let obj: Id<NSObject> = Id::from_ptr(msg_send![ns_number, numberWithBool: objc::runtime::YES]);
Id::from_ptr(msg_send![ns_dict, dictionaryWithObject: &*obj forKey: NSPasteboardURLReadingFileURLsOnlyKey])
}; (At least I'm fairly certain...) |
I'm interested if it helps with soundness & readability. And I know more people who are (in Tauri). I think holding off until it's stable to avoid API churn is a good idea though, but at that point feel free to PR the repo linked above if you need testing grounds.
Yes, I was able to repro a segfault if I wrapped my FFI calls in an autoreleasepool. I think it was double-free in my case.
Thank you. It fixes the spurious segfault. Just goes to show how important it is to have a sound API, or if not possible, good docs - especially around memory management. |
I'm modifying a library that uses objc. I need to import a constant NSString. I tried
const* NSString
andId<NSString>
but rustc warned me that these are not "FFI safe". This, however, appears to work:Here is the usage:
Is this correct, safe and idiomatic?
The text was updated successfully, but these errors were encountered: