@@ -9,6 +9,9 @@ use crate::object::*;
9
9
use crate :: private:: { get_api, ManuallyManagedClassPlaceholder } ;
10
10
use crate :: thread_access:: * ;
11
11
12
+ #[ cfg( feature = "serde" ) ]
13
+ mod serialize;
14
+
12
15
// TODO: implement Debug, PartialEq, etc.
13
16
14
17
/// A `Variant` can represent many of godot's core types.
@@ -109,6 +112,13 @@ macro_rules! decl_variant_type {
109
112
) *
110
113
}
111
114
115
+ impl VariantType {
116
+ /// The potential names of VariantTypes. Mostly used for serialization.
117
+ pub const NAMES : & ' static [ & ' static str ] = & [
118
+ $( stringify!( $variant) , ) *
119
+ ] ;
120
+ }
121
+
112
122
/// Rust enum associating each primitive variant type to its value.
113
123
///
114
124
/// For `Variant`s containing objects, the original `Variant` is returned unchanged, due to
@@ -132,6 +142,19 @@ macro_rules! decl_variant_type {
132
142
}
133
143
}
134
144
}
145
+
146
+ impl <' a> From <& ' a VariantDispatch > for Variant {
147
+ #[ inline]
148
+ fn from( v: & ' a VariantDispatch ) -> Self {
149
+ match v {
150
+ $( $( VariantDispatch :: $variant( v) => {
151
+ let v: & $inner = v;
152
+ v. to_variant( )
153
+ } ) ?) *
154
+ _ => Variant :: new( )
155
+ }
156
+ }
157
+ }
135
158
}
136
159
}
137
160
@@ -173,6 +196,15 @@ impl VariantType {
173
196
pub fn from_sys ( v : sys:: godot_variant_type ) -> VariantType {
174
197
unsafe { transmute ( v as u32 ) }
175
198
}
199
+
200
+ /// The `stringify!` representation of this variant. Mostly used for serialization.
201
+ #[ inline]
202
+ pub const fn name ( & self ) -> & ' static str {
203
+ // NOTE: this assumes that the discriminants remain sequential, since any additions to the
204
+ // VariantType enum would require a breaking change anyway since it is not marked as non-exhaustive.
205
+ // See also the Deserialize implementation in the serde submodule.
206
+ Self :: NAMES [ * self as usize ]
207
+ }
176
208
}
177
209
178
210
#[ repr( u32 ) ]
0 commit comments