Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Use objc2 #63

Closed
martial-plains opened this issue Jun 26, 2022 · 4 comments
Closed

Use objc2 #63

martial-plains opened this issue Jun 26, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@martial-plains
Copy link
Owner

Use objc2 instead of rust-objc

@madsmtm
Copy link

madsmtm commented Nov 11, 2022

Hi there, just discovered this repo after a cursory look at lib.rs, it looks like you did some really nice work here!

I'm currently in the process of creating a setup to automatically generate bindings for Apple frameworks over at madsmtm/objc2#264, and am quite interested in collaborating with your bindings here.

My plan with icrate (as I've currently dubbed the subproject) is to auto-generate the bindings from a configuration file, allowing us to e.g. mark methods as safe, or override it with something else. The desire is also to enrich it with documentation from https://developer.apple.com/ at some point.
And then we'd build on top of that (within the same crate), to provide a better user-experience for the things that are difficult to autogenerate (like the NSCopying protocol, or implementing various iterator traits for NSArray).

A few other things:

  • I assume you've created these bindings by hand? If not, what tool did you use?
  • Even if you decide not to use the autogenerated bindings, I think objc2 could do a lot of good with its msg_send_id! macro, which automatically handles memory management rules for you (or even better, the extern_methods/extern_class/extern_protocol macros).
    • As an example of what this would fix, PNSObject::m_new (and many others) currently leak memory, because it uses FromId::from_id which retains the object, while +[NSObject new] returns an object with +1 retain count that the user is expected to release.
  • Are you aware of the two projects fruity and cacao that try to fill a similar niche as this one?
  • I see that you've adopted a specific naming scheme for your methods (prefixed m_ for method, p_ for property) - if you think I should do something similar in icrate would be willing to discuss it in Figure out naming scheme in icrate madsmtm/objc2#284

Feel free to ask me any questions if there's something I haven't fully explained!

@martial-plains
Copy link
Owner Author

Hey @madsmtm. I think automatically generating bindings for Apple's frameworks will be awesome and I will be happy collaborate with anything I can.

Yes, I've done these bindings by hand. It took a lot of time 🤣.

Thank you for pointing out some of those advantages for using objc2. I've been wanting to use objc2 for awhile but on the GitHub page said that it's a WIP and said to use the objc family of crates instead... But considering you are one of the maintainers of the objc2 recommending me to use it I will give it a shot😊.

I honestly didn't know much about them in all honesty.

@madsmtm
Copy link

madsmtm commented Nov 11, 2022

Hey @madsmtm. I think automatically generating bindings for Apple's frameworks will be awesome and I will be happy collaborate with anything I can.

Wonderful! Let me just give you a general introduction to the project:

  • objc_id -> objc2::rc
  • PNSObject ~= objc2::ClassType
  • object!/shared_object! ~= objc2::extern_class!
    • I've gone with implementing Deref in a similar way as you have, though see Downgrading using Deref madsmtm/objc2#58, it is also used for accessing subclasses methods
    • The driving factor here was usability (though it also has a significant effect on code-size): It is much easier for the user to just type NSMutableString::new().length(), and then have that call -[NSString length] without having to import two extra traits PNSObject and INSString.
  • interface_impl! ~= objc2::extern_methods!
  • register_class! ~= objc2::declare_class!

My long-term plan for the project is roughly:

  1. Get icrate to a working point
  2. Use icrate to replace libraries like core-foundation-sys, metal, ...
  3. For even higher-level stuff that is out of scope for icrate, do it in cacao instead - see discussion in Initial conversion to objc2 ryanmcgrath/cacao#30 (comment)

Yes, I've done these bindings by hand. It took a lot of time 🤣.

Yeah, I figured! Though I suspect it has given you a lot of insight into what would be a nice API, and that insight is quite invaluable to me ;)

Again, feel free to ask or discuss anything, a lot of decisions in the project I've made by myself, and I'm very open to reconsidering them!

Thank you for pointing out some of those advantages for using objc2. I've been wanting to use objc2 for awhile but on the GitHub page said that it's a WIP and said to use the objc family of crates instead... But considering you are one of the maintainers of the objc2 recommending me to use it I will give it a shot😊.

I honestly didn't know much about them in all honesty.

Yeah, that was meant as a big "this is WIP, don't start relying on it unless you've told me about it and I can help you with any issues you may encounter", but I can see why it may have just scared people away, oops... See also madsmtm/objc2#268

@martial-plains
Copy link
Owner Author

@madsmtm I'm looking forward to using icrate in more and more of my projects 😊 It's a very cool crate. I'll be closing this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants