-
Notifications
You must be signed in to change notification settings - Fork 745
Objective-C support #109
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
Comments
I would be happy to be the primary driver behind this feature, and any help/comments on the API of |
Hi, thanks for opening this! I agree this would be nice to have, so if you want to implement this, please do! I'll try to help answer your questions as much as I can :) To be fair, I wouldn't worry too much about the code generation, if we get the parsing right it'd be straight-forward to do. Seems like a good way to start would be adapting our IR to support the equivalent to: FWIW, this is the ast generated by your example:
And there are a bunch of objective C related methods in the cursor docs. Let me know if you need any help. Thanks for this again! :) |
cc @nox who has been working on related things. |
Okay, I've had a look at the structure of the code. It looks like (from a 1000 metre view) that the main entry point creates a Therefore, extending the parser to support objc should mostly require expanding out the One issue I've come across so far is that |
Yep, that sounds accurate, you probably need to add more IIRC with clang 3.9 it parses the file automatically as objc if it ends with |
We started getting some Objective-C support, didn't we? |
Right, it's in progress though. |
So over the last year I've been adding more and more of this features and it's getting to a good place I think. Anyway, I'd like to summarize what the features are really getting to:
I spent a bunch of time the other day trying to take advantage of the nullability attributes in clang and hopefully return Option<_>, with the None case matching to a nil but I couldn't figure out how to get the dumb attribute out of clang. The other thing I spent a bunch of time exploring was how to determine "safety" for functions but I just don't think there's a way to make it "safe" with any assurance. One way to do this, is to add a "safe list" concept that's specified by the user. I just think that if you'd make it too easy for a user to add safe stuff it could get ignored. Thoughts? |
With the Objective-C crate, the machinery to send messages is available in stable Rust. From there, it should be possible to parse Objective-C headers, and generate Rust bindings for them.
This would greatly help with integrating Rust into existing projects, especially iOS and Mac apps.
Some ideas about how this could be done:
id
. Class methods and propertiescan generate an anonymous impl, which calls to
msg_send!
. Note that Objective-C has noconcept of '
const
' methods, so every generated method will take&mut self
.A
andB
, whereB
isinherited from
A
, then we canimpl Deref<Target=A> for B { .. }
andimpl DerefMut<Target=A> for B
. Because Objective-C only has single inheritance, there should be no ambiguity about whichclass to deref to. Base classes won't need this.
method which gets the object pointer, which will be auto implemented on the struct which wraps
the object which implements this protocol. The protocol's methods will then use this method to
provide default implementations which call
msg_send!
on the pointer returned by this method.impl
blocks on a type in a crate, so extensions can just open up anotherimpl
block.A probably-incomplete example which ties these ideas together:
Could generate:
Note that these bindings are not the most ergonomic to use: each method is
unsafe
, each variable has to be declaredmut
to actually call any methods on it, and null pointer checking has to be performed manually (it may be worth adding anis_null
method to NSObject, or to a new trait which is automatically implemented by objc base objects). However, they take a lot of the tedium out of writing bindings manually. Furthermore, it could be possible to refine the ergonomics further:Option<_>
, with theNone
case matching to anil
.If we can parse the the new nullability attributes,
we could probably just return the object if it was declared
_Nonnull
in the header.Mutable
was present to decidewhether methods should pass
&self
or&mut self
, or perhaps it might be possible to add acustom attribute, e.g.
__attribute__((rust_objc_mutating))
to method declarations. Of course,Apple might also decide to add their own mutability attributes to Clang later.
msg_send!
in anunsafe
block and just generate safefn
s.PhantomData<T>
inthe struct. To maintain optional dynamism, this would require a new trait (e.g.
AnyObject
) whichall objc objects implemented, and the
T
generic would require. With this in place,NSArray
couldbe declared as
struct NSArray<T: AnyObject = NSObject>(id, PhantomData<T>)
.std
. There is a clearrelation
NSCopying
andClone
, and everyNSObject
-derived class can implementstd::hash::Hash
courtesy of thehash
method, and eachNSObject
type can bePartialEq
with the
isEqual:
method.The text was updated successfully, but these errors were encountered: