-
-
Notifications
You must be signed in to change notification settings - Fork 225
Implement GString concatenation operator #1117
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
base: master
Are you sure you want to change the base?
Implement GString concatenation operator #1117
Conversation
Hi, and thanks for your contribution! 👍 I'm not sure the Instead of let a = GString::from(" is a ");
let b = GString::from("true");
let concat = &GString::from("12") + &a + &b + &GString::from(" number"); one can write let a = "is a";
let b = true;
let concat: GString = format!("12{a}{b} number").into(); Which has multiple advantages:
|
Hi @Bromeon, The verbosity of my example usage was mostly just due to to mirroring the logic originally employed by the In regard to performance, I do understand your concerns. My overall intention is to allow for a less lexically frictional experience for those adjusting to Rust from GDScript. Though operator overloading in this way may not be strictly so idiomatic for the avid Rust user, it does offer marginally greater parity with GDScript's syntax, which if that is not a primary priority for the GDExt project, then the closure of this pull request may indeed be appropriate. |
Please let me know if PierceLBrooks@7acaafc helps at all to minimize any of your performance concerns. |
Using Using a single
I understand, but in godot-rust we need to strike a balance between what's idiomatic in Rust and what's idiomatic in Godot. The The Potentially, we could also think about providing a format-style macro that internally calls let a = "is a";
let b = true;
let concat = godot_str!("12{a}{b} number"); What do you think about this? |
@PierceLBrooks would you be willing to change this PR to implement There is some prior art with the |
@Bromeon yes, I believe I will have enough free time to make a proper attempt at doing so before the weekend is over here. Thanks for the opportunity! |
Thanks to you! 😊 As mentioned, the already existing |
@PierceLBrooks have you had a chance to look into this yet? Let us know in case you get stuck somewhere! 🙂 |
@Bromeon , yes I have taken a look this week and will likely have conclusive progressive by the end of this weekend. The past week ended up being a little more chaotic than originally anticipated :P |
@Bromeon , the following commit hash reference should successfully address your suggestion... :) |
API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-1117 |
There was a problem hiding this 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 looks already great!
I added some comments; the import is currently wrong (which is why CI fails).
You can run ./check.sh
locally, which runs part of the test suite on your machine.
Would be great if you could squash the commits to a single one.
|
||
#[macro_export] | ||
macro_rules! godot_str { | ||
($fmt:literal $(, $args:expr)* $(,)?) => { | ||
$crate::global::str(&[ | ||
$crate::builtin::Variant::from( | ||
format!($fmt $(, $args)*) | ||
) | ||
]) | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to add this at the end of the file https://github.com/godot-rust/gdext/blob/master/godot-core/src/global/print.rs, since it's so similar to the others.
Could you also add a small documentation, maybe even with an doctest example that shows just 2-3 things being concatenated?
@@ -10,7 +10,7 @@ | |||
|
|||
use crate::framework::itest; | |||
|
|||
use godot::builtin::{GString, Variant}; | |||
use godot::{godot_str, builtin::{GString, Variant}}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The macro should be in godot::global
, not godot
, see
https://godot-rust.github.io/docs/gdext/master/godot/global/index.html#macros
This is likely automatically the case when you apply the previous suggestion of moving the godot_str!
declaration to print.rs
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Bromeon How's this? PierceLBrooks@2446cc8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really meant print.rs
. While the string is technically not "print", what matters much more is that closely related code is co-located. Having one function in its own file just makes it harder to discover and requires extra navigation if e.g. a refactor across all these macros happens.
So please keep it in the same file, it's absolutely fine if that is called print.rs
🙂
After updating the code (see comment), please run Once
Thanks a lot! |
416f477
to
f744dcc
Compare
@Bromeon , I believe the formatting, utility function consolidation migration, and squashing concerns have all now been addressed! :) Thank you for your patience. |
This will allow for users to add together their GString references with plus sign notation from within the Rust runtime of a gdextension, like so: