@@ -70,12 +70,11 @@ impl_builtin_froms!(Array;
70
70
71
71
impl < T : VariantMetadata > TypedArray < T > {
72
72
fn from_opaque ( opaque : sys:: types:: OpaqueArray ) -> Self {
73
- let array = Self {
73
+ // Note: type is not yet checked at this point, because array has not yet been initialized!
74
+ Self {
74
75
opaque,
75
76
_phantom : PhantomData ,
76
- } ;
77
- array. check_type ( ) ;
78
- array
77
+ }
79
78
}
80
79
81
80
/// Returns the number of elements in the array. Equivalent of `size()` in Godot.
@@ -320,8 +319,9 @@ impl<T: VariantMetadata> TypedArray<T> {
320
319
/// # Panics
321
320
///
322
321
/// If the inner type doesn't match `T` or no type is set at all.
323
- fn check_type ( & self ) {
322
+ fn with_checked_type ( self ) -> Self {
324
323
assert_eq ! ( self . type_info( ) , TypeInfo :: new:: <T >( ) ) ;
324
+ self
325
325
}
326
326
327
327
/// Sets the type of the inner array. Can only be called once, directly after creation.
@@ -567,17 +567,9 @@ impl<T: VariantMetadata + ToVariant> TypedArray<T> {
567
567
// }
568
568
569
569
impl < T : VariantMetadata > GodotFfi for TypedArray < T > {
570
- ffi_methods ! {
571
- type sys:: GDExtensionTypePtr = * mut Opaque ;
572
- fn from_sys;
573
- fn sys;
574
- fn write_sys;
575
- }
576
-
577
- unsafe fn from_sys_init ( init_fn : impl FnOnce ( sys:: GDExtensionTypePtr ) ) -> Self {
578
- // Can't use uninitialized pointer -- Array CoW implementation in C++ expects that on
579
- // assignment, the target CoW pointer is either initialized or nullptr
570
+ ffi_methods ! { type sys:: GDExtensionTypePtr = * mut Opaque ; .. }
580
571
572
+ unsafe fn from_sys_init_default ( init_fn : impl FnOnce ( sys:: GDExtensionTypePtr ) ) -> Self {
581
573
let mut result = Self :: default ( ) ;
582
574
init_fn ( result. sys_mut ( ) ) ;
583
575
result
@@ -598,28 +590,25 @@ impl<T: VariantMetadata> fmt::Debug for TypedArray<T> {
598
590
/// [`Array::duplicate_deep()`].
599
591
impl < T : VariantMetadata > Share for TypedArray < T > {
600
592
fn share ( & self ) -> Self {
601
- unsafe {
602
- Self :: from_sys_init ( |self_ptr| {
593
+ let array = unsafe {
594
+ Self :: from_sys_init_default ( |self_ptr| {
603
595
let ctor = :: godot_ffi:: builtin_fn!( array_construct_copy) ;
604
596
let args = [ self . sys_const ( ) ] ;
605
597
ctor ( self_ptr, args. as_ptr ( ) ) ;
606
598
} )
607
- }
599
+ } ;
600
+ array. with_checked_type ( )
608
601
}
609
602
}
610
603
611
604
impl < T : VariantMetadata > Default for TypedArray < T > {
612
605
#[ inline]
613
606
fn default ( ) -> Self {
614
- // Note: can't use from_sys_init(), as that calls the default constructor
615
- // (because most assignments expect initialized target type)
616
- let mut uninit = std:: mem:: MaybeUninit :: < TypedArray < T > > :: uninit ( ) ;
617
607
let mut array = unsafe {
618
- let self_ptr = ( * uninit. as_mut_ptr ( ) ) . sys_mut ( ) ;
619
- sys:: builtin_call! {
620
- array_construct_default( self_ptr, std:: ptr:: null_mut( ) )
621
- } ;
622
- uninit. assume_init ( )
608
+ Self :: from_sys_init ( |self_ptr| {
609
+ let ctor = :: godot_ffi:: builtin_fn!( array_construct_default) ;
610
+ ctor ( self_ptr, std:: ptr:: null_mut ( ) )
611
+ } )
623
612
} ;
624
613
array. init_inner_type ( ) ;
625
614
array
@@ -661,14 +650,14 @@ impl<T: VariantMetadata> FromVariant for TypedArray<T> {
661
650
if variant. get_type ( ) != Self :: variant_type ( ) {
662
651
return Err ( VariantConversionError ) ;
663
652
}
664
- let result = unsafe {
665
- Self :: from_sys_init ( |self_ptr| {
653
+ let array = unsafe {
654
+ Self :: from_sys_init_default ( |self_ptr| {
666
655
let array_from_variant = sys:: builtin_fn!( array_from_variant) ;
667
656
array_from_variant ( self_ptr, variant. var_sys ( ) ) ;
668
657
} )
669
658
} ;
670
659
671
- Ok ( result )
660
+ Ok ( array . with_checked_type ( ) )
672
661
}
673
662
}
674
663
@@ -773,7 +762,7 @@ impl<T: VariantMetadata> PartialEq for TypedArray<T> {
773
762
let mut result = false ;
774
763
sys:: builtin_call! {
775
764
array_operator_equal( self . sys( ) , other. sys( ) , result. sys_mut( ) )
776
- } ;
765
+ }
777
766
result
778
767
}
779
768
}
0 commit comments