Skip to content
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

Make mutable Script Instance functions reentrant #671

Merged

Conversation

TitanNano
Copy link
Contributor

@TitanNano TitanNano commented Apr 21, 2024

Closes #554

Adds a ref guard around a &mut T where T: ScriptInstance for the ScriptInstance trait. This guard exposes an API to access the script instance base object, through which reentrant calls can be achieved.

This also switches the script instance internals from a RefCell to a GdCell and introduces a new WithBase trait that can be shared between GodotClass and ScriptInstance.


I'm open to factor the WithBase introduction into a seperate PR if that is preferred.

@GodotRust
Copy link

API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-671

Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the multiple iterations on this design! This looks quite clean for the user side 🧹 I'll already give some feedback, but will need to look at this deeper next time.

Not yet sure on the name SiMut. It somehow makes sense if you look at GdMut, however is it really equivalent? The former is never explicitly passed to user functions, it's always &mut T.

Please avoid links to issues/PRs inside commit messages, as they create new backreferences in the mentioned issue on every iteration and clutter the conversation. Just mention "Closes #554" once in the PR description (not its title).

godot-codegen/src/generator/classes.rs Outdated Show resolved Hide resolved
godot-core/src/engine/script_instance.rs Show resolved Hide resolved
godot-core/src/engine/script_instance.rs Outdated Show resolved Hide resolved
godot-core/src/engine/script_instance.rs Outdated Show resolved Hide resolved
godot-core/src/obj/base.rs Show resolved Hide resolved
godot-core/src/engine/script_instance.rs Outdated Show resolved Hide resolved
@Bromeon Bromeon force-pushed the jovan/script_instance_mut_wrapper branch from ad99845 to 41b6cb9 Compare April 22, 2024 20:03
@Bromeon
Copy link
Member

Bromeon commented Apr 22, 2024

Rebased to fix #669, CI still red due to changes in this PR though.

@TitanNano TitanNano force-pushed the jovan/script_instance_mut_wrapper branch from 41b6cb9 to dd2f0a1 Compare April 23, 2024 20:23
@TitanNano TitanNano changed the title Make mutable Script Instance functions reentrant #554 Make mutable Script Instance functions reentrant Apr 23, 2024
@TitanNano TitanNano force-pushed the jovan/script_instance_mut_wrapper branch from dd2f0a1 to 898c5a5 Compare April 23, 2024 21:12
@Bromeon
Copy link
Member

Bromeon commented Apr 28, 2024

After some discussion with @lilizoey, we were thinking it would be nice to avoid introducing a new WithBase trait. While it addresses some duplication, it also adds a new symbol for everyone that was previously just dealing with GodotClass, as well as an extra indirection.

Instead, we'd propose separate structs:

// Existing:
struct BaseRef<'a, T: GodotClass> { ... }
struct BaseMut<'a, T: GodotClass> { ... }

// New (added by this PR):
struct ScriptBaseRef<'a, T: ScriptInstance> { ... }
struct ScriptBaseMut<'a, T: ScriptInstance> { ... }

We were also thinking about type aliases, but they have the problem to be leaky abstractions (i.e. error messages still contain the raw types, you cannot specify bounds on aliases, etc). So distinct types are better, since it's very unlikely that the user wants to abstract over both. Distinct types also make the relations BaseRef -> GodotClass and ScriptBaseRef -> ScriptInstance very explicit, which is a plus for understandability.

To avoid the duplication, you could use a declarative macro which substitutes some variables ($Ref:ty, $Mut:ty, $Bound:ty). It's not the most beautiful, but does the job and we do this in several places 😎


Also, create_script_instance would need to be turned into an unsafe function as per discussion 🙂

@TitanNano
Copy link
Contributor Author

@Bromeon thanks for this feedback. I will see that I incorporate it into the PR.

@TitanNano TitanNano force-pushed the jovan/script_instance_mut_wrapper branch 4 times, most recently from 582f56a to 6b3c3c9 Compare May 3, 2024 22:49
@TitanNano
Copy link
Contributor Author

TitanNano commented May 3, 2024

@Bromeon I updated the BaseRef handling and added documentation for SiMut. createcreate_script_instanceis now also unsafe. Why the Clippy job is failing is unclear to me, I can't reproduce it locally.

@TitanNano TitanNano force-pushed the jovan/script_instance_mut_wrapper branch from e9185cc to f5c1fdd Compare May 4, 2024 19:21
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the update!

godot-core/src/engine/script_instance.rs Outdated Show resolved Hide resolved
godot-core/src/engine/script_instance.rs Outdated Show resolved Hide resolved
godot-core/src/engine/script_instance.rs Outdated Show resolved Hide resolved
godot-core/src/engine/script_instance.rs Outdated Show resolved Hide resolved
godot-core/src/obj/guards.rs Outdated Show resolved Hide resolved
godot-core/src/obj/guards.rs Outdated Show resolved Hide resolved
@TitanNano TitanNano force-pushed the jovan/script_instance_mut_wrapper branch from f5c1fdd to 674c823 Compare May 8, 2024 19:35
@TitanNano TitanNano requested a review from Bromeon May 8, 2024 19:41
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! From my side only minor things left.

Here's btw the direct link to SiMut docs from this PR:
https://godot-rust.github.io/docs/gdext/pr-671/godot/engine/struct.SiMut.html

godot-core/src/engine/script_instance.rs Outdated Show resolved Hide resolved
godot-core/src/engine/script_instance.rs Show resolved Hide resolved
godot-core/src/engine/script_instance.rs Outdated Show resolved Hide resolved
godot-core/src/engine/script_instance.rs Outdated Show resolved Hide resolved
@TitanNano TitanNano force-pushed the jovan/script_instance_mut_wrapper branch 2 times, most recently from b9244fd to 4a55fc4 Compare May 10, 2024 20:54
godot-core/src/engine/script_instance.rs Outdated Show resolved Hide resolved
godot-core/src/engine/script_instance.rs Outdated Show resolved Hide resolved
@TitanNano TitanNano force-pushed the jovan/script_instance_mut_wrapper branch from 4a55fc4 to e0b81d1 Compare May 11, 2024 21:50
@TitanNano TitanNano requested a review from Bromeon May 12, 2024 09:10
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for yet another great pull request that enhances ScriptInstance functionality! 👍

godot-core/src/engine/script_instance.rs Show resolved Hide resolved
@Bromeon Bromeon added this pull request to the merge queue May 12, 2024
Merged via the queue into godot-rust:master with commit 22fd33d May 12, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make ScriptInstance.call and company re-entrant
4 participants