Skip to content

Commit d96906f

Browse files
authored
Merge pull request #487 from godot-rust/qol/revert-get-prefixes
Reintroduce `get_` prefixes (revert #477)
2 parents 46f94f6 + 05ec030 commit d96906f

File tree

17 files changed

+86
-125
lines changed

17 files changed

+86
-125
lines changed

.github/workflows/update-docs.yml

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,25 @@ jobs:
5858
# Opened/reopened/updated PR: include PR author + title
5959
- name: "Construct JSON (for PR sync)"
6060
if: github.event_name == 'pull_request' && github.event.action != 'closed'
61-
# Escape double quotes in PR title, as it will be used in a JSON string.
61+
# jq escapes backticks, double quotes etc. in PR title.
6262
run: |
63-
escapedPrTitle=$(echo "${{ github.event.pull_request.title }}" | sed 's/"/\\"/g')
64-
payload=$(cat <<'HEREDOC'
65-
{
63+
payload=$(jq -n \
64+
--arg repoOwner '${{ github.repository_owner }}' \
65+
--arg num '${{ github.event.number }}' \
66+
--arg commitSha '${{ github.event.pull_request.head.sha }}' \
67+
--arg date '${{ github.event.pull_request.updated_at }}' \
68+
--arg prAuthor '${{ github.event.pull_request.user.login }}' \
69+
--arg prTitle '${{ github.event.pull_request.title }}' \
70+
'{
6671
"op": "put",
6772
"repo": "gdext",
68-
"repo-owner": "${{ github.repository_owner }}",
69-
"num": "${{ github.event.number }}",
70-
"commit-sha": "${{ github.event.pull_request.head.sha }}",
71-
"date": "${{ github.event.pull_request.updated_at }}",
72-
"pr-author": "${{ github.event.pull_request.user.login }}",
73-
"pr-title": "$escapedPrTitle"
74-
}
75-
HEREDOC)
73+
"repo-owner": $repoOwner,
74+
"num": $num,
75+
"commit-sha": $commitSha,
76+
"date": $date,
77+
"pr-author": $prAuthor,
78+
"pr-title": $prTitle
79+
}')
7680
echo "VAR=$payload"
7781
echo "GDEXT_JSON<<HEREDOC" >> $GITHUB_ENV
7882
echo "${payload}" >> $GITHUB_ENV

examples/dodge-the-creeps/rust/src/main_scene.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Main {
4545

4646
self.score = 0;
4747

48-
player.bind_mut().start(start_position.position());
48+
player.bind_mut().start(start_position.get_position());
4949
start_timer.start();
5050

5151
let mut hud = self.base.get_node_as::<Hud>("Hud");
@@ -84,9 +84,9 @@ impl Main {
8484
let progress = rng.gen_range(u32::MIN..u32::MAX);
8585

8686
mob_spawn_location.set_progress(progress as f32);
87-
mob_scene.set_position(mob_spawn_location.position());
87+
mob_scene.set_position(mob_spawn_location.get_position());
8888

89-
let mut direction = mob_spawn_location.rotation() + PI / 2.0;
89+
let mut direction = mob_spawn_location.get_rotation() + PI / 2.0;
9090
direction += rng.gen_range(-PI / 4.0..PI / 4.0);
9191

9292
mob_scene.set_rotation(direction);
@@ -101,7 +101,7 @@ impl Main {
101101
};
102102

103103
mob.set_linear_velocity(Vector2::new(range, 0.0));
104-
let lin_vel = mob.linear_velocity().rotated(real::from_f32(direction));
104+
let lin_vel = mob.get_linear_velocity().rotated(real::from_f32(direction));
105105
mob.set_linear_velocity(lin_vel);
106106

107107
let mut hud = self.base.get_node_as::<Hud>("Hud");

examples/dodge-the-creeps/rust/src/mob.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl IRigidBody2D for Mob {
4141
.get_node_as::<AnimatedSprite2D>("AnimatedSprite2D");
4242

4343
sprite.play();
44-
let anim_names = sprite.sprite_frames().unwrap().animation_names();
44+
let anim_names = sprite.get_sprite_frames().unwrap().get_animation_names();
4545

4646
// TODO use pick_random() once implemented
4747
let anim_names = anim_names.to_vec();

examples/dodge-the-creeps/rust/src/player.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl IArea2D for Player {
5252
}
5353

5454
fn ready(&mut self) {
55-
let viewport = self.base.viewport_rect();
55+
let viewport = self.base.get_viewport_rect();
5656
self.screen_size = viewport.size;
5757
self.base.hide();
5858
}
@@ -101,7 +101,7 @@ impl IArea2D for Player {
101101
}
102102

103103
let change = velocity * real::from_f64(delta);
104-
let position = self.base.global_position() + change;
104+
let position = self.base.get_global_position() + change;
105105
let position = Vector2::new(
106106
position.x.clamp(0.0, self.screen_size.x),
107107
position.y.clamp(0.0, self.screen_size.y),

godot-codegen/src/api_parser.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,6 @@ pub struct ClassMethod {
200200
pub arguments: Option<Vec<MethodArg>>,
201201
}
202202

203-
impl ClassMethod {
204-
pub fn map_args<R>(&self, f: impl FnOnce(&Vec<MethodArg>) -> R) -> R {
205-
match self.arguments.as_ref() {
206-
Some(args) => f(args),
207-
None => f(&vec![]),
208-
}
209-
}
210-
}
211-
212203
// Example: set_point_weight_scale ->
213204
// [ {name: "id", type: "int", meta: "int64"},
214205
// {name: "weight_scale", type: "float", meta: "float"},

godot-codegen/src/class_generator.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
99
use proc_macro2::{Ident, TokenStream};
1010
use quote::{format_ident, quote};
11-
use std::borrow::Cow;
1211
use std::path::Path;
1312

1413
use crate::api_parser::*;
@@ -1129,31 +1128,28 @@ fn make_class_method_definition(
11291128

11301129
let class_name_str = &class_name.godot_ty;
11311130
let godot_method_name = &method.name;
1132-
let renamed_method_name = special_cases::maybe_renamed(class_name, godot_method_name);
1131+
let rust_method_name = special_cases::maybe_renamed(class_name, godot_method_name);
11331132

1134-
let mut rust_method_name = Cow::Borrowed(renamed_method_name);
1133+
// Override const-qualification for known special cases (FileAccess::get_16, StreamPeer::get_u16, etc.).
1134+
/* TODO enable this once JSON/domain models are separated. Remove #[allow] above.
11351135
let mut override_is_const = None;
1136-
1137-
if method.map_args(|args| args.is_empty()) {
1138-
// Getters (i.e. 0 arguments) are stripped of their `get_` prefix, to conform to Rust convention.
1139-
// Currently also applies to static methods, but NOT to methods which have default parameters but can be called with 0 arguments.
1140-
// TODO(bromeon): should we add #[doc(alias)]?
1141-
if let Some(remainder) = renamed_method_name.strip_prefix("get_") {
1142-
// Do not apply for FileAccess::get_16, StreamPeer::get_u16, etc.
1143-
if !special_cases::keeps_get_prefix(class_name, method) {
1144-
rust_method_name = Cow::Owned(remainder.to_string());
1145-
1146-
// Many getters are mutably qualified (GltfAccessor::get_max, CameraAttributes::get_exposure_multiplier, ...).
1147-
override_is_const = Some(true);
1148-
}
1149-
}
1136+
if let Some(override_const) = special_cases::is_method_const(class_name, &method) {
1137+
override_is_const = Some(override_const);
11501138
}
11511139
1152-
let rust_method_name = rust_method_name.as_ref();
1140+
// Getters in particular are re-qualified as const (if there isn't already an override).
1141+
if override_is_const.is_none() && option_as_slice(&method.arguments).is_empty() {
1142+
if rust_method_name.starts_with("get_") {
1143+
// Many getters are mutably qualified (GltfAccessor::get_max, CameraAttributes::get_exposure_multiplier, ...).
1144+
// As a default, set those to const.
1145+
override_is_const = Some(true);
1146+
}
1147+
}*/
11531148

11541149
let receiver = make_receiver(
11551150
method.is_static,
1156-
override_is_const.unwrap_or(method.is_const),
1151+
//override_is_const.unwrap_or(method.is_const),
1152+
method.is_const,
11571153
quote! { self.object_ptr },
11581154
);
11591155

@@ -1912,8 +1908,8 @@ fn make_all_virtual_methods(
19121908
all_virtuals.extend(
19131909
get_methods_in_class(class)
19141910
.iter()
1915-
.cloned()
1916-
.filter(|m| m.is_virtual),
1911+
.filter(|m| m.is_virtual)
1912+
.cloned(),
19171913
);
19181914
};
19191915

godot-codegen/src/special_cases.rs

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -176,66 +176,36 @@ pub(crate) fn is_excluded_from_default_params(class_name: Option<&TyName>, godot
176176
}
177177
}
178178

179+
/// Return `true` if a method should have `&self` receiver in Rust, `false` if `&mut self` and `None` if original qualifier should be kept.
180+
///
181+
/// In cases where the method falls under some general category (like getters) that have their own const-qualification overrides, `Some`
182+
/// should be returned to take precedence over general rules. Example: `FileAccess::get_pascal_string()` is mut, but would be const-qualified
183+
/// since it looks like a getter.
179184
#[rustfmt::skip]
180-
pub(crate) fn keeps_get_prefix(class_name: &TyName, method: &ClassMethod) -> bool {
181-
// Also list those which have default parameters and can be called with 0 arguments. Those are anyway
182-
// excluded at the moment, but this is more robust if the outer logic changes.
183-
184-
match (class_name.godot_ty.as_str(), method.name.as_str()) {
185-
// For Object
186-
// https://docs.godotengine.org/en/stable/classes/class_object.html#methods
187-
| ("Object", "get_class")
188-
| ("Object", "get_instance_id") // currently removed, but would be shadowed by Gd::instance_id().
189-
| ("Object", "get_script")
190-
| ("Object", "get_script_instance")
191-
// The following ones often have slight variations with parameters, so it's more consistent to have get_signal_list() and
192-
// get_signal_connection_list(signal). This may change in the future.
193-
| ("Object", "get_incoming_connections")
194-
| ("Object", "get_meta_list")
195-
| ("Object", "get_method_list")
196-
| ("Object", "get_property_list")
197-
| ("Object", "get_signal_list")
198-
199-
// For Node
200-
// https://docs.godotengine.org/en/stable/classes/class_node.html#methods
201-
// TODO get_child_count?
202-
203-
// https://docs.godotengine.org/en/stable/classes/class_fileaccess.html#methods
185+
#[cfg(FALSE)] // TODO enable this once JSON/domain models are separated.
186+
pub(crate) fn is_method_const(class_name: &TyName, godot_method: &ClassMethod) -> Option<bool> {
187+
match (class_name.godot_ty.as_str(), godot_method.name.as_str()) {
188+
// Changed to mut.
204189
| ("FileAccess", "get_16")
205190
| ("FileAccess", "get_32")
206191
| ("FileAccess", "get_64")
207192
| ("FileAccess", "get_8")
208-
| ("FileAccess", "get_as_text")
209193
| ("FileAccess", "get_csv_line")
210-
| ("FileAccess", "get_double")
211-
| ("FileAccess", "get_error") // If this has side effects, should definitely keep prefix. Not clear.
212-
| ("FileAccess", "get_float")
213-
| ("FileAccess", "get_line")
214-
| ("FileAccess", "get_open_error")
215-
| ("FileAccess", "get_pascal_string")
216194
| ("FileAccess", "get_real")
195+
| ("FileAccess", "get_float")
196+
| ("FileAccess", "get_double")
217197
| ("FileAccess", "get_var")
218-
219-
// https://docs.godotengine.org/en/stable/classes/class_streampeer.html#methods
220-
// do for 8,16,32,64 and u*
198+
| ("FileAccess", "get_line")
199+
| ("FileAccess", "get_pascal_string") // already mut.
200+
| ("StreamPeer", "get_8")
221201
| ("StreamPeer", "get_16")
222202
| ("StreamPeer", "get_32")
223203
| ("StreamPeer", "get_64")
224-
| ("StreamPeer", "get_8")
225-
| ("StreamPeer", "get_double")
226204
| ("StreamPeer", "get_float")
227-
| ("StreamPeer", "get_string")
228-
| ("StreamPeer", "get_u16")
229-
| ("StreamPeer", "get_u32")
230-
| ("StreamPeer", "get_u64")
231-
| ("StreamPeer", "get_u8")
232-
| ("StreamPeer", "get_utf8_string")
233-
| ("StreamPeer", "get_var")
234-
235-
// Others that conflict with a verb:
236-
| ("AnimationPlayer", "get_queue")
205+
| ("StreamPeer", "get_double")
206+
=> Some(false),
237207

238-
=> true, _ => false,
208+
_ => None,
239209
}
240210
}
241211

godot-core/src/obj/traits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub trait Share {
106106
/// T: Inherits<Node>,
107107
/// {
108108
/// let up = node.upcast(); // type Gd<Node> inferred
109-
/// println!("Node #{} with name {}", up.instance_id(), up.name());
109+
/// println!("Node #{} with name {}", up.instance_id(), up.get_name());
110110
/// up.free();
111111
/// }
112112
///

itest/godot/SpecialTests.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func test_collision_object_2d_input_event():
3333
window.add_child(collision_object)
3434

3535
assert_that(not collision_object.input_event_called())
36-
assert_eq(collision_object.viewport(), null)
36+
assert_eq(collision_object.get_viewport(), null)
3737

3838
var event := InputEventMouseMotion.new()
3939
event.global_position = Vector2.ZERO
@@ -43,7 +43,7 @@ func test_collision_object_2d_input_event():
4343
await root.get_tree().physics_frame
4444

4545
assert_that(collision_object.input_event_called())
46-
assert_eq(collision_object.viewport(), window)
46+
assert_eq(collision_object.get_viewport(), window)
4747

4848
window.queue_free()
4949

itest/rust/src/builtin_tests/containers/array_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ fn typed_array_pass_to_godot_func() {
398398
let error = texture.create_from_images(images);
399399

400400
assert_eq!(error, Error::OK);
401-
assert_eq!((texture.width(), texture.height()), (2, 4));
401+
assert_eq!((texture.get_width(), texture.get_height()), (2, 4));
402402
}
403403

404404
#[itest]

0 commit comments

Comments
 (0)