Skip to content

to_gd(), Gd::try_cast() result, formatting #497

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

Merged
merged 8 commits into from
Nov 26, 2023
Merged

Conversation

Bromeon
Copy link
Member

@Bromeon Bromeon commented Nov 25, 2023

You can now do this:

use godot::prelude::*;

#[derive(GodotClass)]
struct MyClass {
    #[base]
    my_base: Base<RefCounted>,
}

impl MyClass {
    fn some_method(&self) {
        let outer_ptr: Gd<Self> = self.self_gd();
    }
}

A #[base] field must be present, otherwise self_gd (and its extension trait WithBaseField) is not provided.


Also, this changes Gd::try_cast<U> from returning Option<Gd<U>> to Result<Gd<U>, Gd<T>>.

In other words, if the cast fails, Err(self) is returned by value. This allows you to re-use the original object for other cast attempts (e.g. when checking against multiple derived classes in a chain).


Other changes:

  1. Callable::from_object_method() now takes its object parameter by shared-ref instead of value, no longer requiring the user to clone or submit ownership.
  2. Docs and consistency/formatting changes.
  3. Removes unused dependencies criterion and serde_json. The benchmark in godot-fmt can be manually re-enabled, but currently the dependencies slow down CI for no reason.
  4. Adjusts sh check.sh clippy to run --all-targets like CI.
  5. Removes remaining occurrences of bors. RIP 🪦

@Bromeon Bromeon added quality-of-life No new functionality, but improves ergonomics/internals c: core Core components labels Nov 25, 2023
@GodotRust
Copy link

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

@lilizoey
Copy link
Member

lilizoey commented Nov 25, 2023

i think to_gd might be a better name, self.to_gd() feels more apt and rusty than self.self_gd(). not to mention it's a bit repetitive to do self.self_gd().

also in a situation like this:

fn process(&mut self, delta: f64) {
  self.some_reference.self_gd()
}

it seems a bit less obvious to me that self_gd refers to the self of some_reference and not to the self that's in scope.

…cies although never used

Can be manually re-enabled. Unfortunately, Cargo doesn't offer optional dev-dependencies.

Also remove unused serde_json dependency.
@Bromeon Bromeon changed the title self_gd(), Gd::try_cast() result, formatting to_gd(), Gd::try_cast() result, formatting Nov 26, 2023
@Bromeon
Copy link
Member Author

Bromeon commented Nov 26, 2023

I was also thinking about outer_gd or similar to express that we get the enclosing/wrapping pointer.
But we can go with to_gd for now, might want to re-evaluate this once there are upcoming Base changes.

@Bromeon Bromeon enabled auto-merge November 26, 2023 10:43
@Bromeon Bromeon added this pull request to the merge queue Nov 26, 2023
Merged via the queue into master with commit d3745f8 Nov 26, 2023
@Bromeon Bromeon deleted the qol/docs-and-utils branch November 26, 2023 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: core Core components quality-of-life No new functionality, but improves ergonomics/internals
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants