Skip to content

Commit 1242944

Browse files
japaricehuss
authored andcommitted
document #[panic_implementation]
1 parent 2f9a1f2 commit 1242944

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
- [Constant Evaluation](const_eval.md)
113113

114114
- [Application Binary Interface](abi.md)
115+
- [Beneath `std`](beneath-std.md)
116+
- [#[panic_implementation]](panic-implementation.md)
115117

116118
[Appendix: Influences](influences.md)
117119

src/beneath-std.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Beneath `std`
2+
3+
This section documents (or will document) features that are provided by the standard library and
4+
that `#![no_std]` developers have to deal with (i.e. provide) to build `#![no_std]` binary crates. A
5+
(likely incomplete) list of such features is shown below:
6+
7+
- #[lang = "eh_personality"]
8+
- #[lang = "start"]
9+
- #[lang = "termination"]
10+
- #[panic_implementation]

src/panic-implementation.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## #[panic_implementation]
2+
3+
The `#[panic_implementation]` attribute can only be applied to a function with signature
4+
`fn(&PanicInfo) -> !`. The function marked with this attribute defines the behavior of `panic!` in
5+
`#![no_std]` applications. There must be a *single* `#[panic_implementation]` function in the
6+
dependency graph of a binary / dylib / cdylib crate.
7+
8+
The `PanicInfo` struct contains information about the location of the panic. The API of `PanicInfo`
9+
can be found in the [API docs]. As of 1.28-beta (2018-06-21) there's a difference between
10+
`core::panic!` and `std::panic!`: the former doesn't accept non-string payloads -- that is
11+
`core::panic!(42)` is not accepted. In the future `core::panic!` may gain support for non-string
12+
payloads. The implementation of `core::panic!` can be found in `src/libcore/{macros,panicking}.rs`
13+
14+
[API docs]: https://doc.rust-lang.org/nightly/core/panic/struct.PanicInfo.html
15+
16+
Below is shown a `#[panic_implementation]` that logs the panic message and then aborts the program.
17+
18+
``` rust
19+
#![feature(core_intrinsics)]
20+
#![feature(panic_implementation)]
21+
#![no_std]
22+
23+
use core::intrinsics;
24+
use core::panic::PanicInfo;
25+
26+
#[panic_implementation]
27+
fn panic(info: &PanicInfo) -> ! {
28+
let mut sink = /* .. */;
29+
30+
// logs "panicked at '$reason', src/main.rs:27:4" to some `sink`
31+
let _ = writeln!(sink, "{}", info);
32+
33+
unsafe { intrinsics::abort() }
34+
}
35+
```

0 commit comments

Comments
 (0)