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

Add ability to rename functions bound to Godot #376

Closed

Conversation

you-win
Copy link
Contributor

@you-win you-win commented Aug 8, 2023

  • Modify func macro to accept a 'rename = ...' attribute
  • Add integration tests to cover renamed functions

Comments

Please let me know if it would be better to break this functionality out like with field_var.rs. I tried to keep the number of changed files to a minimum.

There are also TODOs left in the file as a starting point for discussion, but feel free to start picking at the PR wherever you want.

Still missing

  • signal renaming
  • docs
  • unit tests?

@Bromeon Bromeon added feature Adds functionality to the library c: register Register classes, functions and other symbols to GDScript labels Aug 9, 2023
@GodotRust
Copy link

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

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! This is a nice addition 🙂

I commented on a few things to simplify code flow. Some parts of it are related more to existing code that is now updated, rather than your code in particular.

Also thanks for the tests!

godot-macros/src/godot_api.rs Outdated Show resolved Hide resolved
Comment on lines 72 to 76
// TODO you-win (August 8, 2023): might be worth it to be pub(crate) so that method registration can reuse the struct?
struct GodotFn {
func: Function,
rename: Option<String>,
}
Copy link
Member

Choose a reason for hiding this comment

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

If that refactoring is possible (method registration reusing the struct), could you do it right away?

Maybe name the struct Func or FuncDefinition or so, rather than GodotFn.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have created a new FuncDefinition struct in method_registration/mod.rs

func: Function,
rename: Option<String>,
}
type Signal = Function;
Copy link
Member

@Bromeon Bromeon Aug 9, 2023

Choose a reason for hiding this comment

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

I'd probably keep this as a separate type, as the possible options for #[func] and #[signal] likely diverge over time. It also slightly enhances type safety.

Signal renames are not yet supported as far as I can see, so they should not have this field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it not worth it to rename the type for a bit more clarity in the method signature for?

fn process_godot_fns(decl: &mut Impl) -> Result<(Vec<FuncDefinition>, Vec<Signal>), Error>

vs

fn process_godot_fns(decl: &mut Impl) -> Result<(Vec<FuncDefinition>, Vec<Function>), Error>

godot-macros/src/godot_api.rs Outdated Show resolved Hide resolved
name if name == "func" => {
// TODO you-win (August 8, 2023): handle default values here as well?

// Safe unwrap since #[func] must be present if we got to this point
Copy link
Member

Choose a reason for hiding this comment

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

The code flow gets a bit weird (not your code, but the result of the existing one growing).

We do this at the moment:

for attribute in all_attributes {
   match attribute {
       "func" => KvParser::parse(all_attributes) // again searching through all
       "signal" => ...
   }
}

detect_duplicates(); // custom validation that's less expressive than KvParser's built-in one

Whereas KvParser is designed to already extract the relevant attributes and check for unhandled/duplicate/leftover ones in the finish() call. So you could do this:

if let Some(mut parser) = KvParser::parse(attributes, "func")? {
    // handle #[func]
    
    // eventually:
    parser.finish();
}

if let Some(_) = KvParser::parse(attributes, "signal")? {
    // handle #[signal]
}

Do you think this could be done?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is in progress, my latest WIP code does not work properly since it picks up other annotations first.

Currently, it's picking up #[allow(clippy::uninlined_format_args)], but I will continue to poke at it.

godot-macros/src/godot_api.rs Outdated Show resolved Hide resolved
* Modify func macro to accept a 'rename = ...' attribute
* Add integration tests to cover renamed functions
@you-win you-win force-pushed the feature/func-and-signal-renaming branch from e6480ef to be0ceee Compare August 10, 2023 02:55
…from a signature to resolve double immutable borrow
@you-win you-win closed this Aug 10, 2023
@you-win you-win deleted the feature/func-and-signal-renaming branch August 10, 2023 05:15
@you-win you-win restored the feature/func-and-signal-renaming branch August 10, 2023 05:16
@you-win you-win mentioned this pull request Aug 10, 2023
6 tasks
@Bromeon
Copy link
Member

Bromeon commented Aug 10, 2023

Replaced by #378.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: register Register classes, functions and other symbols to GDScript feature Adds functionality to the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants