-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recommend panic::resume_unwind instead of panicking. #80169
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1186,32 +1186,37 @@ impl fmt::Debug for Thread { | |
/// the [`Error`](crate::error::Error) trait. | ||
/// | ||
/// 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 | ||
/// 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. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Matching on the result of a joined thread: | ||
/// | ||
Comment on lines
+1199
to
+1200
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to mean that the example is going to show case (2) above. However, the example below is changed from case (2) to case (1). Which one is correct/intended? Or should there be two examples? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously it was just demonstrating matching. Now it is demonstrating matching and the panic_unwind. |
||
/// ```no_run | ||
/// use std::thread; | ||
/// use std::fs; | ||
/// use std::{fs, thread, panic}; | ||
/// | ||
/// 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() { | ||
/// match copy_in_thread() { | ||
/// Ok(_) => println!("this is fine"), | ||
/// Err(_) => println!("thread panicked"), | ||
/// Ok(_) => println!("copy succeeded"), | ||
/// Err(e) => panic::resume_unwind(e), | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// [`Result`]: crate::result::Result | ||
/// [`std::panic::resume_unwind`]: crate::panic::resume_unwind | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub type Result<T> = crate::result::Result<T, Box<dyn Any + Send + 'static>>; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove the period? It's a complete sentence even though it is formatted with an enumeration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The statement on its own is not a complete sentence