Skip to content

Commit 107a87e

Browse files
authored
Merge pull request #1662 from godot-rust/mtnc/callback-names
Name virtual-callback layers after user-facing `I*` virtuals
2 parents e940567 + fec190a commit 107a87e

6 files changed

Lines changed: 108 additions & 88 deletions

File tree

godot-core/src/obj/traits.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,8 @@ where
830830
// ----------------------------------------------------------------------------------------------------------------------------------------------
831831

832832
/// Capability traits, providing dedicated functionalities for Godot classes
833+
///
834+
/// The `__godot_*` methods are named after the user-facing `I*` virtual; see [`crate::registry`] for the naming rule across all layers.
833835
pub mod cap {
834836
use std::any::Any;
835837

@@ -904,7 +906,7 @@ pub mod cap {
904906
#[doc(hidden)]
905907
pub trait GodotNotification: GodotClass {
906908
#[doc(hidden)]
907-
fn __godot_notification(&mut self, what: i32);
909+
fn __godot_on_notification(&mut self, what: i32);
908910
}
909911

910912
// TODO Evaluate whether we want this public or not
@@ -920,7 +922,7 @@ pub mod cap {
920922
type Recv: IntoVirtualMethodReceiver<Self>;
921923

922924
#[doc(hidden)]
923-
fn __godot_get_property(
925+
fn __godot_on_get(
924926
this: VirtualMethodReceiver<Self>,
925927
property: StringName,
926928
) -> Option<Variant>;
@@ -932,7 +934,7 @@ pub mod cap {
932934
type Recv: IntoVirtualMethodReceiver<Self>;
933935

934936
#[doc(hidden)]
935-
fn __godot_set_property(
937+
fn __godot_on_set(
936938
this: VirtualMethodReceiver<Self>,
937939
property: StringName,
938940
value: Variant,
@@ -945,7 +947,7 @@ pub mod cap {
945947
type Recv: IntoVirtualMethodReceiver<Self>;
946948

947949
#[doc(hidden)]
948-
fn __godot_get_property_list(
950+
fn __godot_on_get_property_list(
949951
this: VirtualMethodReceiver<Self>,
950952
) -> Vec<crate::registry::info::PropertyInfo>;
951953
}
@@ -956,7 +958,7 @@ pub mod cap {
956958
type Recv: IntoVirtualMethodReceiver<Self>;
957959

958960
#[doc(hidden)]
959-
fn __godot_property_get_revert(
961+
fn __godot_on_property_get_revert(
960962
this: VirtualMethodReceiver<Self>,
961963
property: StringName,
962964
) -> Option<Variant>;
@@ -968,7 +970,7 @@ pub mod cap {
968970
type Recv: IntoVirtualMethodReceiver<Self>;
969971

970972
#[doc(hidden)]
971-
fn __godot_validate_property(
973+
fn __godot_on_validate_property(
972974
this: VirtualMethodReceiver<Self>,
973975
property: &mut PropertyInfo,
974976
);

godot-core/src/registry/callbacks.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::storage::{InstanceStorage, Storage, StorageRefCounted, as_storage};
2727

2828
/// Invokes `code` -- a callback that calls into user code -- and catches any panic, so it does not unwind across the FFI boundary.
2929
///
30-
/// `method` names the callback in the error context, e.g. `"to_string"` is reported as `MyClass::to_string()`.
30+
/// `method` is the user-facing `I*` virtual name, not the Godot slot name -- `"on_get"` is reported as `MyClass::on_get()`.
3131
/// The caller decides how to degrade on `Err`, since each Godot callback has its own failure representation.
3232
fn handle_method_panic<T: GodotClass, R>(
3333
method: &str,
@@ -301,24 +301,24 @@ pub unsafe extern "C" fn to_string<T: cap::GodotToString>(
301301
}
302302

303303
#[expect(unsafe_op_in_unsafe_fn)] // Pointer validity asserted by Godot.
304-
pub unsafe extern "C" fn on_notification<T: cap::GodotNotification>(
304+
pub unsafe extern "C" fn notification<T: cap::GodotNotification>(
305305
instance: sys::GDExtensionClassInstancePtr,
306306
what: i32,
307307
_reversed: sys::GDExtensionBool,
308308
) {
309-
// `get_mut()` can also panic on borrow conflicts, in addition to `__godot_notification` itself.
309+
// `get_mut()` can also panic on borrow conflicts, in addition to `__godot_on_notification` itself.
310310
let code = || {
311311
let storage = as_storage::<T>(instance);
312312
let mut instance = storage.get_mut();
313313

314-
T::__godot_notification(&mut *instance, what);
314+
T::__godot_on_notification(&mut *instance, what);
315315
};
316316

317317
let _ = handle_method_panic::<T, _>("on_notification", code);
318318
}
319319

320320
#[expect(unsafe_op_in_unsafe_fn)] // Pointer validity asserted by Godot.
321-
pub unsafe extern "C" fn get_property<T: cap::GodotGet>(
321+
pub unsafe extern "C" fn get<T: cap::GodotGet>(
322322
instance: sys::GDExtensionClassInstancePtr,
323323
name: sys::GDExtensionConstStringNamePtr,
324324
ret: sys::GDExtensionVariantPtr,
@@ -328,7 +328,7 @@ pub unsafe extern "C" fn get_property<T: cap::GodotGet>(
328328
let instance = T::Recv::instance(storage);
329329
let property = StringName::new_from_string_sys(name);
330330

331-
match T::__godot_get_property(instance, property) {
331+
match T::__godot_on_get(instance, property) {
332332
Some(value) => {
333333
value.move_into_var_ptr(ret);
334334
true
@@ -337,11 +337,11 @@ pub unsafe extern "C" fn get_property<T: cap::GodotGet>(
337337
}
338338
};
339339

340-
handle_method_panic_bool::<T>("get_property", code)
340+
handle_method_panic_bool::<T>("on_get", code)
341341
}
342342

343343
#[expect(unsafe_op_in_unsafe_fn)] // Pointer validity asserted by Godot.
344-
pub unsafe extern "C" fn set_property<T: cap::GodotSet>(
344+
pub unsafe extern "C" fn set<T: cap::GodotSet>(
345345
instance: sys::GDExtensionClassInstancePtr,
346346
name: sys::GDExtensionConstStringNamePtr,
347347
value: sys::GDExtensionConstVariantPtr,
@@ -353,10 +353,10 @@ pub unsafe extern "C" fn set_property<T: cap::GodotSet>(
353353
let property = StringName::new_from_string_sys(name);
354354
let value = Variant::new_from_var_sys(value);
355355

356-
T::__godot_set_property(instance, property, value)
356+
T::__godot_on_set(instance, property, value)
357357
};
358358

359-
handle_method_panic_bool::<T>("set_property", code)
359+
handle_method_panic_bool::<T>("on_set", code)
360360
}
361361

362362
pub unsafe extern "C" fn reference<T: GodotClass>(instance: sys::GDExtensionClassInstancePtr) {
@@ -381,7 +381,7 @@ pub unsafe extern "C" fn get_property_list<T: cap::GodotGetPropertyList>(
381381
let storage = as_storage::<T>(instance);
382382
let instance = T::Recv::instance(storage);
383383

384-
let property_list = T::__godot_get_property_list(instance);
384+
let property_list = T::__godot_on_get_property_list(instance);
385385
let property_list_sys: Box<[sys::GDExtensionPropertyInfo]> = property_list
386386
.into_iter()
387387
.map(|prop| prop.into_owned_property_sys())
@@ -397,7 +397,7 @@ pub unsafe extern "C" fn get_property_list<T: cap::GodotGetPropertyList>(
397397
Box::leak(property_list_sys).as_mut_ptr().cast_const()
398398
};
399399

400-
handle_method_panic::<T, _>("get_property_list", code).unwrap_or_else(|_| {
400+
handle_method_panic::<T, _>("on_get_property_list", code).unwrap_or_else(|_| {
401401
// On panic, report an empty list -- `count` comes uninitialized, so it must be set in any case.
402402
*count = 0;
403403

@@ -453,7 +453,7 @@ unsafe fn raw_property_get_revert<T: cap::GodotPropertyGetRevert>(
453453
let instance = T::Recv::instance(storage);
454454

455455
let property = StringName::borrow_string_sys(property_name);
456-
T::__godot_property_get_revert(instance, property.clone())
456+
T::__godot_on_property_get_revert(instance, property.clone())
457457
}
458458

459459
/// # Safety
@@ -466,7 +466,7 @@ pub unsafe extern "C" fn property_can_revert<T: cap::GodotPropertyGetRevert>(
466466
) -> sys::GDExtensionBool {
467467
let code = || raw_property_get_revert::<T>(instance, property_name).is_some();
468468

469-
handle_method_panic_bool::<T>("property_can_revert", code)
469+
handle_method_panic_bool::<T>("on_property_get_revert", code)
470470
}
471471

472472
/// # Safety
@@ -487,7 +487,7 @@ pub unsafe extern "C" fn property_get_revert<T: cap::GodotPropertyGetRevert>(
487487
true
488488
};
489489

490-
handle_method_panic_bool::<T>("property_get_revert", code)
490+
handle_method_panic_bool::<T>("on_property_get_revert", code)
491491
}
492492

493493
/// Callback for `validate_property`.
@@ -509,14 +509,14 @@ pub unsafe extern "C" fn validate_property<T: cap::GodotValidateProperty>(
509509
let instance = T::Recv::instance(storage);
510510

511511
let mut property_info = PropertyInfo::new_from_sys(property_info_ptr);
512-
T::__godot_validate_property(instance, &mut property_info);
512+
T::__godot_on_validate_property(instance, &mut property_info);
513513

514514
// `property_info_ptr` remains valid and unchanged by the user callback.
515515
property_info.move_into_property_info_ptr(property_info_ptr);
516516
true
517517
};
518518

519-
handle_method_panic_bool::<T>("validate_property", code)
519+
handle_method_panic_bool::<T>("on_validate_property", code)
520520
}
521521

522522
// ----------------------------------------------------------------------------------------------------------------------------------------------

godot-core/src/registry/class.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pub(crate) fn register_class<
165165

166166
let godot_params = GodotCreationInfo {
167167
to_string_func: Some(callbacks::to_string::<T>),
168-
notification_func: Some(callbacks::on_notification::<T>),
168+
notification_func: Some(callbacks::notification::<T>),
169169
reference_func: Some(callbacks::reference::<T>),
170170
unreference_func: Some(callbacks::unreference::<T>),
171171
create_instance_func: Some(callbacks::create::<T>),
@@ -520,7 +520,7 @@ fn fill_class_info(item: ShardItem, c: &mut ClassRegistrationInfo) {
520520
user_create_fn,
521521
user_recreate_fn,
522522
user_to_string_fn,
523-
user_on_notification_fn,
523+
user_notification_fn,
524524
user_set_fn,
525525
user_get_fn,
526526
get_virtual_fn,
@@ -542,7 +542,7 @@ fn fill_class_info(item: ShardItem, c: &mut ClassRegistrationInfo) {
542542
.expect("duplicate: recreate_instance_func (i)");
543543

544544
c.godot_params.to_string_func = user_to_string_fn;
545-
c.godot_params.notification_func = user_on_notification_fn;
545+
c.godot_params.notification_func = user_notification_fn;
546546
c.godot_params.set_func = user_set_fn;
547547
c.godot_params.get_func = user_get_fn;
548548
c.godot_params.get_property_list_func = user_get_property_list_fn;

godot-core/src/registry/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
77

8+
//! Class registration: turns a user's `#[godot_api] impl I* for MyClass` into the callbacks Godot invokes.
9+
//!
10+
//! # Naming of lifecycle callbacks
11+
//! Layers godot-rust owns are named after the user-facing `I*` virtual; those 1:1 with GDExtension use Godot's slot names.
12+
//! Example `on_get`, from outermost to innermost:
13+
//!
14+
//! - `godot-macros`, `interface_trait_impl::TraitImpl::handle_on_get()` -- generates the code below.
15+
//! - `godot-macros`, `interface_trait_impl::Decls::on_get_impl` -- holds the generated `cap` trait impl.
16+
//! - [`shard::ITraitImpl::with_on_get()`] -- registers the callback; the sole point where the naming switches sides.
17+
//! - `callbacks::get()` -- the `extern "C" fn` filling `GDExtensionClassCreationInfo::get_func`, via field `ITraitImpl::user_get_fn`.
18+
//! - `handle_method_panic()`'s context, reported as `MyClass::on_get()` -- diagnostics, so user-facing again.
19+
//! - [`cap::GodotGet::__godot_on_get()`][crate::obj::cap::GodotGet] -- calls the user's `on_get()`.
20+
//!
21+
//! Both sides are needed since the mapping isn't 1:1: `on_property_get_revert` installs the `property_can_revert` and
22+
//! `property_get_revert` slots, `on_get_property_list` additionally `free_property_list`.
23+
824
// Note: final re-exports from godot-core are in lib.rs, mod register::private.
925
// These are public here for simplicity, but many are not imported by the main crate.
1026

godot-core/src/registry/shard.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ pub struct ITraitImpl {
362362
>,
363363

364364
/// User-defined `on_notification` function.
365-
pub(crate) user_on_notification_fn: Option<
365+
pub(crate) user_notification_fn: Option<
366366
unsafe extern "C" fn(
367367
p_instance: sys::GDExtensionClassInstancePtr,
368368
p_what: i32,
@@ -474,30 +474,27 @@ impl ITraitImpl {
474474
self
475475
}
476476

477-
pub fn with_string<T: GodotClass + cap::GodotToString>(mut self) -> Self {
477+
pub fn with_to_string<T: GodotClass + cap::GodotToString>(mut self) -> Self {
478478
set(&mut self.user_to_string_fn, callbacks::to_string::<T>);
479479
self
480480
}
481481

482482
pub fn with_on_notification<T: GodotClass + cap::GodotNotification>(mut self) -> Self {
483-
set(
484-
&mut self.user_on_notification_fn,
485-
callbacks::on_notification::<T>,
486-
);
483+
set(&mut self.user_notification_fn, callbacks::notification::<T>);
487484
self
488485
}
489486

490-
pub fn with_get_property<T: GodotClass + cap::GodotGet>(mut self) -> Self {
491-
set(&mut self.user_get_fn, callbacks::get_property::<T>);
487+
pub fn with_on_get<T: GodotClass + cap::GodotGet>(mut self) -> Self {
488+
set(&mut self.user_get_fn, callbacks::get::<T>);
492489
self
493490
}
494491

495-
pub fn with_set_property<T: GodotClass + cap::GodotSet>(mut self) -> Self {
496-
set(&mut self.user_set_fn, callbacks::set_property::<T>);
492+
pub fn with_on_set<T: GodotClass + cap::GodotSet>(mut self) -> Self {
493+
set(&mut self.user_set_fn, callbacks::set::<T>);
497494
self
498495
}
499496

500-
pub fn with_get_property_list<T: GodotClass + cap::GodotGetPropertyList>(mut self) -> Self {
497+
pub fn with_on_get_property_list<T: GodotClass + cap::GodotGetPropertyList>(mut self) -> Self {
501498
set(
502499
&mut self.user_get_property_list_fn,
503500
callbacks::get_property_list::<T>,
@@ -511,7 +508,9 @@ impl ITraitImpl {
511508
self
512509
}
513510

514-
pub fn with_property_get_revert<T: GodotClass + cap::GodotPropertyGetRevert>(mut self) -> Self {
511+
pub fn with_on_property_get_revert<T: GodotClass + cap::GodotPropertyGetRevert>(
512+
mut self,
513+
) -> Self {
515514
set(
516515
&mut self.user_property_get_revert_fn,
517516
callbacks::property_get_revert::<T>,
@@ -523,7 +522,7 @@ impl ITraitImpl {
523522
self
524523
}
525524

526-
pub fn with_validate_property<T: GodotClass + cap::GodotValidateProperty>(mut self) -> Self {
525+
pub fn with_on_validate_property<T: GodotClass + cap::GodotValidateProperty>(mut self) -> Self {
527526
set(
528527
&mut self.validate_property_fn,
529528
callbacks::validate_property::<T>,

0 commit comments

Comments
 (0)