ndarray/low_level_util.rs
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]
15pub(crate) struct AbortIfPanic(pub(crate) &'static &'static str);
16
17impl 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
25impl 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}