6
6
*/
7
7
8
8
use crate :: builtin:: { GString , NodePath , StringName } ;
9
- use crate :: meta:: CowArg ;
9
+ use crate :: meta:: { CowArg , GodotType } ;
10
10
use std:: ffi:: CStr ;
11
11
12
12
/// Implicit conversions for arguments passed to Godot APIs.
@@ -23,7 +23,7 @@ use std::ffi::CStr;
23
23
/// Implicitly converting from `T` for by-ref builtins is explicitly not supported. This emphasizes that there is no need to consume the object,
24
24
/// thus discourages unnecessary cloning.
25
25
///
26
- /// If you need to pass owned values in generic code, you can use [`ArgTarget ::value_to_arg()`].
26
+ /// If you need to pass owned values in generic code, you can use [`ApiParam ::value_to_arg()`].
27
27
///
28
28
/// # Performance for strings
29
29
/// Godot has three string types: [`GString`], [`StringName`] and [`NodePath`]. Conversions between those three, as well as between `String` and
@@ -37,18 +37,25 @@ use std::ffi::CStr;
37
37
///
38
38
/// If you want to convert between Godot's string types for the sake of argument passing, each type provides an `arg()` method, such as
39
39
/// [`GString::arg()`]. You cannot use this method in other contexts.
40
+ ///
41
+ /// # Using the trait
42
+ /// `AsArg` is meant to be used from the function call site, not the declaration site. If you declare a parameter as `impl AsArg<...>` yourself,
43
+ /// you can only forward it as-is to a Godot API -- there are no stable APIs to access the inner object yet.
44
+ ///
45
+ /// Furthermore, there is currently no benefit in implementing `AsArg` for your own types, as it's only used by Godot APIs which don't accept
46
+ /// custom types. Classes are already supported through upcasting and [`AsObjectArg`][crate::obj::AsObjectArg].
40
47
#[ diagnostic:: on_unimplemented(
41
48
message = "Argument of type `{Self}` cannot be passed to an `impl AsArg<{T}>` parameter" ,
42
49
note = "If you pass by value, consider borrowing instead." ,
43
50
note = "GString/StringName/NodePath aren't implicitly convertible for performance reasons; use their `arg()` method." ,
44
51
note = "See also `AsArg` docs: https://godot-rust.github.io/docs/gdext/master/godot/meta/trait.AsArg.html"
45
52
) ]
46
- pub trait AsArg < T : ArgTarget >
53
+ pub trait AsArg < T : ApiParam >
47
54
where
48
55
Self : Sized ,
49
56
{
50
57
#[ doc( hidden) ]
51
- fn into_arg < ' r > ( self ) -> <T as ArgTarget >:: Arg < ' r >
58
+ fn into_arg < ' r > ( self ) -> <T as ApiParam >:: Arg < ' r >
52
59
where
53
60
Self : ' r ;
54
61
}
@@ -70,7 +77,7 @@ macro_rules! arg_into_ref {
70
77
} ;
71
78
( $arg_variable: ident: $T: ty) => {
72
79
let $arg_variable = $arg_variable. into_arg( ) ;
73
- let $arg_variable: & $T = $crate:: meta:: ArgTarget :: arg_to_ref( & $arg_variable) ;
80
+ let $arg_variable: & $T = $crate:: meta:: ApiParam :: arg_to_ref( & $arg_variable) ;
74
81
} ;
75
82
}
76
83
@@ -82,21 +89,21 @@ macro_rules! arg_into_owned {
82
89
( $arg_variable: ident) => {
83
90
let $arg_variable = $arg_variable. into_arg( ) ;
84
91
let $arg_variable = $arg_variable. cow_into_owned( ) ;
85
- // cow_into_owned() is not yet used generically; could be abstracted in ArgTarget ::arg_to_owned() as well.
92
+ // cow_into_owned() is not yet used generically; could be abstracted in ApiParam ::arg_to_owned() as well.
86
93
} ;
87
94
}
88
95
89
96
#[ macro_export]
90
97
macro_rules! impl_asarg_by_value {
91
98
( $T: ty) => {
92
99
impl $crate:: meta:: AsArg <$T> for $T {
93
- fn into_arg<' r>( self ) -> <$T as $crate:: meta:: ArgTarget >:: Arg <' r> {
100
+ fn into_arg<' r>( self ) -> <$T as $crate:: meta:: ApiParam >:: Arg <' r> {
94
101
// Moves value (but typically a Copy type).
95
102
self
96
103
}
97
104
}
98
105
99
- impl $crate:: meta:: ArgTarget for $T {
106
+ impl $crate:: meta:: ApiParam for $T {
100
107
type Arg <' v> = $T;
101
108
102
109
fn value_to_arg<' v>( self ) -> Self :: Arg <' v> {
@@ -121,15 +128,15 @@ macro_rules! impl_asarg_by_ref {
121
128
// Thus, keep `where` on same line.
122
129
// type ArgType<'v> = &'v $T where Self: 'v;
123
130
124
- fn into_arg<' cow>( self ) -> <$T as $crate:: meta:: ArgTarget >:: Arg <' cow>
131
+ fn into_arg<' cow>( self ) -> <$T as $crate:: meta:: ApiParam >:: Arg <' cow>
125
132
where
126
133
' r: ' cow, // Original reference must be valid for at least as long as the returned cow.
127
134
{
128
135
$crate:: meta:: CowArg :: Borrowed ( self )
129
136
}
130
137
}
131
138
132
- impl $crate:: meta:: ArgTarget for $T {
139
+ impl $crate:: meta:: ApiParam for $T {
133
140
type Arg <' v> = $crate:: meta:: CowArg <' v, $T>;
134
141
135
142
fn value_to_arg<' v>( self ) -> Self :: Arg <' v> {
@@ -150,11 +157,11 @@ macro_rules! declare_arg_method {
150
157
///
151
158
/// # Generic bounds
152
159
/// The bounds are implementation-defined and may change at any time. Do not use this function in a generic context requiring `T`
153
- /// -- use the `From` trait or [`ArgTarget `][crate::meta::ArgTarget ] in that case.
160
+ /// -- use the `From` trait or [`ApiParam `][crate::meta::ApiParam ] in that case.
154
161
pub fn arg<T >( & self ) -> impl $crate:: meta:: AsArg <T >
155
162
where
156
163
for <' a> T : From <& ' a Self >
157
- + $crate:: meta:: ArgTarget <Arg <' a> = $crate:: meta:: CowArg <' a, T >>
164
+ + $crate:: meta:: ApiParam <Arg <' a> = $crate:: meta:: CowArg <' a, T >>
158
165
+ ' a,
159
166
{
160
167
$crate:: meta:: CowArg :: Owned ( T :: from( self ) )
@@ -171,7 +178,7 @@ macro_rules! declare_arg_method {
171
178
/// This is necessary for packed array dispatching to different "inner" backend signatures.
172
179
impl < ' a , T > AsArg < T > for CowArg < ' a , T >
173
180
where
174
- for < ' r > T : ArgTarget < Arg < ' r > = CowArg < ' r , T > > + ' r ,
181
+ for < ' r > T : ApiParam < Arg < ' r > = CowArg < ' r , T > > + ' r ,
175
182
{
176
183
fn into_arg < ' r > ( self ) -> CowArg < ' r , T >
177
184
where
@@ -181,7 +188,7 @@ where
181
188
}
182
189
}
183
190
184
- // impl<'a, T> ArgTarget for CowArg<'a, T> {
191
+ // impl<'a, T> ApiParam for CowArg<'a, T> {
185
192
// type Type<'v> = CowArg<'v, T>
186
193
// where Self: 'v;
187
194
// }
@@ -243,7 +250,8 @@ impl AsArg<NodePath> for &String {
243
250
// ----------------------------------------------------------------------------------------------------------------------------------------------
244
251
245
252
/// Implemented for all parameter types `T` that are allowed to receive [impl `AsArg<T>`][AsArg].
246
- pub trait ArgTarget
253
+ pub trait ApiParam : GodotType
254
+ // GodotType bound not required right now, but conceptually should always be the case.
247
255
where
248
256
Self : Sized ,
249
257
{
0 commit comments