Rollup merge of #80169 - frewsxcv:frewsxcv-docs-fix, r=jyn514

Recommend panic::resume_unwind instead of panicking.

Fixes https://github.com/rust-lang/rust/issues/79950.
This commit is contained in:
Mara Bos 2021-01-14 17:59:57 +00:00 committed by GitHub
commit 930371b3ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1186,32 +1186,37 @@ impl fmt::Debug for Thread {
/// the [`Error`](crate::error::Error) trait. /// the [`Error`](crate::error::Error) trait.
/// ///
/// Thus, a sensible way to handle a thread panic is to either: /// Thus, a sensible way to handle a thread panic is to either:
/// 1. `unwrap` the `Result<T>`, propagating the panic ///
/// 1. propagate the panic with [`std::panic::resume_unwind`]
/// 2. or in case the thread is intended to be a subsystem boundary /// 2. or in case the thread is intended to be a subsystem boundary
/// that is supposed to isolate system-level failures, /// that is supposed to isolate system-level failures,
/// match on the `Err` variant and handle the panic in an appropriate way. /// match on the `Err` variant and handle the panic in an appropriate way
/// ///
/// A thread that completes without panicking is considered to exit successfully. /// A thread that completes without panicking is considered to exit successfully.
/// ///
/// # Examples /// # Examples
/// ///
/// Matching on the result of a joined thread:
///
/// ```no_run /// ```no_run
/// use std::thread; /// use std::{fs, thread, panic};
/// use std::fs;
/// ///
/// fn copy_in_thread() -> thread::Result<()> { /// fn copy_in_thread() -> thread::Result<()> {
/// thread::spawn(move || { fs::copy("foo.txt", "bar.txt").unwrap(); }).join() /// thread::spawn(|| {
/// fs::copy("foo.txt", "bar.txt").unwrap();
/// }).join()
/// } /// }
/// ///
/// fn main() { /// fn main() {
/// match copy_in_thread() { /// match copy_in_thread() {
/// Ok(_) => println!("this is fine"), /// Ok(_) => println!("copy succeeded"),
/// Err(_) => println!("thread panicked"), /// Err(e) => panic::resume_unwind(e),
/// } /// }
/// } /// }
/// ``` /// ```
/// ///
/// [`Result`]: crate::result::Result /// [`Result`]: crate::result::Result
/// [`std::panic::resume_unwind`]: crate::panic::resume_unwind
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub type Result<T> = crate::result::Result<T, Box<dyn Any + Send + 'static>>; pub type Result<T> = crate::result::Result<T, Box<dyn Any + Send + 'static>>;