godot_core/meta/param_tuple.rs
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
8use godot_ffi as sys;
9
10use crate::builtin::Variant;
11use crate::meta::error::CallResult;
12use crate::meta::{CallContext, PropertyInfo};
13
14mod impls;
15
16/// Represents a parameter list as Rust tuple where each tuple element is one parameter.
17///
18/// This trait only contains metadata for the parameter list, the actual functionality is contained in [`InParamTuple`] and
19/// [`OutParamTuple`].
20pub trait ParamTuple: Sized {
21 /// The number of elements in this parameter list.
22 const LEN: usize;
23
24 /// The param info of the parameter at index `index`.
25 #[doc(hidden)]
26 fn param_info(
27 index: usize,
28 param_name: &str,
29 ) -> Option<crate::registry::method::MethodParamOrReturnInfo>;
30
31 /// The property info of the parameter at index `index`.
32 fn property_info(index: usize, param_name: &str) -> Option<PropertyInfo> {
33 Self::param_info(index, param_name).map(|param| param.info)
34 }
35
36 /// Return a string representing the arguments.
37 fn format_args(&self) -> String;
38}
39
40/// Represents a parameter list that is received from some external location (usually Godot).
41///
42/// As an example, this would be used for user-defined functions that will be called from Godot, however this is _not_ used when
43/// calling a Godot function from Rust code.
44pub trait InParamTuple: ParamTuple {
45 /// Converts `args_ptr` to `Self`, merging with default values if needed.
46 ///
47 /// # Safety
48 ///
49 /// - `args_ptr` must be a pointer to an array of length `arg_count`
50 /// - Each element of `args_ptr` must be reborrowable as a `&Variant` with a lifetime that lasts for the duration of the call.
51 /// - `arg_count + default_values.len()` must equal `Self::LEN`
52 #[doc(hidden)]
53 unsafe fn from_varcall_args(
54 args_ptr: *const sys::GDExtensionConstVariantPtr,
55 arg_count: usize,
56 default_values: &[Variant],
57 call_ctx: &CallContext,
58 ) -> CallResult<Self>;
59
60 /// Converts `args_ptr` to `Self` directly.
61 ///
62 /// # Safety
63 ///
64 /// - `args_ptr` must be a pointer to a valid array of length [`Self::LEN`](ParamTuple::LEN)
65 /// - each element of `args_ptr` must be of the same type as each element of `Self`
66 #[doc(hidden)] // Hidden since v0.3.2.
67 unsafe fn from_ptrcall_args(
68 args_ptr: *const sys::GDExtensionConstTypePtr,
69 call_type: sys::PtrcallType,
70 call_ctx: &CallContext,
71 ) -> CallResult<Self>;
72
73 /// Converts `array` to `Self` by calling [`from_variant`](crate::meta::FromGodot::from_variant) on each argument.
74 fn from_variant_array(array: &[&Variant]) -> Self;
75}
76
77/// Represents a parameter list that is used to call some external code.
78///
79/// As an example, this would be used to call Godot functions through FFI, however this is _not_ used when Godot calls a user-defined
80/// function.
81pub trait OutParamTuple: ParamTuple {
82 /// Call `f` on the tuple `self` by first converting `self` to an array of [`Variant`]s.
83 fn with_variants<F, R>(self, f: F) -> R
84 where
85 F: FnOnce(&[Variant]) -> R;
86
87 /// Call `f` on the tuple `self` by first converting `self` to an array of [`Variant`] pointers.
88 #[doc(hidden)] // Hidden since v0.3.2.
89 fn with_variant_pointers<F, R>(self, f: F) -> R
90 where
91 F: FnOnce(&[sys::GDExtensionConstVariantPtr]) -> R;
92
93 /// Call `f` on the tuple `self` by first converting `self` to an array of Godot type pointers.
94 #[doc(hidden)] // Hidden since v0.3.2.
95 fn with_type_pointers<F, R>(self, f: F) -> R
96 where
97 F: FnOnce(&[sys::GDExtensionConstTypePtr]) -> R;
98
99 /// Converts `array` to `Self` by calling [`to_variant`](crate::meta::ToGodot::to_variant) on each argument.
100 fn to_variant_array(&self) -> Vec<Variant>;
101}