Skip to content

Commit a871a4b

Browse files
committed
std: Rename thread::catch_panic to panic::recover
This commit is an implementation of [RFC 1236][rfc] which renames the `thread::catch_panic` function to `panic::recover` while also removing the `Send` bound (but leaving the `'static` bound). [rfc]: rust-lang/rfcs#1236 cc rust-lang#27719
1 parent 8dba06a commit a871a4b

File tree

6 files changed

+74
-27
lines changed

6 files changed

+74
-27
lines changed

src/libstd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ pub mod fs;
365365
pub mod io;
366366
pub mod net;
367367
pub mod os;
368+
pub mod panic;
368369
pub mod path;
369370
pub mod process;
370371
pub mod sync;

src/libstd/panic.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Panic support in the standard library
12+
13+
#![unstable(feature = "std_panic", reason = "module recently added",
14+
issue = "27719")]
15+
16+
use thread::Result;
17+
18+
/// Invokes a closure, capturing the cause of panic if one occurs.
19+
///
20+
/// This function will return `Ok` with the closure's result if the closure
21+
/// does not panic, and will return `Err(cause)` if the closure panics. The
22+
/// `cause` returned is the object with which panic was originally invoked.
23+
///
24+
/// It is currently undefined behavior to unwind from Rust code into foreign
25+
/// code, so this function is particularly useful when Rust is called from
26+
/// another language (normally C). This can run arbitrary Rust code, capturing a
27+
/// panic and allowing a graceful handling of the error.
28+
///
29+
/// It is **not** recommended to use this function for a general try/catch
30+
/// mechanism. The `Result` type is more appropriate to use for functions that
31+
/// can fail on a regular basis.
32+
///
33+
/// The closure provided is required to adhere to the `'static` bound to ensure
34+
/// that it cannot reference data in the parent stack frame, mitigating problems
35+
/// with exception safety.
36+
///
37+
/// # Examples
38+
///
39+
/// ```
40+
/// #![feature(recover, std_panic)]
41+
///
42+
/// use std::panic;
43+
///
44+
/// let result = panic::recover(|| {
45+
/// println!("hello!");
46+
/// });
47+
/// assert!(result.is_ok());
48+
///
49+
/// let result = panic::recover(|| {
50+
/// panic!("oh no!");
51+
/// });
52+
/// assert!(result.is_err());
53+
/// ```
54+
#[unstable(feature = "recover", reason = "recent API addition",
55+
issue = "27719")]
56+
pub fn recover<F, R>(f: F) -> Result<R> where F: FnOnce() -> R + 'static {
57+
let mut result = None;
58+
unsafe {
59+
let result = &mut result;
60+
try!(::rt::unwind::try(move || *result = Some(f())))
61+
}
62+
Ok(result.unwrap())
63+
}

src/libstd/rt/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
issue = "0")]
2323
#![allow(missing_docs)]
2424

25+
use panic;
2526
use prelude::v1::*;
2627
use sys;
27-
use thread;
2828

2929
// Reexport some of our utilities which are expected by other crates.
3030
pub use self::util::min_stack;
@@ -96,7 +96,7 @@ fn lang_start(main: *const u8, argc: isize, argv: *const *const u8) -> isize {
9696
args::init(argc, argv);
9797

9898
// And finally, let's run some code!
99-
let res = thread::catch_panic(mem::transmute::<_, fn()>(main));
99+
let res = panic::recover(mem::transmute::<_, fn()>(main));
100100
cleanup();
101101
res.is_err()
102102
};

src/libstd/thread/mod.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -357,26 +357,9 @@ pub fn panicking() -> bool {
357357
/// with exception safety. Furthermore, a `Send` bound is also required,
358358
/// providing the same safety guarantees as `thread::spawn` (ensuring the
359359
/// closure is properly isolated from the parent).
360-
///
361-
/// # Examples
362-
///
363-
/// ```
364-
/// #![feature(catch_panic)]
365-
///
366-
/// use std::thread;
367-
///
368-
/// let result = thread::catch_panic(|| {
369-
/// println!("hello!");
370-
/// });
371-
/// assert!(result.is_ok());
372-
///
373-
/// let result = thread::catch_panic(|| {
374-
/// panic!("oh no!");
375-
/// });
376-
/// assert!(result.is_err());
377-
/// ```
378360
#[unstable(feature = "catch_panic", reason = "recent API addition",
379361
issue = "27719")]
362+
#[deprecated(since = "1.4.0", reason = "renamed to std::panic::recover")]
380363
pub fn catch_panic<F, R>(f: F) -> Result<R>
381364
where F: FnOnce() -> R + Send + 'static
382365
{

src/test/run-pass/binary-heap-panic-safe.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(std_misc, collections, catch_panic, rand, sync_poison)]
11+
#![feature(collections, recover, std_panic, rand)]
1212

1313
use std::__rand::{thread_rng, Rng};
14-
use std::thread;
14+
use std::panic;
1515

1616
use std::collections::BinaryHeap;
1717
use std::cmp;
@@ -74,7 +74,7 @@ fn test_integrity() {
7474

7575

7676
// push the panicking item to the heap and catch the panic
77-
let thread_result = thread::catch_panic(move || {
77+
let thread_result = panic::recover(move || {
7878
heap.lock().unwrap().push(panic_item);
7979
});
8080
assert!(thread_result.is_err());

src/test/run-pass/running-with-no-runtime.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(catch_panic, start)]
11+
#![feature(std_panic, recover, start)]
1212

1313
use std::ffi::CStr;
1414
use std::process::{Command, Output};
15-
use std::thread;
15+
use std::panic;
1616
use std::str;
1717

1818
#[start]
@@ -22,8 +22,8 @@ fn start(argc: isize, argv: *const *const u8) -> isize {
2222
match **argv.offset(1) as char {
2323
'1' => {}
2424
'2' => println!("foo"),
25-
'3' => assert!(thread::catch_panic(|| {}).is_ok()),
26-
'4' => assert!(thread::catch_panic(|| panic!()).is_err()),
25+
'3' => assert!(panic::recover(|| {}).is_ok()),
26+
'4' => assert!(panic::recover(|| panic!()).is_err()),
2727
'5' => assert!(Command::new("test").spawn().is_err()),
2828
_ => panic!()
2929
}

0 commit comments

Comments
 (0)