File tree Expand file tree Collapse file tree 3 files changed +44
-0
lines changed Expand file tree Collapse file tree 3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ use crate::dimension::broadcast::co_broadcast;
2626use crate :: error:: { self , ErrorKind , ShapeError , from_kind} ;
2727use crate :: math_cell:: MathCell ;
2828use crate :: itertools:: zip;
29+ use crate :: low_level_util:: AbortIfPanic ;
2930use crate :: zip:: { IntoNdProducer , Zip } ;
3031use crate :: AxisDescription ;
3132
@@ -2477,6 +2478,7 @@ where
24772478 // dst = elt;
24782479 // }
24792480 //
2481+ let guard = AbortIfPanic ( & "remove_index: temporarily moving out of owned value" ) ;
24802482 let mut slot = MaybeUninit :: < A > :: uninit ( ) ;
24812483 unsafe {
24822484 slot. as_mut_ptr ( ) . copy_from_nonoverlapping ( dst, 1 ) ;
@@ -2486,6 +2488,7 @@ where
24862488 }
24872489 ( dst as * mut A ) . copy_from_nonoverlapping ( slot. as_ptr ( ) , 1 ) ;
24882490 }
2491+ guard. defuse ( ) ;
24892492 } ) ;
24902493 // then slice the axis in place to cut out the removed final element
24912494 self . slice_axis_inplace ( axis, Slice :: new ( 0 , Some ( -1 ) , 1 ) ) ;
Original file line number Diff line number Diff line change @@ -205,6 +205,7 @@ mod shape_builder;
205205mod slice;
206206mod split_at;
207207mod stacking;
208+ mod low_level_util;
208209#[ macro_use]
209210mod zip;
210211
Original file line number Diff line number Diff line change 1+ // Copyright 2021 bluss and ndarray developers.
2+ //
3+ // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+ // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+ // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+ // option. This file may not be copied, modified, or distributed
7+ // except according to those terms.
8+
9+
10+ /// Guard value that will abort if it is dropped.
11+ /// To defuse, this value must be forgotten before the end of the scope.
12+ ///
13+ /// The string value is added to the message printed if aborting.
14+ #[ must_use]
15+ pub ( crate ) struct AbortIfPanic ( pub ( crate ) & ' static & ' static str ) ;
16+
17+ impl AbortIfPanic {
18+ /// Defuse the AbortIfPanic guard. This *must* be done when finished.
19+ #[ inline]
20+ pub ( crate ) fn defuse ( self ) {
21+ std:: mem:: forget ( self ) ;
22+ }
23+ }
24+
25+ impl Drop for AbortIfPanic {
26+ // The compiler should be able to remove this, if it can see through that there
27+ // is no panic in the code section.
28+ fn drop ( & mut self ) {
29+ #[ cfg( feature="std" ) ]
30+ {
31+ eprintln ! ( "ndarray: panic in no-panic section, aborting: {}" , self . 0 ) ;
32+ std:: process:: abort ( )
33+ }
34+ #[ cfg( not( feature="std" ) ) ]
35+ {
36+ // no-std uses panic-in-panic (should abort)
37+ panic ! ( "ndarray: panic in no-panic section, bailing out: {}" , self . 0 ) ;
38+ }
39+ }
40+ }
You can’t perform that action at this time.
0 commit comments