@@ -319,7 +319,7 @@ impl<T: ArrayElement> Array<T> {
319319
320320 /// Clears the array, removing all elements.
321321 pub fn clear ( & mut self ) {
322- self . debug_ensure_mutable ( ) ;
322+ self . balanced_ensure_mutable ( ) ;
323323
324324 // SAFETY: No new values are written to the array, we only remove values from the array.
325325 unsafe { self . as_inner_mut ( ) } . clear ( ) ;
@@ -331,7 +331,7 @@ impl<T: ArrayElement> Array<T> {
331331 ///
332332 /// If `index` is out of bounds.
333333 pub fn set ( & mut self , index : usize , value : impl AsArg < T > ) {
334- self . debug_ensure_mutable ( ) ;
334+ self . balanced_ensure_mutable ( ) ;
335335
336336 let ptr_mut = self . ptr_mut ( index) ;
337337
@@ -348,7 +348,7 @@ impl<T: ArrayElement> Array<T> {
348348 #[ doc( alias = "append" ) ]
349349 #[ doc( alias = "push_back" ) ]
350350 pub fn push ( & mut self , value : impl AsArg < T > ) {
351- self . debug_ensure_mutable ( ) ;
351+ self . balanced_ensure_mutable ( ) ;
352352
353353 meta:: arg_into_ref!( value: T ) ;
354354
@@ -362,7 +362,7 @@ impl<T: ArrayElement> Array<T> {
362362 /// On large arrays, this method is much slower than [`push()`][Self::push], as it will move all the array's elements.
363363 /// The larger the array, the slower `push_front()` will be.
364364 pub fn push_front ( & mut self , value : impl AsArg < T > ) {
365- self . debug_ensure_mutable ( ) ;
365+ self . balanced_ensure_mutable ( ) ;
366366
367367 meta:: arg_into_ref!( value: T ) ;
368368
@@ -376,7 +376,7 @@ impl<T: ArrayElement> Array<T> {
376376 /// _Godot equivalent: `pop_back`_
377377 #[ doc( alias = "pop_back" ) ]
378378 pub fn pop ( & mut self ) -> Option < T > {
379- self . debug_ensure_mutable ( ) ;
379+ self . balanced_ensure_mutable ( ) ;
380380
381381 ( !self . is_empty ( ) ) . then ( || {
382382 // SAFETY: We do not write any values to the array, we just remove one.
@@ -390,7 +390,7 @@ impl<T: ArrayElement> Array<T> {
390390 /// Note: On large arrays, this method is much slower than `pop()` as it will move all the
391391 /// array's elements. The larger the array, the slower `pop_front()` will be.
392392 pub fn pop_front ( & mut self ) -> Option < T > {
393- self . debug_ensure_mutable ( ) ;
393+ self . balanced_ensure_mutable ( ) ;
394394
395395 ( !self . is_empty ( ) ) . then ( || {
396396 // SAFETY: We do not write any values to the array, we just remove one.
@@ -407,7 +407,7 @@ impl<T: ArrayElement> Array<T> {
407407 /// # Panics
408408 /// If `index > len()`.
409409 pub fn insert ( & mut self , index : usize , value : impl AsArg < T > ) {
410- self . debug_ensure_mutable ( ) ;
410+ self . balanced_ensure_mutable ( ) ;
411411
412412 let len = self . len ( ) ;
413413 assert ! (
@@ -431,8 +431,7 @@ impl<T: ArrayElement> Array<T> {
431431 /// If `index` is out of bounds.
432432 #[ doc( alias = "pop_at" ) ]
433433 pub fn remove ( & mut self , index : usize ) -> T {
434- self . debug_ensure_mutable ( ) ;
435-
434+ self . balanced_ensure_mutable ( ) ;
436435 self . check_bounds ( index) ;
437436
438437 // SAFETY: We do not write any values to the array, we just remove one.
@@ -447,7 +446,7 @@ impl<T: ArrayElement> Array<T> {
447446 /// On large arrays, this method is much slower than [`pop()`][Self::pop], as it will move all the array's
448447 /// elements after the removed element.
449448 pub fn erase ( & mut self , value : impl AsArg < T > ) {
450- self . debug_ensure_mutable ( ) ;
449+ self . balanced_ensure_mutable ( ) ;
451450
452451 meta:: arg_into_ref!( value: T ) ;
453452
@@ -458,7 +457,7 @@ impl<T: ArrayElement> Array<T> {
458457 /// Assigns the given value to all elements in the array. This can be used together with
459458 /// `resize` to create an array with a given size and initialized elements.
460459 pub fn fill ( & mut self , value : impl AsArg < T > ) {
461- self . debug_ensure_mutable ( ) ;
460+ self . balanced_ensure_mutable ( ) ;
462461
463462 meta:: arg_into_ref!( value: T ) ;
464463
@@ -473,7 +472,7 @@ impl<T: ArrayElement> Array<T> {
473472 ///
474473 /// If you know that the new size is smaller, then consider using [`shrink`](Array::shrink) instead.
475474 pub fn resize ( & mut self , new_size : usize , value : impl AsArg < T > ) {
476- self . debug_ensure_mutable ( ) ;
475+ self . balanced_ensure_mutable ( ) ;
477476
478477 let original_size = self . len ( ) ;
479478
@@ -505,7 +504,7 @@ impl<T: ArrayElement> Array<T> {
505504 /// If you want to increase the size of the array, use [`resize`](Array::resize) instead.
506505 #[ doc( alias = "resize" ) ]
507506 pub fn shrink ( & mut self , new_size : usize ) -> bool {
508- self . debug_ensure_mutable ( ) ;
507+ self . balanced_ensure_mutable ( ) ;
509508
510509 if new_size >= self . len ( ) {
511510 return false ;
@@ -519,7 +518,7 @@ impl<T: ArrayElement> Array<T> {
519518
520519 /// Appends another array at the end of this array. Equivalent of `append_array` in GDScript.
521520 pub fn extend_array ( & mut self , other : & Array < T > ) {
522- self . debug_ensure_mutable ( ) ;
521+ self . balanced_ensure_mutable ( ) ;
523522
524523 // SAFETY: `append_array` will only read values from `other`, and all types can be converted to `Variant`.
525524 let other: & VariantArray = unsafe { other. assume_type_ref :: < Variant > ( ) } ;
@@ -743,7 +742,7 @@ impl<T: ArrayElement> Array<T> {
743742
744743 /// Reverses the order of the elements in the array.
745744 pub fn reverse ( & mut self ) {
746- self . debug_ensure_mutable ( ) ;
745+ self . balanced_ensure_mutable ( ) ;
747746
748747 // SAFETY: We do not write any values that don't already exist in the array, so all values have the correct type.
749748 unsafe { self . as_inner_mut ( ) } . reverse ( ) ;
@@ -760,7 +759,7 @@ impl<T: ArrayElement> Array<T> {
760759 /// _Godot equivalent: `Array.sort()`_
761760 #[ doc( alias = "sort" ) ]
762761 pub fn sort_unstable ( & mut self ) {
763- self . debug_ensure_mutable ( ) ;
762+ self . balanced_ensure_mutable ( ) ;
764763
765764 // SAFETY: We do not write any values that don't already exist in the array, so all values have the correct type.
766765 unsafe { self . as_inner_mut ( ) } . sort ( ) ;
@@ -780,7 +779,7 @@ impl<T: ArrayElement> Array<T> {
780779 where
781780 F : FnMut ( & T , & T ) -> cmp:: Ordering ,
782781 {
783- self . debug_ensure_mutable ( ) ;
782+ self . balanced_ensure_mutable ( ) ;
784783
785784 let godot_comparator = |args : & [ & Variant ] | {
786785 let lhs = T :: from_variant ( args[ 0 ] ) ;
@@ -809,7 +808,7 @@ impl<T: ArrayElement> Array<T> {
809808 /// _Godot equivalent: `Array.sort_custom()`_
810809 #[ doc( alias = "sort_custom" ) ]
811810 pub fn sort_unstable_custom ( & mut self , func : & Callable ) {
812- self . debug_ensure_mutable ( ) ;
811+ self . balanced_ensure_mutable ( ) ;
813812
814813 // SAFETY: We do not write any values that don't already exist in the array, so all values have the correct type.
815814 unsafe { self . as_inner_mut ( ) } . sort_custom ( func) ;
@@ -819,7 +818,7 @@ impl<T: ArrayElement> Array<T> {
819818 /// global random number generator common to methods such as `randi`. Call `randomize` to
820819 /// ensure that a new seed will be used each time if you want non-reproducible shuffling.
821820 pub fn shuffle ( & mut self ) {
822- self . debug_ensure_mutable ( ) ;
821+ self . balanced_ensure_mutable ( ) ;
823822
824823 // SAFETY: We do not write any values that don't already exist in the array, so all values have the correct type.
825824 unsafe { self . as_inner_mut ( ) } . shuffle ( ) ;
@@ -859,10 +858,10 @@ impl<T: ArrayElement> Array<T> {
859858
860859 /// Best-effort mutability check.
861860 ///
862- /// # Panics
863- /// In Debug mode, if the array is marked as read-only.
864- fn debug_ensure_mutable ( & self ) {
865- debug_assert ! (
861+ /// # Panics (safeguards-balanced)
862+ /// If the array is marked as read-only.
863+ fn balanced_ensure_mutable ( & self ) {
864+ sys :: balanced_assert !(
866865 !self . is_read_only( ) ,
867866 "mutating operation on read-only array"
868867 ) ;
@@ -873,6 +872,7 @@ impl<T: ArrayElement> Array<T> {
873872 /// # Panics
874873 /// If `index` is out of bounds.
875874 fn check_bounds ( & self , index : usize ) {
875+ // Safety-relevant; explicitly *don't* use safeguards-dependent validation.
876876 let len = self . len ( ) ;
877877 assert ! (
878878 index < len,
@@ -1049,8 +1049,8 @@ impl<T: ArrayElement> Array<T> {
10491049 /// # Safety
10501050 /// Must only be called once, directly after creation.
10511051 unsafe fn init_inner_type ( & mut self ) {
1052- debug_assert ! ( self . is_empty( ) ) ;
1053- debug_assert ! (
1052+ sys :: strict_assert !( self . is_empty( ) ) ;
1053+ sys :: strict_assert !(
10541054 self . cached_element_type. get( ) . is_none( ) ,
10551055 "init_inner_type() called twice"
10561056 ) ;
0 commit comments