Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -56,3 +56,5 @@
* [Final Code](vec-final.md)
* [Implementing Arc and Mutex](arc-and-mutex.md)
* [FFI](ffi.md)
* [Beneath `std`](beneath-std.md)
* [#[panic_handler]](panic-handler.md)
10 changes: 10 additions & 0 deletions src/beneath-std.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Beneath std

This section documents (or will document) features that are provided by the standard library and
that `#![no_std]` developers have to deal with (i.e. provide) to build `#![no_std]` binary crates. A
(likely incomplete) list of such features is shown below:

- #[lang = "eh_personality"]
- #[lang = "start"]
- #[lang = "termination"]
- #[panic_implementation]
65 changes: 65 additions & 0 deletions src/panic-handler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## #[panic_handler]

`#[panic_handler]` is used to define the behavior of `panic!` in `#![no_std]` applications.
The `#[panic_handler]` attribute must be applied to a function with signature `fn(&PanicInfo)
-> !` and such function must appear *once* in the dependency graph of a binary / dylib / cdylib
crate. The API of `PanicInfo` can be found in the [API docs].

[API docs]: https://doc.rust-lang.org/nightly/core/panic/struct.PanicInfo.html
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a relative link so it works offline

../core/panic/struct.PanicInfo.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!


Given that `#![no_std]` applications have no *standard* output and that some `#![no_std]`
applications, e.g. embedded applications, need different panicking behaviors for development and for
release it can be helpful to have panic crates, crate that only contain a `#[panic_handler]`.
This way applications can easily swap the panicking behavior by simply linking to a different panic
crate.

Below is shown an example where an application has a different panicking behavior depending on
whether is compiled using the dev profile (`cargo build`) or using the release profile (`cargo build
--release`).

``` rust
// crate: panic-semihosting -- log panic message to the host stderr using semihosting

#![no_std]

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

// logs "panicked at '$reason', src/main.rs:27:4" to the host stderr
writeln!(host_stderr, "{}", info).ok();

loop {}
}
```

``` rust
// crate: panic-halt -- halt the thread on panic; messages are discarded

#![no_std]

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
loop {}
}
```

``` rust
// crate: app

#![no_std]

// dev profile
#[cfg(debug_assertions)]
extern crate panic_semihosting;

// release profile
#[cfg(not(debug_assertions))]
extern crate panic_halt;

// omitted: other `extern crate`s

fn main() {
// ..
}
```