@@ -13,7 +13,10 @@ use std::fmt;
1313use std:: marker:: PhantomData ;
1414use sys:: { ffi_methods, interface_fn, GodotFfi } ;
1515
16- use super :: meta:: { FromGodot , GodotConvert , GodotFfiVariant , GodotType , ToGodot } ;
16+ use super :: meta:: {
17+ ConvertError , FromGodot , FromGodotError , FromVariantError , GodotConvert , GodotFfiVariant ,
18+ GodotType , ToGodot ,
19+ } ;
1720
1821/// Godot's `Array` type.
1922///
@@ -337,11 +340,18 @@ impl<T: GodotType> Array<T> {
337340 }
338341
339342 /// Checks that the inner array has the correct type set on it for storing elements of type `T`.
340- fn with_checked_type ( self ) -> Result < Self , VariantConversionError > {
341- if self . type_info ( ) == TypeInfo :: of :: < T > ( ) {
343+ fn with_checked_type ( self ) -> Result < Self , ConvertError > {
344+ let self_ty = self . type_info ( ) ;
345+ let target_ty = TypeInfo :: of :: < T > ( ) ;
346+
347+ if self_ty == target_ty {
342348 Ok ( self )
343349 } else {
344- Err ( VariantConversionError :: BadType )
350+ Err ( FromGodotError :: BadArrayType {
351+ expected : target_ty,
352+ got : self_ty,
353+ }
354+ . into_error ( self ) )
345355 }
346356 }
347357
@@ -633,11 +643,15 @@ impl<T: GodotType> ToGodot for Array<T> {
633643 fn into_godot ( self ) -> Self :: Via {
634644 self
635645 }
646+
647+ fn to_variant ( & self ) -> Variant {
648+ self . ffi_to_variant ( )
649+ }
636650}
637651
638652impl < T : GodotType > FromGodot for Array < T > {
639- fn try_from_godot ( via : Self :: Via ) -> Option < Self > {
640- Some ( via)
653+ fn try_from_godot ( via : Self :: Via ) -> Result < Self , ConvertError > {
654+ Ok ( via)
641655 }
642656}
643657
@@ -755,15 +769,16 @@ impl<T: GodotType> GodotType for Array<T> {
755769 type Ffi = Self ;
756770
757771 fn to_ffi ( & self ) -> Self :: Ffi {
772+ // `to_ffi` is sometimes intentionally called with an array in an invalid state.
758773 self . clone ( )
759774 }
760775
761776 fn into_ffi ( self ) -> Self :: Ffi {
762777 self
763778 }
764779
765- fn try_from_ffi ( ffi : Self :: Ffi ) -> Option < Self > {
766- Some ( ffi)
780+ fn try_from_ffi ( ffi : Self :: Ffi ) -> Result < Self , ConvertError > {
781+ Ok ( ffi)
767782 }
768783
769784 fn godot_type_name ( ) -> String {
@@ -781,9 +796,13 @@ impl<T: GodotType> GodotFfiVariant for Array<T> {
781796 }
782797 }
783798
784- fn ffi_from_variant ( variant : & Variant ) -> Result < Self , VariantConversionError > {
799+ fn ffi_from_variant ( variant : & Variant ) -> Result < Self , ConvertError > {
785800 if variant. get_type ( ) != Self :: variant_type ( ) {
786- return Err ( VariantConversionError :: BadType ) ;
801+ return Err ( FromVariantError :: BadType {
802+ expected : Self :: variant_type ( ) ,
803+ got : variant. get_type ( ) ,
804+ }
805+ . into_error ( variant. clone ( ) ) ) ;
787806 }
788807
789808 let array = unsafe {
@@ -992,7 +1011,7 @@ macro_rules! varray {
9921011///
9931012/// We ignore the `script` parameter because it has no impact on typing in Godot.
9941013#[ derive( PartialEq , Eq ) ]
995- struct TypeInfo {
1014+ pub ( crate ) struct TypeInfo {
9961015 variant_type : VariantType ,
9971016
9981017 /// Not a `ClassName` because some values come from Godot engine API.
@@ -1007,9 +1026,17 @@ impl TypeInfo {
10071026 }
10081027 }
10091028
1010- fn is_typed ( & self ) -> bool {
1029+ pub fn is_typed ( & self ) -> bool {
10111030 self . variant_type != VariantType :: Nil
10121031 }
1032+
1033+ pub fn variant_type ( & self ) -> VariantType {
1034+ self . variant_type
1035+ }
1036+
1037+ pub fn class_name ( & self ) -> & StringName {
1038+ & self . class_name
1039+ }
10131040}
10141041
10151042impl fmt:: Debug for TypeInfo {
0 commit comments