@@ -533,12 +533,11 @@ impl<T: ArrayElement> Array<T> {
533
533
/// To create a deep copy, use [`duplicate_deep()`][Self::duplicate_deep] instead.
534
534
/// To create a new reference to the same array data, use [`clone()`][Clone::clone].
535
535
pub fn duplicate_shallow ( & self ) -> Self {
536
- // SAFETY: We never write to the duplicated array , and all values read are read as `Variant`.
537
- let duplicate: VariantArray = unsafe { self . as_inner ( ) . duplicate ( false ) } ;
536
+ // SAFETY: duplicate() returns a typed array with the same type as Self , and all values are taken from `self` so have the right type
537
+ let duplicate: Self = unsafe { self . as_inner ( ) . duplicate ( false ) } ;
538
538
539
- // SAFETY: duplicate() returns a typed array with the same type as Self, and all values are taken from `self` so have the right type.
540
- let result = unsafe { duplicate. assume_type ( ) } ;
541
- result. with_cache ( self )
539
+ // Note: cache is being set while initializing the duplicate as a return value for above call.
540
+ duplicate
542
541
}
543
542
544
543
/// Returns a deep copy of the array. All nested arrays and dictionaries are duplicated and
@@ -548,12 +547,11 @@ impl<T: ArrayElement> Array<T> {
548
547
/// To create a shallow copy, use [`duplicate_shallow()`][Self::duplicate_shallow] instead.
549
548
/// To create a new reference to the same array data, use [`clone()`][Clone::clone].
550
549
pub fn duplicate_deep ( & self ) -> Self {
551
- // SAFETY: We never write to the duplicated array , and all values read are read as `Variant`.
552
- let duplicate: VariantArray = unsafe { self . as_inner ( ) . duplicate ( true ) } ;
550
+ // SAFETY: duplicate() returns a typed array with the same type as Self , and all values are taken from `self` so have the right type
551
+ let duplicate: Self = unsafe { self . as_inner ( ) . duplicate ( true ) } ;
553
552
554
- // SAFETY: duplicate() returns a typed array with the same type as Self, and all values are taken from `self` so have the right type.
555
- let result = unsafe { duplicate. assume_type ( ) } ;
556
- result. with_cache ( self )
553
+ // Note: cache is being set while initializing the duplicate as a return value for above call.
554
+ duplicate
557
555
}
558
556
559
557
/// Returns a sub-range `begin..end` as a new `Array`.
@@ -588,13 +586,10 @@ impl<T: ArrayElement> Array<T> {
588
586
let ( begin, end) = range. signed ( ) ;
589
587
let end = end. unwrap_or ( i32:: MAX as i64 ) ;
590
588
591
- // SAFETY: The type of the array is `T` and we convert the returned array to an `Array<T>` immediately.
592
- let subarray: VariantArray =
593
- unsafe { self . as_inner ( ) . slice ( begin, end, step as i64 , deep) } ;
589
+ // SAFETY: slice() returns a typed array with the same type as Self, and all values are taken from `self` so have the right type.
590
+ let subarray: Self = unsafe { self . as_inner ( ) . slice ( begin, end, step as i64 , deep) } ;
594
591
595
- // SAFETY: slice() returns a typed array with the same type as Self.
596
- let result = unsafe { subarray. assume_type ( ) } ;
597
- result. with_cache ( self )
592
+ subarray
598
593
}
599
594
600
595
/// Returns an iterator over the elements of the `Array`. Note that this takes the array
@@ -950,8 +945,7 @@ impl<T: ArrayElement> Array<T> {
950
945
}
951
946
}
952
947
953
- /// Changes the generic type on this array, without changing its contents. Needed for API
954
- /// functions that return a variant array even though we know its type, and for API functions
948
+ /// Changes the generic type on this array, without changing its contents. Needed for API functions
955
949
/// that take a variant array even though we want to pass a typed one.
956
950
///
957
951
/// # Safety
@@ -966,13 +960,6 @@ impl<T: ArrayElement> Array<T> {
966
960
/// Note also that any `GodotType` can be written to a `Variant` array.
967
961
///
968
962
/// In the current implementation, both cases will produce a panic rather than undefined behavior, but this should not be relied upon.
969
- unsafe fn assume_type < U : ArrayElement > ( self ) -> Array < U > {
970
- // The memory layout of `Array<T>` does not depend on `T`.
971
- std:: mem:: transmute :: < Array < T > , Array < U > > ( self )
972
- }
973
-
974
- /// # Safety
975
- /// See [`assume_type`](Self::assume_type).
976
963
unsafe fn assume_type_ref < U : ArrayElement > ( & self ) -> & Array < U > {
977
964
// The memory layout of `Array<T>` does not depend on `T`.
978
965
std:: mem:: transmute :: < & Array < T > , & Array < U > > ( self )
0 commit comments