Skip to content

Commit 8975297

Browse files
lilizoeyBromeon
authored andcommitted
Merge {Varcall/Ptrcall}SignatureTuple -> Signature<Params, Ret>
Split parameter behavior into a set of new traits: `ParamList`, `InParamList` and `OutParamList`. Allows treating input/output parameters separately.
1 parent b0be794 commit 8975297

File tree

19 files changed

+734
-640
lines changed

19 files changed

+734
-640
lines changed

godot-codegen/src/generator/builtins.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ fn make_builtin_method_definition(
253253
let ptrcall_invocation = quote! {
254254
let method_bind = sys::builtin_method_table().#fptr_access;
255255

256-
<CallSig as PtrcallSignatureTuple>::out_builtin_ptrcall(
256+
257+
Signature::<CallParams, CallRet>::out_builtin_ptrcall(
257258
method_bind,
258259
#builtin_name_str,
259260
#method_name_str,
@@ -265,7 +266,7 @@ fn make_builtin_method_definition(
265266
let varcall_invocation = quote! {
266267
let method_bind = sys::builtin_method_table().#fptr_access;
267268

268-
<CallSig as VarcallSignatureTuple>::out_builtin_ptrcall_varargs(
269+
Signature::<CallParams, CallRet>::out_builtin_ptrcall_varargs(
269270
method_bind,
270271
#builtin_name_str,
271272
#method_name_str,

godot-codegen/src/generator/classes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ fn make_class_method_definition(
515515
let ptrcall_invocation = quote! {
516516
let method_bind = sys::#get_method_table().#fptr_access;
517517

518-
<CallSig as PtrcallSignatureTuple>::out_class_ptrcall(
518+
Signature::<CallParams, CallRet>::out_class_ptrcall(
519519
method_bind,
520520
#rust_class_name,
521521
#rust_method_name,
@@ -528,7 +528,7 @@ fn make_class_method_definition(
528528
let varcall_invocation = quote! {
529529
let method_bind = sys::#get_method_table().#fptr_access;
530530

531-
<CallSig as VarcallSignatureTuple>::out_class_varcall(
531+
Signature::<CallParams, CallRet>::out_class_varcall(
532532
method_bind,
533533
#rust_class_name,
534534
#rust_method_name,

godot-codegen/src/generator/functions_common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ pub fn make_function_definition(
177177
let call_sig_decl = {
178178
let return_ty = &sig.return_value().type_tokens();
179179

180-
// Build <'a0, 'a1, ...> for lifetimes.
181180
quote! {
182-
type CallSig #callsig_lifetime_args = ( #return_ty, #(#param_types),* );
181+
type CallRet = #return_ty;
182+
type CallParams #callsig_lifetime_args = (#(#param_types,)*);
183183
}
184184
};
185185

godot-codegen/src/generator/utility_functions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub(crate) fn make_utility_function_definition(function: &UtilityFunction) -> To
4646
let ptrcall_invocation = quote! {
4747
let utility_fn = sys::utility_function_table().#function_ident;
4848

49-
<CallSig as PtrcallSignatureTuple>::out_utility_ptrcall(
49+
Signature::<CallParams, CallRet>::out_utility_ptrcall(
5050
utility_fn,
5151
#function_name_str,
5252
args
@@ -56,7 +56,7 @@ pub(crate) fn make_utility_function_definition(function: &UtilityFunction) -> To
5656
let varcall_invocation = quote! {
5757
let utility_fn = sys::utility_function_table().#function_ident;
5858

59-
<CallSig as VarcallSignatureTuple>::out_utility_ptrcall_varargs(
59+
Signature::<CallParams, CallRet>::out_utility_ptrcall_varargs(
6060
utility_fn,
6161
#function_name_str,
6262
args,

godot-codegen/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn make_imports() -> TokenStream {
2525
quote! {
2626
use godot_ffi as sys;
2727
use crate::builtin::*;
28-
use crate::meta::{AsArg, AsObjectArg, ClassName, CowArg, ObjectArg, ObjectCow, PtrcallSignatureTuple, RefArg, VarcallSignatureTuple};
28+
use crate::meta::{AsArg, AsObjectArg, ClassName, CowArg, InParamTuple, ObjectArg, ObjectCow, OutParamTuple, ParamTuple, RefArg, Signature};
2929
use crate::classes::native::*;
3030
use crate::classes::Object;
3131
use crate::obj::Gd;

godot-core/src/meta/godot_convert/mod.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,6 @@ pub trait FromGodot: Sized + GodotConvert {
120120
}
121121
}
122122

123-
pub(crate) fn into_ffi_variant<T: ToGodot>(value: &T) -> Variant {
124-
let via = value.to_godot();
125-
let ffi = via.to_ffi();
126-
GodotFfiVariant::ffi_to_variant(&ffi)
127-
}
128-
129-
pub(crate) fn try_from_ffi<T: FromGodot>(
130-
ffi: <T::Via as GodotType>::Ffi,
131-
) -> Result<T, ConvertError> {
132-
let via = <T::Via as GodotType>::try_from_ffi(ffi)?;
133-
T::try_from_godot(via)
134-
}
135-
136123
#[macro_export]
137124
macro_rules! impl_godot_as_self {
138125
($T:ty) => {

godot-core/src/meta/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ mod array_type_info;
4848
mod class_name;
4949
mod godot_convert;
5050
mod method_info;
51+
mod param_tuple;
5152
mod property_info;
5253
mod signature;
5354
mod traits;
@@ -61,8 +62,7 @@ pub use class_name::ClassName;
6162
pub use godot_convert::{FromGodot, GodotConvert, ToGodot};
6263
pub use traits::{ArrayElement, GodotType, PackedArrayElement};
6364

64-
#[cfg(since_api = "4.2")]
65-
pub use crate::registry::signal::variadic::ParamTuple;
65+
pub use param_tuple::{InParamTuple, OutParamTuple, ParamTuple};
6666

6767
pub(crate) use array_type_info::ArrayTypeInfo;
6868
pub(crate) use traits::{

godot-core/src/meta/param_tuple.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) godot-rust; Bromeon and contributors.
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+
*/
7+
8+
use crate::builtin::Variant;
9+
10+
use super::{CallContext, CallResult, PropertyInfo};
11+
use godot_ffi as sys;
12+
13+
mod impls;
14+
15+
/// Represents a parameter list as Rust tuple where each tuple element is one parameter.
16+
///
17+
/// This trait only contains metadata for the parameter list, the actual functionality is contained in [`InParamTuple`] and
18+
/// [`OutParamTuple`].
19+
pub trait ParamTuple: Sized {
20+
/// The number of elements in this parameter list.
21+
const LEN: usize;
22+
23+
/// The param info of the parameter at index `index`.
24+
#[doc(hidden)]
25+
fn param_info(
26+
index: usize,
27+
param_name: &str,
28+
) -> Option<crate::registry::method::MethodParamOrReturnInfo>;
29+
30+
/// The property info of the parameter at index `index`.
31+
fn property_info(index: usize, param_name: &str) -> Option<PropertyInfo> {
32+
Self::param_info(index, param_name).map(|param| param.info)
33+
}
34+
35+
/// Return a string representing the arguments.
36+
fn format_args(&self) -> String;
37+
}
38+
39+
/// Represents a parameter list that is received from some external location (usually Godot).
40+
///
41+
/// As an example, this would be used for user-defined functions that will be called from Godot, however this is _not_ used when
42+
/// calling a Godot function from Rust code.
43+
pub trait InParamTuple: ParamTuple {
44+
/// Converts `args_ptr` to `Self` by first going through [`Variant`].
45+
///
46+
/// # Safety
47+
///
48+
/// - `args_ptr` must be a pointer to an array of length [`Self::LEN`](ParamTuple::LEN)
49+
/// - Each element of `args_ptr` must be reborrowable as a `&Variant` with a lifetime that lasts for the duration of the call.
50+
unsafe fn from_varcall_args(
51+
args_ptr: *const sys::GDExtensionConstVariantPtr,
52+
call_ctx: &CallContext,
53+
) -> CallResult<Self>;
54+
55+
/// Converts `args_ptr` to `Self` directly.
56+
///
57+
/// # Safety
58+
///
59+
/// - `args_ptr` must be a pointer to a valid array of length [`Self::LEN`](ParamTuple::LEN)
60+
/// - each element of `args_ptr` must be of the same type as each element of `Self`
61+
unsafe fn from_ptrcall_args(
62+
args_ptr: *const sys::GDExtensionConstTypePtr,
63+
call_type: sys::PtrcallType,
64+
call_ctx: &CallContext,
65+
) -> Self;
66+
67+
/// Converts `array` to `Self` by calling [`from_variant`](crate::meta::FromGodot::from_variant) on each argument.
68+
fn from_variant_array(array: &[&Variant]) -> Self;
69+
}
70+
71+
/// Represents a parameter list that is used to call some external code.
72+
///
73+
/// As an example, this would be used to call Godot functions through FFI, however this is _not_ used when Godot calls a user-defined
74+
/// function.
75+
pub trait OutParamTuple: ParamTuple {
76+
/// Call `f` on the tuple `self` by first converting `self` to an array of [`Variant`]s.
77+
fn with_variants<F, R>(self, f: F) -> R
78+
where
79+
F: FnOnce(&[Variant]) -> R;
80+
81+
/// Call `f` on the tuple `self` by first converting `self` to an array of [`Variant`] pointers.
82+
fn with_variant_pointers<F, R>(self, f: F) -> R
83+
where
84+
F: FnOnce(&[sys::GDExtensionConstVariantPtr]) -> R;
85+
86+
/// Call `f` on the tuple `self` by first converting `self` to an array of Godot type pointers.
87+
fn with_type_pointers<F, R>(self, f: F) -> R
88+
where
89+
F: FnOnce(&[sys::GDExtensionConstTypePtr]) -> R;
90+
91+
/// Converts `array` to `Self` by calling [`to_variant`](crate::meta::ToGodot::to_variant) on each argument.
92+
fn to_variant_array(&self) -> Vec<Variant>;
93+
}

0 commit comments

Comments
 (0)