-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
Multiple #[godot_api] impl blocks #927
base: master
Are you sure you want to change the base?
Conversation
The idea is that the "primary" impl block defines a The "secondary" impl blocks then just push into these Here is the generated code, for completeness sake: use godot::prelude::*;
use godot::classes::Node;
use godot::classes::INode;
#[derive(GodotClass)]
#[class(base=Node, init)]
pub struct TestNode {
base: Base<Node>
}
// Recursive expansion of godot_api macro
// =======================================
impl TestNode {
fn foo() {
godot_print!("[TestNode] called 'foo'");
}
}
#[used]
#[allow(non_upper_case_globals)]
#[doc(hidden)]
static __registration_methods_TestNode: std::sync::Mutex<Vec<fn()>> =
std::sync::Mutex::new(Vec::new());
#[used]
#[allow(non_upper_case_globals)]
#[doc(hidden)]
static __registration_constants_TestNode: std::sync::Mutex<Vec<fn()>> =
std::sync::Mutex::new(Vec::new());
impl ::godot::obj::cap::ImplementsGodotApi for TestNode {
fn __register_methods() {
let guard = __registration_methods_TestNode.lock().unwrap();
for f in guard.iter() {
f();
}
}
fn __register_constants() {
let guard = __registration_constants_TestNode.lock().unwrap();
for f in guard.iter() {
f();
}
}
}
const _: () = {
#[allow(non_upper_case_globals)]
#[used]
#[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
#[cfg_attr(target_os = "ios", link_section = "__DATA,__mod_init_func")]
#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
#[cfg_attr(target_os = "android", link_section = ".init_array")]
#[cfg_attr(target_os = "dragonfly", link_section = ".init_array")]
#[cfg_attr(target_os = "freebsd", link_section = ".init_array")]
#[cfg_attr(target_os = "linux", link_section = ".init_array")]
#[cfg_attr(target_os = "netbsd", link_section = ".init_array")]
#[cfg_attr(target_os = "openbsd", link_section = ".init_array")]
static __init: extern "C" fn() = {
#[cfg_attr(target_os = "android", link_section = ".text.startup")]
#[cfg_attr(target_os = "linux", link_section = ".text.startup")]
extern "C" fn __inner_init() {
{
__registration_methods_TestNode.lock().unwrap().push(|| {
{
use ::godot::builtin::{StringName, Variant};
use ::godot::obj::GodotClass;
use ::godot::register::private::method::ClassMethodInfo;
use ::godot::sys;
type Sig = ((),);
let method_name = StringName::from("foo");
unsafe extern "C" fn varcall_fn(
_method_data: *mut std::ffi::c_void,
instance_ptr: sys::GDExtensionClassInstancePtr,
args_ptr: *const sys::GDExtensionConstVariantPtr,
arg_count: sys::GDExtensionInt,
ret: sys::GDExtensionVariantPtr,
err: *mut sys::GDExtensionCallError,
) {
let call_ctx = ::godot::meta::CallContext::func("TestNode", "foo");
::godot::private::handle_varcall_panic(&call_ctx, &mut *err, || {
<Sig as ::godot::meta::VarcallSignatureTuple>::in_varcall(
instance_ptr,
&call_ctx,
args_ptr,
arg_count,
ret,
err,
|_, params| {
let () = params;
TestNode::foo()
},
)
});
};
unsafe extern "C" fn ptrcall_fn(
_method_data: *mut std::ffi::c_void,
instance_ptr: sys::GDExtensionClassInstancePtr,
args_ptr: *const sys::GDExtensionConstTypePtr,
ret: sys::GDExtensionTypePtr,
) {
let call_ctx = ::godot::meta::CallContext::func("TestNode", "foo");
let _success = ::godot::private::handle_panic(
|| &call_ctx,
|| {
<Sig as ::godot::meta::PtrcallSignatureTuple>::in_ptrcall(
instance_ptr,
&call_ctx,
args_ptr,
ret,
|_, params| {
let () = params;
TestNode::foo()
},
sys::PtrcallType::Standard,
)
},
);
};
let method_info = unsafe {
ClassMethodInfo::from_signature::<TestNode, Sig>(
method_name,
Some(varcall_fn),
Some(ptrcall_fn),
::godot::global::MethodFlags::NORMAL
| ::godot::global::MethodFlags::STATIC,
&[],
)
};
{
if false {
format_args!(" Register fn: {}::{}", "TestNode", "foo");
}
};
method_info.register_extension_class_method();
};
});
__registration_constants_TestNode
.lock()
.unwrap()
.push(|| {});
}
}
__inner_init
};
#[cfg(target_family = "wasm")]
{
core::panicking::panic_fmt(core::const_format_args!(
"not yet implemented: {}",
core::format_args!("wasm not yet implemented")
));
}
};
const _: () = {
#[allow(non_upper_case_globals)]
#[used]
#[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
#[cfg_attr(target_os = "ios", link_section = "__DATA,__mod_init_func")]
#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
#[cfg_attr(target_os = "android", link_section = ".init_array")]
#[cfg_attr(target_os = "dragonfly", link_section = ".init_array")]
#[cfg_attr(target_os = "freebsd", link_section = ".init_array")]
#[cfg_attr(target_os = "linux", link_section = ".init_array")]
#[cfg_attr(target_os = "netbsd", link_section = ".init_array")]
#[cfg_attr(target_os = "openbsd", link_section = ".init_array")]
static __init: extern "C" fn() = {
#[cfg_attr(target_os = "android", link_section = ".text.startup")]
#[cfg_attr(target_os = "linux", link_section = ".text.startup")]
extern "C" fn __inner_init() {
let mut guard = ::godot::private::__godot_rust_plugin___GODOT_PLUGIN_REGISTRY
.lock()
.unwrap();
guard.push(
(::godot::private::ClassPlugin {
class_name: <TestNode as ::godot::obj::GodotClass>::class_name(),
item: ::godot::private::PluginItem::InherentImpl(
::godot::private::InherentImpl {
register_methods_constants_fn: ::godot::private::ErasedRegisterFn {
raw: ::godot::private::callbacks::register_user_methods_constants::<
TestNode,
>,
},
register_rpcs_fn: Some(::godot::private::ErasedRegisterRpcsFn {
raw: ::godot::private::callbacks::register_user_rpcs::<TestNode>,
}),
},
),
init_level: <TestNode as ::godot::obj::GodotClass>::INIT_LEVEL,
}),
);
}
__inner_init
};
#[cfg(target_family = "wasm")]
godot_ffi::gensym! {
godot_ffi::plugin_add_inner_wasm!()
}
};
// Recursive expansion of godot_api macro
// =======================================
impl TestNode {
fn bar() {
godot_print!("[TestNode] called 'bar'");
}
}
const _: () = {
#[allow(non_upper_case_globals)]
#[used]
#[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
#[cfg_attr(target_os = "ios", link_section = "__DATA,__mod_init_func")]
#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
#[cfg_attr(target_os = "android", link_section = ".init_array")]
#[cfg_attr(target_os = "dragonfly", link_section = ".init_array")]
#[cfg_attr(target_os = "freebsd", link_section = ".init_array")]
#[cfg_attr(target_os = "linux", link_section = ".init_array")]
#[cfg_attr(target_os = "netbsd", link_section = ".init_array")]
#[cfg_attr(target_os = "openbsd", link_section = ".init_array")]
static __init: extern "C" fn() = {
#[cfg_attr(target_os = "android", link_section = ".text.startup")]
#[cfg_attr(target_os = "linux", link_section = ".text.startup")]
extern "C" fn __inner_init() {
{
__registration_methods_TestNode.lock().unwrap().push(|| {
{
use ::godot::builtin::{StringName, Variant};
use ::godot::obj::GodotClass;
use ::godot::register::private::method::ClassMethodInfo;
use ::godot::sys;
type Sig = ((),);
let method_name = StringName::from("bar");
unsafe extern "C" fn varcall_fn(
_method_data: *mut std::ffi::c_void,
instance_ptr: sys::GDExtensionClassInstancePtr,
args_ptr: *const sys::GDExtensionConstVariantPtr,
arg_count: sys::GDExtensionInt,
ret: sys::GDExtensionVariantPtr,
err: *mut sys::GDExtensionCallError,
) {
let call_ctx = ::godot::meta::CallContext::func("TestNode", "bar");
::godot::private::handle_varcall_panic(&call_ctx, &mut *err, || {
<Sig as ::godot::meta::VarcallSignatureTuple>::in_varcall(
instance_ptr,
&call_ctx,
args_ptr,
arg_count,
ret,
err,
|_, params| {
let () = params;
TestNode::bar()
},
)
});
};
unsafe extern "C" fn ptrcall_fn(
_method_data: *mut std::ffi::c_void,
instance_ptr: sys::GDExtensionClassInstancePtr,
args_ptr: *const sys::GDExtensionConstTypePtr,
ret: sys::GDExtensionTypePtr,
) {
let call_ctx = ::godot::meta::CallContext::func("TestNode", "bar");
let _success = ::godot::private::handle_panic(
|| &call_ctx,
|| {
<Sig as ::godot::meta::PtrcallSignatureTuple>::in_ptrcall(
instance_ptr,
&call_ctx,
args_ptr,
ret,
|_, params| {
let () = params;
TestNode::bar()
},
sys::PtrcallType::Standard,
)
},
);
};
let method_info = unsafe {
ClassMethodInfo::from_signature::<TestNode, Sig>(
method_name,
Some(varcall_fn),
Some(ptrcall_fn),
::godot::global::MethodFlags::NORMAL
| ::godot::global::MethodFlags::STATIC,
&[],
)
};
{
if false {
format_args!(" Register fn: {}::{}", "TestNode", "bar");
}
};
method_info.register_extension_class_method();
};
});
__registration_constants_TestNode
.lock()
.unwrap()
.push(|| {});
}
}
__inner_init
};
#[cfg(target_family = "wasm")]
{
core::panicking::panic_fmt(core::const_format_args!(
"not yet implemented: {}",
core::format_args!("wasm not yet implemented")
));
}
};
|
It should be possible to get rid of the explicit |
One limitation currently is that you can have multiple impl blocks, but only in the same file. The |
API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-927 |
Wow, impressive 😮 thanks a lot!
Let's avoid that, stateful macros are a hack that may cease to work in the future, or with sandboxed macros (c.f. experimental projects like https://github.com/dtolnay/watt). It's a very minor QoL impediment to write "secondary", I don't see it as an issue in practice. Are multiple
Imo it's fine to have a first version with this limitation, and look into this feature in a separate PR. That way, the amount of changes to review and check isn't too big 🙂 |
yes, unlimited, unless there's a limit w.r.t. the linker etc |
d5c73e6
to
6adbdc3
Compare
2006f8a
to
a0df6ed
Compare
have the key 'secondary'
a0df6ed
to
2a5660f
Compare
Thanks! I'm a bit busy with preparing v0.2 release, so review may have to wait a bit, but I haven't forgotten! 🙂 |
fixes #925
It errors with acceptable error messages and spans when unsupported keys are added to the attribute: