8
8
use godot_ffi as sys;
9
9
10
10
use crate :: builtin:: { Array , Variant } ;
11
+ use crate :: meta;
11
12
use crate :: meta:: error:: { ConvertError , ErrorKind , FromFfiError , FromVariantError } ;
12
13
use crate :: meta:: {
13
14
ArrayElement , ClassName , FromGodot , GodotConvert , GodotNullableFfi , GodotType ,
@@ -83,41 +84,49 @@ where
83
84
fn godot_type_name ( ) -> String {
84
85
T :: godot_type_name ( )
85
86
}
87
+
88
+ // Only relevant for object types T.
89
+ unsafe fn as_object_arg ( & self ) -> meta:: ObjectArg {
90
+ match self {
91
+ Some ( inner) => unsafe { inner. as_object_arg ( ) } ,
92
+ None => meta:: ObjectArg :: null ( ) ,
93
+ }
94
+ }
86
95
}
87
96
88
- impl < T : GodotConvert > GodotConvert for Option < T >
97
+ impl < T > GodotConvert for Option < T >
89
98
where
99
+ T : GodotConvert ,
90
100
Option < T :: Via > : GodotType ,
91
101
{
92
102
type Via = Option < T :: Via > ;
93
103
}
94
104
95
- impl < T : ToGodot > ToGodot for Option < T >
105
+ impl < T > ToGodot for Option < T >
96
106
where
97
- Option < T :: Via > : GodotType ,
107
+ // Currently limited to holding objects -> needed to establish to_godot() relation T::to_godot() = Option<&T::Via>.
108
+ T : ToGodot < Pass = meta:: ByObject > ,
109
+ // Extra Clone bound for to_godot_owned(); might be extracted in the future.
110
+ T :: Via : Clone ,
111
+ // T::Via must be a Godot nullable type (to support the None case).
98
112
for < ' f > T :: Via : GodotType <
99
113
// Associated types need to be nullable.
100
114
Ffi : GodotNullableFfi ,
101
115
ToFfi < ' f > : GodotNullableFfi ,
102
116
> ,
103
- T :: Via : Clone ,
117
+ // Previously used bound, not needed right now but don't remove: Option< T::Via>: GodotType ,
104
118
{
105
- // Potential optimization: use underlying T::Pass instead of ByValue.
106
- // Problem is that ByRef requires to_godot() -> &Self::Via, which is &Option<T::Via>. We would however need Option<&T::Via>.
107
- // Might need a third ArgPassing impl, or different design.
108
- type Pass = crate :: meta:: ByValue ;
119
+ // Basically ByRef, but allows Option<T> -> Option<&T::Via> conversion.
120
+ type Pass = meta:: ByOption < T :: Via > ;
109
121
110
- fn to_godot ( & self ) -> Option < T :: Via > {
111
- self . as_ref ( ) . map ( T :: to_godot_owned )
122
+ fn to_godot ( & self ) -> Option < & T :: Via > {
123
+ self . as_ref ( ) . map ( T :: to_godot )
112
124
}
113
125
114
- fn to_godot_owned ( & self ) -> Self :: Via
126
+ fn to_godot_owned ( & self ) -> Option < T :: Via >
115
127
where
116
128
Self :: Via : Clone ,
117
129
{
118
- // Default implementation calls underlying T::to_godot().clone(), which may be wrong.
119
- // Some to_godot_owned() calls are specialized/overridden, we need to honor that.
120
-
121
130
self . as_ref ( ) . map ( T :: to_godot_owned)
122
131
}
123
132
@@ -248,7 +257,7 @@ macro_rules! impl_godot_scalar {
248
257
}
249
258
250
259
impl ToGodot for $T {
251
- type Pass = crate :: meta:: ByValue ;
260
+ type Pass = meta:: ByValue ;
252
261
253
262
fn to_godot( & self ) -> Self :: Via {
254
263
* self
@@ -264,10 +273,10 @@ macro_rules! impl_godot_scalar {
264
273
}
265
274
266
275
// `GodotType` for these three is implemented in `godot-core/src/builtin/variant/impls.rs`.
267
- crate :: meta:: impl_godot_as_self!( bool : ByValue ) ;
268
- crate :: meta:: impl_godot_as_self!( i64 : ByValue ) ;
269
- crate :: meta:: impl_godot_as_self!( f64 : ByValue ) ;
270
- crate :: meta:: impl_godot_as_self!( ( ) : ByValue ) ;
276
+ meta:: impl_godot_as_self!( bool : ByValue ) ;
277
+ meta:: impl_godot_as_self!( i64 : ByValue ) ;
278
+ meta:: impl_godot_as_self!( f64 : ByValue ) ;
279
+ meta:: impl_godot_as_self!( ( ) : ByValue ) ;
271
280
272
281
// Also implements ArrayElement.
273
282
impl_godot_scalar ! (
@@ -334,7 +343,7 @@ impl GodotConvert for u64 {
334
343
}
335
344
336
345
impl ToGodot for u64 {
337
- type Pass = crate :: meta:: ByValue ;
346
+ type Pass = meta:: ByValue ;
338
347
339
348
fn to_godot ( & self ) -> Self :: Via {
340
349
* self
@@ -374,7 +383,7 @@ impl<T: ArrayElement> GodotConvert for Vec<T> {
374
383
}
375
384
376
385
impl < T : ArrayElement > ToGodot for Vec < T > {
377
- type Pass = crate :: meta:: ByValue ;
386
+ type Pass = meta:: ByValue ;
378
387
379
388
fn to_godot ( & self ) -> Self :: Via {
380
389
Array :: from ( self . as_slice ( ) )
@@ -392,7 +401,7 @@ impl<T: ArrayElement, const LEN: usize> GodotConvert for [T; LEN] {
392
401
}
393
402
394
403
impl < T : ArrayElement , const LEN : usize > ToGodot for [ T ; LEN ] {
395
- type Pass = crate :: meta:: ByValue ;
404
+ type Pass = meta:: ByValue ;
396
405
397
406
fn to_godot ( & self ) -> Self :: Via {
398
407
Array :: from ( self )
@@ -432,7 +441,7 @@ impl<T: ArrayElement> GodotConvert for &[T] {
432
441
}
433
442
434
443
impl < T : ArrayElement > ToGodot for & [ T ] {
435
- type Pass = crate :: meta:: ByValue ;
444
+ type Pass = meta:: ByValue ;
436
445
437
446
fn to_godot ( & self ) -> Self :: Via {
438
447
Array :: from ( * self )
@@ -453,7 +462,7 @@ macro_rules! impl_pointer_convert {
453
462
}
454
463
455
464
impl ToGodot for $Ptr {
456
- type Pass = crate :: meta:: ByValue ;
465
+ type Pass = meta:: ByValue ;
457
466
458
467
fn to_godot( & self ) -> Self :: Via {
459
468
* self as i64
0 commit comments