Skip to content

Commit

Permalink
panic_implementation -> panic_handler; beneath std -> the Rust runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
japaric committed Sep 7, 2018
1 parent 2c9b97d commit 93f140e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
- [Behavior considered undefined](behavior-considered-undefined.md)
- [Behavior not considered unsafe](behavior-not-considered-unsafe.md)

- [Beneath `std`](beneath-std.md)
- [The Rust runtime](runtime.md)

[Appendix: Influences](influences.md)

Expand Down
39 changes: 0 additions & 39 deletions src/beneath-std.md

This file was deleted.

43 changes: 43 additions & 0 deletions src/runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# The Rust runtime

This section documents features that define some aspects of the Rust runtime. A list of such
features is shown below:

- `#[panic_handler]`, the panicking behavior

## `#[panic_handler]`

The `panic_handler` attribute can only be applied to a function with signature
`fn(&PanicInfo) -> !`. The function marked with this attribute defines the behavior of panics. The
[`PanicInfo`] struct contains information about the location of the panic. There must be a single
`panic_handler` function in the dependency graph of a binary, dylib or cdylib crate.

[`PanicInfo`]: https://doc.rust-lang.org/nightly/core/panic/struct.PanicInfo.html

Below is shown a `panic_handler` function that logs the panic message and then halts the
thread.

``` rust
#![no_std]

use core::intrinsics;
use core::panic::PanicInfo;

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
let mut sink = /* .. */;

// logs "panicked at '$reason', src/main.rs:27:4" to some `sink`
let _ = writeln!(sink, "{}", info);

loop {}
}
```

### Standard behavior

The standard library provides an implementation of `panic_handler` than can be
statically customized using the `-C panic` flag. `-C panic=abort` makes panics
abort the process, and `-C panic=unwind` makes panics unwind the panicking
thread. If no panicking behavior is specified using `-C panic` one of these two
behaviors is chosen according to the compilation target.

0 comments on commit 93f140e

Please sign in to comment.