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

Add inline attributes documentation. #11491

Merged
merged 1 commit into from
Jan 12, 2014
Merged
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
34 changes: 28 additions & 6 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ A static item must have a _constant expression_ giving its definition.

Static items must be explicitly typed.
The type may be ```bool```, ```char```, a number, or a type derived from those primitive types.
The derived types are references with the `'static` lifetime,
The derived types are references with the `static` lifetime,
fixed-size arrays, tuples, and structs.

~~~~
Expand Down Expand Up @@ -1730,14 +1730,17 @@ names are effectively reserved. Some significant attributes include:

* The `doc` attribute, for documenting code in-place.
* The `cfg` attribute, for conditional-compilation by build-configuration.
* The `lang` attribute, for custom definitions of traits and functions that are known to the Rust compiler (see [Language items](#language-items)).
* The `link` attribute, for describing linkage metadata for a extern blocks.
* The `crate_id` attribute, for describing the package ID of a crate.
* The `lang` attribute, for custom definitions of traits and functions that are
known to the Rust compiler (see [Language items](#language-items)).
* The `link` attribute, for describing linkage metadata for a extern blocks.
* The `test` attribute, for marking functions as unit tests.
* The `allow`, `warn`, `forbid`, and `deny` attributes, for
controlling lint checks (see [Lint check attributes](#lint-check-attributes)).
* The `deriving` attribute, for automatically generating
implementations of certain traits.
* The `inline` attribute, for expanding functions at caller location (see
[Inline attributes](#inline-attributes)).
* The `static_assert` attribute, for asserting that a static bool is true at compiletime
* The `thread_local` attribute, for defining a `static mut` as a thread-local. Note that this is
only a low-level building block, and is not local to a *task*, nor does it provide safety.
Expand Down Expand Up @@ -1910,6 +1913,25 @@ A complete list of the built-in language items follows:
> **Note:** This list is likely to become out of date. We should auto-generate it
> from `librustc/middle/lang_items.rs`.

### Inline attributes

The inline attribute is used to suggest to the compiler to perform an inline
expansion and place a copy of the function in the caller rather than generating
code to call the function where it is defined.

The compiler automatically inlines functions based on internal heuristics.
Incorrectly inlining functions can actually making the program slower, so it
should be used with care.

`#[inline]` and `#[inline(always)]` always causes the function to be serialized
into crate metadata to allow cross-crate inlining.

There are three different types of inline attributes:

* `#[inline]` hints the compiler to perform an inline expansion.
* `#[inline(always)]` asks the compiler to always perform an inline expansion.
Copy link
Member

Choose a reason for hiding this comment

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

"This can easily cause code bloat and actually make code slower, and so should be used with care."

* `#[inline(never)]` asks the compiler to never perform an inline expansion.

Copy link
Member

Choose a reason for hiding this comment

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

It would be good to mention that having either #[inline] or #[inline(always)] causes the function to be serialised to crate metadata, and so allow cross-crate inlining.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's also worth mentioning that #[inline(always)] may not always work due to indirection through function pointers. Link-time optimization allows cross-crate inlining without manual annotation so that's worth noting too.

### Deriving

The `deriving` attribute allows certain traits to be automatically
Expand Down Expand Up @@ -3223,12 +3245,12 @@ The type of a closure mapping an input of type `A` to an output of type `B` is `
An example of creating and calling a closure:

```rust
let captured_var = 10;
let captured_var = 10;

let closure_no_args = || println!("captured_var={}", captured_var);
let closure_no_args = || println!("captured_var={}", captured_var);

let closure_args = |arg: int| -> int {
println!("captured_var={}, arg={}", captured_var, arg);
println!("captured_var={}, arg={}", captured_var, arg);
arg // Note lack of semicolon after 'arg'
};

Expand Down