Skip to content

doc: document the #[deriving] attribute. #6448

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

Merged
merged 1 commit into from
May 13, 2013
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
43 changes: 43 additions & 0 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,8 @@ names are effectively reserved. Some significant attributes include:
* The `test` attribute, for marking functions as unit tests.
* The `allow`, `warn`, `forbid`, and `deny` attributes, for controlling lint checks. Lint checks supported
by the compiler can be found via `rustc -W help`.
* The `deriving` attribute, for automatically generating
implementations of certain traits.

Other attributes may be added or removed during development of the language.

Expand Down Expand Up @@ -1526,6 +1528,47 @@ 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`.

### Deriving

The `deriving` attribute allows certain traits to be automatically
implemented for data structures. For example, the following will
create an `impl` for the `Eq` and `Clone` traits for `Foo`, the type
parameter `T` will be given the `Eq` or `Clone` constraints for the
appropriate `impl`:

~~~
#[deriving(Eq, Clone)]
struct Foo<T> {
a: int,
b: T
}
~~~

The generated `impl` for `Eq` is equivalent to

~~~
# struct Foo<T> { a: int, b: T }
impl<T: Eq> Eq for Foo<T> {
fn eq(&self, other: &Foo<T>) -> bool {
self.a == other.a && self.b == other.b
}

fn ne(&self, other: &Foo<T>) -> bool {
self.a != other.a || self.b != other.b
}
}
~~~

Supported traits for `deriving` are:

* Comparison traits: `Eq`, `TotalEq`, `Ord`, `TotalOrd`.
* Serialization: `Encodable`, `Decodable`. These require `std`.
* `Clone`, to perform deep copies.
* `IterBytes`, to iterate over the bytes in a data type.
* `Rand`, to create a random instance of a data type.
* `ToStr`, to convert to a string. For a type with this instance,
`obj.to_str()` has the same output as `fmt!("%?", obj)`.

# Statements and expressions

Rust is _primarily_ an expression language. This means that most forms of
Expand Down
21 changes: 21 additions & 0 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -2290,6 +2290,27 @@ let nonsense = mycircle.radius() * mycircle.area();

> ***Note:*** Trait inheritance does not actually work with objects yet

## Deriving implementations for traits

A small number of traits in `core` and `std` can have implementations
that can be automatically derived. These instances are specified by
placing the `deriving` attribute on a data type declaration. For
example, the following will mean that `Circle` has an implementation
for `Eq` and can be used with the equality operators, and that a value
of type `ABC` can be randomly generated and converted to a string:

~~~
#[deriving(Eq)]
struct Circle { radius: float }

#[deriving(Rand, ToStr)]
enum ABC { A, B, C }
~~~

The full list of derivable traits is `Eq`, `TotalEq`, `Ord`,
`TotalOrd`, `Encodable` `Decodable`, `Clone`, `IterBytes`, `Rand` and
`ToStr`.

# Modules and crates

The Rust namespace is arranged in a hierarchy of modules. Each source
Expand Down