Skip to content
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

document #[panic_handler] #362

Merged
merged 6 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@

- [Application Binary Interface](abi.md)

- [The Rust runtime](runtime.md)

[Appendix: Influences](influences.md)

[Appendix: As-yet-undocumented Features](undocumented.md)
Expand Down
2 changes: 2 additions & 0 deletions src/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ which can be used to control type layout.
symbol for this item to its identifier.
- [`used`] - on statics, this forces the compiler to keep the variable in the
output object file.
- [`panic_handler`] — sets the function to handle panics.

### Deprecation

Expand Down Expand Up @@ -632,3 +633,4 @@ pub fn f() {}
[Expression Attributes]: expressions.html#expression-attributes
[`meta` macro fragment specifier]: macros-by-example.html
[`used`]: abi.html#the-used-attribute
[`panic_handler`]: runtime.html#the-panic_handler-attribute
57 changes: 57 additions & 0 deletions src/runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# The Rust runtime

This section documents features that define some aspects of the Rust runtime.

## The `panic_handler` attribute

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.

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

<!-- NOTE(ignore) `mdbook test` doesn't support `no_std` code -->

``` rust, ignore
#![no_std]

use core::fmt::{self, Write};
use core::panic::PanicInfo;

struct Sink {
// ..
# _0: (),
}
#
# impl Sink {
# fn new() -> Sink { Sink { _0: () }}
# }
#
# impl fmt::Write for Sink {
# fn write_str(&mut self, _: &str) -> fmt::Result { Ok(()) }
# }

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

// 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` that
defaults to unwinding the stack but that can be [changed to abort the
process][abort]. The standard library's panic behavior can be modified at
runtime with the [set_hook] function.

[`PanicInfo`]: ../core/panic/struct.PanicInfo.html
[abort]: ../book/ch09-01-unrecoverable-errors-with-panic.html
[attribute]: attributes.html
[set_hook]: ../std/panic/fn.set_hook.html