-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
Implement dynTrait for Gd<T: TraitA> #930
base: master
Are you sure you want to change the base?
Conversation
API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-930 |
Thanks a lot! I'll review this after v0.2 release and once I've had a chance to drive my own implementation of this pattern a bit further -- would be cool to compare the two afterwards, probably there are some trade-offs in each! 🙂 As such, don't spend too much time on docs/tests/refinement just yet. |
No problem – I'm going to use it on my own, so it shouldn't lag behind master too much. |
9d7f14b
to
d292853
Compare
- Explicitly disallow using anything but associated functions for dynTrait objects.
- Add minimalistic docs
06f88fa
to
43aa173
Compare
So, I finally found some time to wrap up my own approach in #953, took a few iterations. I haven't had a chance to look deeply into this PR's implementation yet, but from a glance at your examples, it seems our approaches have some different goals: This PR
My PR #953
Please correct if something is inaccurate! From your comment
I believe that |
closes: #631
Adds an ability to cast
Gd<T: Trait>
to dyn Traitobject.It is based on dynamic dispatch. Long story short, we create set of closures for every dynTrait Class which allow us to cast
Gd<TraitBase>
toGd<dynTraitClass>
, bind it and finally use given binded smart pointer gd as a dyn TraitObject.I'm not sure if it is the best possible approach, albeit it is much better than anything else I've tried so far (this being – inheritance with custom-godot-api, hacky "codegen" (same as previous but with more errors), some unsafe magic with enum dispatch and just-let-godot-runtime-handle-it-all). Creating proper dynTraits might be a bit wonky due to Rust Object Safety.
Ergonomics wise it is… pretty good. I've been using it for some time (two months) and it handles all of my usecases (throwing gdext-rust-declared commands Objects from gdscript to rust dispatcher) without much issue.
I still have to do docs (ETA: 4 days) and tests (ETA: after initial review); Outside of that I would change declaration from
to
let me know if it is alright.
Parts of the macro that check if given trait can be used to create trait objects are a bit wonky – I would gladly apply any suggestions. Same goes for checking if given method is marked as non-dispatchable.
Generated signatures in internal godot docs properly point to dynTraits Base object – it would be cool if we would be able to somehow specify groups or traits for exports and docs in the future.
I have no idea how to export dynTraitObjects with Arrays<…> and whatnot. It would be good idea to explore this topic in the future.
Demo:
Output (after calling it in a scene with
n
brothers):