about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2020-10-31 09:49:28 +0100
committerGitHub <noreply@github.com>2020-10-31 09:49:28 +0100
commit76b8b00b4ff09b958e9ebcaa851f5dcb7b827f8a (patch)
tree8c0a906e51338ebc3c57322d0a1869eefbd80497
parent3478d7c3607ffbba629af2265c01002771e48c18 (diff)
parentb48fee010c92dde304154ba45c0e41d396e60568 (diff)
downloadrust-76b8b00b4ff09b958e9ebcaa851f5dcb7b827f8a.tar.gz
rust-76b8b00b4ff09b958e9ebcaa851f5dcb7b827f8a.zip
Rollup merge of #74622 - fusion-engineering-forks:panic-box, r=KodrAus
Add std::panic::panic_any.

The discussion of #67984 lead to the conclusion that there should be a macro or function separate from `std::panic!()` for throwing arbitrary payloads, to make it possible to deprecate or disallow (in edition 2021) `std::panic!(arbitrary_payload)`.

Alternative names:

- `panic_with!(..)`
- ~~`start_unwind(..)`~~ (panicking doesn't always unwind)
- `throw!(..)`
- `panic_throwing!(..)`
- `panic_with_value(..)`
- `panic_value(..)`
- `panic_with(..)`
- `panic_box(..)`
- `panic(..)`

The equivalent (private, unstable) function in `libstd` is called `std::panicking::begin_panic`.

I suggest `panic_any`, because it allows for any (`Any + Send`) type.

_Tracking issue: #78500_
-rw-r--r--library/std/src/panic.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs
index 4281867314c..d18b94b6c1a 100644
--- a/library/std/src/panic.rs
+++ b/library/std/src/panic.rs
@@ -23,6 +23,20 @@ pub use crate::panicking::{set_hook, take_hook};
 #[stable(feature = "panic_hooks", since = "1.10.0")]
 pub use core::panic::{Location, PanicInfo};
 
+/// Panic the current thread with the given message as the panic payload.
+///
+/// The message can be of any (`Any + Send`) type, not just strings.
+///
+/// The message is wrapped in a `Box<'static + Any + Send>`, which can be
+/// accessed later using [`PanicInfo::payload`].
+///
+/// See the [`panic!`] macro for more information about panicking.
+#[unstable(feature = "panic_any", issue = "78500")]
+#[inline]
+pub fn panic_any<M: Any + Send>(msg: M) -> ! {
+    crate::panicking::begin_panic(msg);
+}
+
 /// A marker trait which represents "panic safe" types in Rust.
 ///
 /// This trait is implemented by default for many types and behaves similarly in