#[used] attribute
(For an explanation of what this feature does, read the commit message)
I'd like to propose landing this as an experimental feature (experimental as in:
no clear stabilization path -- like `asm!`, `#[linkage]`) as it's low
maintenance (I think) and relevant to the "Usage in resource-constrained
environments" exploration area.
The main use case I see is running code before `main`. This could be used, for
instance, to cheaply initialize an allocator before `main` where the alternative
is to use `lazy_static` to initialize the allocator on its first use which it's
more expensive (atomics) and doesn't work on ARM Cortex-M0 microcontrollers (no
`AtomicUsize` on that platform)
Here's a `std` example of that:
``` rust
unsafe extern "C" fn before_main_1() {
println!("Hello");
}
unsafe extern "C" fn before_main_2() {
println!("World");
}
#[link_section = ".init_arary"]
#[used]
static INIT_ARRAY: [unsafe extern "C" fn(); 2] = [before_main_1, before_main_2];
fn main() {
println!("Goodbye");
}
```
```
$ rustc -C lto -C opt-level=3 before_main.rs
$ ./before_main
Hello
World
Goodbye
```
In general, this pattern could be used to let *dependencies* run code before
`main` (which sounds like it could go very wrong in some cases). There are
probably other use cases; I hope that the people I have cc-ed can comment on
those.
Note that I'm personally unsure if the above pattern is something we want to
promote / allow and that's why I'm proposing this feature as experimental. If
this leads to more footguns than benefits then we can just axe the feature.
cc @nikomatsakis ^ I know you have some thoughts on having a process for
experimental features though I'm fine with writing an RFC before landing this.
- `dead_code` lint will have to be updated to special case `#[used]` symbols.
- Should we extend `#[used]` to work on non-generic functions?
cc rust-lang/rfcs#1002
cc rust-lang/rfcs#1459
cc @dpc @JinShil
|
||
|---|---|---|
| .. | ||
| book@a2c56870d4 | ||
| nomicon@616b98444f | ||
| reference@acedc32cac | ||
| unstable-book | ||
| complement-design-faq.md | ||
| complement-lang-faq.md | ||
| complement-project-faq.md | ||
| favicon.inc | ||
| footer.inc | ||
| full-toc.inc | ||
| grammar.md | ||
| guide-crates.md | ||
| guide-error-handling.md | ||
| guide-ffi.md | ||
| guide-macros.md | ||
| guide-ownership.md | ||
| guide-plugins.md | ||
| guide-pointers.md | ||
| guide-strings.md | ||
| guide-tasks.md | ||
| guide-testing.md | ||
| guide-unsafe.md | ||
| guide.md | ||
| index.md | ||
| intro.md | ||
| not_found.md | ||
| README.md | ||
| reference.md | ||
| rust.css | ||
| rust.md | ||
| rustc-ux-guidelines.md | ||
| rustdoc.md | ||
| tutorial.md | ||
| version_info.html.template | ||
Rust documentations
Building
To generate all the docs, follow the "Building Documentation" instructions in the README in the root of the repository. This will convert the distributed Markdown docs to HTML and generate HTML doc for the books, 'std' and 'extra' libraries.
To generate HTML documentation from one source file/crate, do something like:
rustdoc --output html-doc/ --output-format html ../src/libstd/path.rs
(This, of course, requires a working build of the rustdoc tool.)
Additional notes
To generate an HTML version of a doc from Markdown manually, you can do something like:
rustdoc reference.md
(reference.md being the Rust Reference Manual.)
An overview of how to use the rustdoc command is available in the docs.
Further details are available from the command line by with rustdoc --help.