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 super::{CallContext, CallResult, PropertyInfo};
11use crate::builtin::Variant;
12
13mod 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`].
19pub 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.
43pub 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 #[doc(hidden)] // Hidden since v0.3.2.
51 unsafe fn from_varcall_args(
52 args_ptr: *const sys::GDExtensionConstVariantPtr,
53 call_ctx: &CallContext,
54 ) -> CallResult<Self>;
55
56 /// Converts `args_ptr` to `Self` directly.
57 ///
58 /// # Safety
59 ///
60 /// - `args_ptr` must be a pointer to a valid array of length [`Self::LEN`](ParamTuple::LEN)
61 /// - each element of `args_ptr` must be of the same type as each element of `Self`
62 #[doc(hidden)] // Hidden since v0.3.2.
63 unsafe fn from_ptrcall_args(
64 args_ptr: *const sys::GDExtensionConstTypePtr,
65 call_type: sys::PtrcallType,
66 call_ctx: &CallContext,
67 ) -> Self;
68
69 /// Converts `array` to `Self` by calling [`from_variant`](crate::meta::FromGodot::from_variant) on each argument.
70 fn from_variant_array(array: &[&Variant]) -> Self;
71}
72
73/// Represents a parameter list that is used to call some external code.
74///
75/// As an example, this would be used to call Godot functions through FFI, however this is _not_ used when Godot calls a user-defined
76/// function.
77pub trait OutParamTuple: ParamTuple {
78 /// Call `f` on the tuple `self` by first converting `self` to an array of [`Variant`]s.
79 fn with_variants<F, R>(self, f: F) -> R
80 where
81 F: FnOnce(&[Variant]) -> R;
82
83 /// Call `f` on the tuple `self` by first converting `self` to an array of [`Variant`] pointers.
84 #[doc(hidden)] // Hidden since v0.3.2.
85 fn with_variant_pointers<F, R>(self, f: F) -> R
86 where
87 F: FnOnce(&[sys::GDExtensionConstVariantPtr]) -> R;
88
89 /// Call `f` on the tuple `self` by first converting `self` to an array of Godot type pointers.
90 #[doc(hidden)] // Hidden since v0.3.2.
91 fn with_type_pointers<F, R>(self, f: F) -> R
92 where
93 F: FnOnce(&[sys::GDExtensionConstTypePtr]) -> R;
94
95 /// Converts `array` to `Self` by calling [`to_variant`](crate::meta::ToGodot::to_variant) on each argument.
96 fn to_variant_array(&self) -> Vec<Variant>;
97}