6
6
*/
7
7
8
8
use crate :: builtin:: { GString , NodePath , StringName } ;
9
- use crate :: meta:: { sealed, CowArg , ToGodot } ;
9
+ use crate :: meta:: sealed:: Sealed ;
10
+ use crate :: meta:: { CowArg , ToGodot } ;
10
11
use std:: ffi:: CStr ;
11
12
12
13
/// Implicit conversions for arguments passed to Godot APIs.
64
65
Self : ' r ;
65
66
}
66
67
67
- impl < T : ToGodot > AsArg < T > for & T {
68
+ /// Generic abstraction over `T` and `&T` that should be passed as `AsArg<T>`.
69
+ #[ doc( hidden) ]
70
+ pub fn val_into_arg < ' r , T > ( arg : T ) -> impl AsArg < T > + ' r
71
+ where
72
+ T : ToGodot + ' r ,
73
+ {
74
+ CowArg :: Owned ( arg)
75
+ }
76
+
77
+ impl < T > AsArg < T > for & T
78
+ where
79
+ T : ToGodot + ParamType < ArgPassing = ByRef > ,
80
+ {
68
81
fn into_arg < ' r > ( self ) -> CowArg < ' r , T >
69
82
where
70
83
Self : ' r ,
@@ -73,7 +86,10 @@ impl<T: ToGodot> AsArg<T> for &T {
73
86
}
74
87
}
75
88
76
- impl < T : ToGodot + Copy > AsArg < T > for T {
89
+ impl < T > AsArg < T > for T
90
+ where
91
+ T : ToGodot + ParamType < ArgPassing = ByValue > ,
92
+ {
77
93
fn into_arg < ' r > ( self ) -> CowArg < ' r , T >
78
94
where
79
95
Self : ' r ,
@@ -128,27 +144,17 @@ macro_rules! arg_into_owned {
128
144
#[ macro_export]
129
145
macro_rules! impl_asarg_by_value {
130
146
( $T: ty) => {
131
- #[ expect( deprecated) ]
132
147
impl $crate:: meta:: ParamType for $T {
133
- type Arg <' v> = $T;
134
-
135
- fn owned_to_arg<' v>( self ) -> Self :: Arg <' v> {
136
- self
137
- }
148
+ type ArgPassing = $crate:: meta:: ByValue ;
138
149
}
139
150
} ;
140
151
}
141
152
142
153
#[ macro_export]
143
154
macro_rules! impl_asarg_by_ref {
144
155
( $T: ty) => {
145
- #[ expect( deprecated) ]
146
156
impl $crate:: meta:: ParamType for $T {
147
- type Arg <' v> = $crate:: meta:: CowArg <' v, $T>;
148
-
149
- fn owned_to_arg<' v>( self ) -> Self :: Arg <' v> {
150
- $crate:: meta:: CowArg :: Owned ( self )
151
- }
157
+ type ArgPassing = $crate:: meta:: ByRef ;
152
158
}
153
159
} ;
154
160
}
@@ -255,27 +261,31 @@ impl AsArg<NodePath> for &String {
255
261
/// Implemented for all parameter types `T` that are allowed to receive [impl `AsArg<T>`][AsArg].
256
262
// ParamType used to be a subtrait of GodotType, but this can be too restrictive. For example, DynGd is not a "Godot canonical type"
257
263
// (GodotType), however it's still useful to store it in arrays -- which requires AsArg and subsequently ParamType.
258
- #[ deprecated(
259
- since = "0.3.2" ,
260
- note = "This trait is no longer needed and will be removed in 0.4"
261
- ) ]
262
- pub trait ParamType : sealed:: Sealed + Sized + ' static + ToGodot
264
+ //
265
+ // TODO(v0.4): merge ParamType::ArgPassing into ToGodot::ToVia, reducing redundancy on user side.
266
+ pub trait ParamType : ToGodot + Sized + ' static
263
267
// GodotType bound not required right now, but conceptually should always be the case.
264
268
{
265
- /// Canonical argument passing type, either `T` or an internally-used CoW type.
266
- ///
267
- /// The general rule is that `Copy` types are passed by value, while the rest is passed by reference.
268
- ///
269
- /// This associated type is closely related to [`ToGodot::ToVia<'v>`][crate::meta::ToGodot::ToVia] and may be reorganized in the future.
270
- #[ doc( hidden) ]
271
- type Arg < ' v > : AsArg < Self >
272
- where
273
- Self : ' v ;
274
-
275
- /// Converts an owned value to the canonical argument type, which can be passed to [`impl AsArg<T>`][AsArg].
276
- ///
277
- /// Useful in generic contexts where only a value is available, and one doesn't want to dispatch between value/reference.
278
- ///
279
- /// You should not rely on the exact return type, as it may change in future versions; treat it like `impl AsArg<Self>`.
280
- fn owned_to_arg < ' v > ( self ) -> Self :: Arg < ' v > ;
269
+ type ArgPassing : ArgPassing ;
270
+
271
+ #[ deprecated(
272
+ since = "0.3.2" ,
273
+ note = "This method is no longer needed and will be removed in 0.4"
274
+ ) ]
275
+ fn owned_to_arg ( self ) -> impl AsArg < Self > {
276
+ val_into_arg ( self )
277
+ }
281
278
}
279
+
280
+ // ----------------------------------------------------------------------------------------------------------------------------------------------
281
+ // Argument passing (mutually exclusive by-val or by-ref).
282
+
283
+ pub trait ArgPassing : Sealed { }
284
+
285
+ pub enum ByValue { }
286
+ impl ArgPassing for ByValue { }
287
+ impl Sealed for ByValue { }
288
+
289
+ pub enum ByRef { }
290
+ impl ArgPassing for ByRef { }
291
+ impl Sealed for ByRef { }
0 commit comments