@@ -9,7 +9,9 @@ use super::*;
99use crate :: builtin:: * ;
1010use crate :: global;
1111use crate :: meta:: error:: { ConvertError , FromVariantError } ;
12- use crate :: meta:: { ArrayElement , GodotFfiVariant , GodotType , PropertyHintInfo , PropertyInfo } ;
12+ use crate :: meta:: {
13+ ArrayElement , GodotFfiVariant , GodotType , PropertyHintInfo , PropertyInfo , RefArg ,
14+ } ;
1315use godot_ffi as sys;
1416// For godot-cpp, see https://github.com/godotengine/godot-cpp/blob/master/include/godot_cpp/core/type_info.hpp.
1517
@@ -22,7 +24,15 @@ use godot_ffi as sys;
2224//
2325// Therefore, we can use `init` to indicate when it must be initialized in 4.0.
2426macro_rules! impl_ffi_variant {
25- ( $T: ty, $from_fn: ident, $to_fn: ident $( ; $godot_type_name: ident) ?) => {
27+ ( ref $T: ty, $from_fn: ident, $to_fn: ident $( ; $GodotTy: ident) ?) => {
28+ impl_ffi_variant!( @impls by_ref; $T, $from_fn, $to_fn $( ; $GodotTy) ?) ;
29+ } ;
30+ ( $T: ty, $from_fn: ident, $to_fn: ident $( ; $GodotTy: ident) ?) => {
31+ impl_ffi_variant!( @impls by_val; $T, $from_fn, $to_fn $( ; $GodotTy) ?) ;
32+ } ;
33+
34+ // Implementations
35+ ( @impls $by_ref_or_val: ident; $T: ty, $from_fn: ident, $to_fn: ident $( ; $GodotTy: ident) ?) => {
2636 impl GodotFfiVariant for $T {
2737 fn ffi_to_variant( & self ) -> Variant {
2838 let variant = unsafe {
@@ -58,10 +68,7 @@ macro_rules! impl_ffi_variant {
5868
5969 impl GodotType for $T {
6070 type Ffi = Self ;
61-
62- fn to_ffi( & self ) -> Self :: Ffi {
63- self . clone( )
64- }
71+ impl_ffi_variant!( @assoc_to_ffi $by_ref_or_val) ;
6572
6673 fn into_ffi( self ) -> Self :: Ffi {
6774 self
@@ -71,7 +78,7 @@ macro_rules! impl_ffi_variant {
7178 Ok ( ffi)
7279 }
7380
74- impl_ffi_variant!( @godot_type_name $T $( , $godot_type_name ) ?) ;
81+ impl_ffi_variant!( @godot_type_name $T $( , $GodotTy ) ?) ;
7582 }
7683
7784 impl ArrayElement for $T { }
@@ -88,6 +95,22 @@ macro_rules! impl_ffi_variant {
8895 stringify!( $godot_type_name) . into( )
8996 }
9097 } ;
98+
99+ ( @assoc_to_ffi by_ref) => {
100+ type ToFfi <' a> = RefArg <' a, Self >;
101+
102+ fn to_ffi( & self ) -> Self :: ToFfi <' _> {
103+ RefArg :: new( self )
104+ }
105+ } ;
106+
107+ ( @assoc_to_ffi by_val) => {
108+ type ToFfi <' a> = Self ;
109+
110+ fn to_ffi( & self ) -> Self :: ToFfi <' _> {
111+ self . clone( )
112+ }
113+ } ;
91114}
92115
93116// ----------------------------------------------------------------------------------------------------------------------------------------------
@@ -116,17 +139,17 @@ mod impls {
116139 impl_ffi_variant ! ( GString , string_to_variant, string_from_variant; String ) ;
117140 impl_ffi_variant ! ( StringName , string_name_to_variant, string_name_from_variant) ;
118141 impl_ffi_variant ! ( NodePath , node_path_to_variant, node_path_from_variant) ;
119- impl_ffi_variant ! ( PackedByteArray , packed_byte_array_to_variant, packed_byte_array_from_variant) ;
120- impl_ffi_variant ! ( PackedInt32Array , packed_int32_array_to_variant, packed_int32_array_from_variant) ;
121- impl_ffi_variant ! ( PackedInt64Array , packed_int64_array_to_variant, packed_int64_array_from_variant) ;
122- impl_ffi_variant ! ( PackedFloat32Array , packed_float32_array_to_variant, packed_float32_array_from_variant) ;
123- impl_ffi_variant ! ( PackedFloat64Array , packed_float64_array_to_variant, packed_float64_array_from_variant) ;
124- impl_ffi_variant ! ( PackedStringArray , packed_string_array_to_variant, packed_string_array_from_variant) ;
125- impl_ffi_variant ! ( PackedVector2Array , packed_vector2_array_to_variant, packed_vector2_array_from_variant) ;
126- impl_ffi_variant ! ( PackedVector3Array , packed_vector3_array_to_variant, packed_vector3_array_from_variant) ;
142+ impl_ffi_variant ! ( ref PackedByteArray , packed_byte_array_to_variant, packed_byte_array_from_variant) ;
143+ impl_ffi_variant ! ( ref PackedInt32Array , packed_int32_array_to_variant, packed_int32_array_from_variant) ;
144+ impl_ffi_variant ! ( ref PackedInt64Array , packed_int64_array_to_variant, packed_int64_array_from_variant) ;
145+ impl_ffi_variant ! ( ref PackedFloat32Array , packed_float32_array_to_variant, packed_float32_array_from_variant) ;
146+ impl_ffi_variant ! ( ref PackedFloat64Array , packed_float64_array_to_variant, packed_float64_array_from_variant) ;
147+ impl_ffi_variant ! ( ref PackedStringArray , packed_string_array_to_variant, packed_string_array_from_variant) ;
148+ impl_ffi_variant ! ( ref PackedVector2Array , packed_vector2_array_to_variant, packed_vector2_array_from_variant) ;
149+ impl_ffi_variant ! ( ref PackedVector3Array , packed_vector3_array_to_variant, packed_vector3_array_from_variant) ;
127150 #[ cfg( since_api = "4.3" ) ]
128- impl_ffi_variant ! ( PackedVector4Array , packed_vector4_array_to_variant, packed_vector4_array_from_variant) ;
129- impl_ffi_variant ! ( PackedColorArray , packed_color_array_to_variant, packed_color_array_from_variant) ;
151+ impl_ffi_variant ! ( ref PackedVector4Array , packed_vector4_array_to_variant, packed_vector4_array_from_variant) ;
152+ impl_ffi_variant ! ( ref PackedColorArray , packed_color_array_to_variant, packed_color_array_from_variant) ;
130153 impl_ffi_variant ! ( Plane , plane_to_variant, plane_from_variant) ;
131154 impl_ffi_variant ! ( Projection , projection_to_variant, projection_from_variant) ;
132155 impl_ffi_variant ! ( Rid , rid_to_variant, rid_from_variant; RID ) ;
@@ -135,7 +158,7 @@ mod impls {
135158 impl_ffi_variant ! ( Signal , signal_to_variant, signal_from_variant) ;
136159 impl_ffi_variant ! ( Transform2D , transform_2d_to_variant, transform_2d_from_variant) ;
137160 impl_ffi_variant ! ( Transform3D , transform_3d_to_variant, transform_3d_from_variant) ;
138- impl_ffi_variant ! ( Dictionary , dictionary_to_variant, dictionary_from_variant) ;
161+ impl_ffi_variant ! ( ref Dictionary , dictionary_to_variant, dictionary_from_variant) ;
139162
140163}
141164
@@ -162,9 +185,10 @@ impl GodotFfiVariant for () {
162185}
163186
164187impl GodotType for ( ) {
165- type Ffi = Self ;
188+ type Ffi = ( ) ;
189+ type ToFfi < ' a > = ( ) ;
166190
167- fn to_ffi ( & self ) -> Self :: Ffi { }
191+ fn to_ffi ( & self ) -> Self :: ToFfi < ' _ > { }
168192
169193 fn into_ffi ( self ) -> Self :: Ffi { }
170194
@@ -189,9 +213,10 @@ impl GodotFfiVariant for Variant {
189213
190214impl GodotType for Variant {
191215 type Ffi = Variant ;
216+ type ToFfi < ' a > = RefArg < ' a , Variant > ;
192217
193- fn to_ffi ( & self ) -> Self :: Ffi {
194- self . clone ( )
218+ fn to_ffi ( & self ) -> Self :: ToFfi < ' _ > {
219+ RefArg :: new ( self )
195220 }
196221
197222 fn into_ffi ( self ) -> Self :: Ffi {
0 commit comments