Skip to content

Commit d43908a

Browse files
committed
doc: document the #[deriving] attribute.
Closes #4916.
1 parent 8a5561b commit d43908a

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

doc/rust.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,8 @@ names are effectively reserved. Some significant attributes include:
14251425
* The `test` attribute, for marking functions as unit tests.
14261426
* The `allow`, `warn`, `forbid`, and `deny` attributes, for controlling lint checks. Lint checks supported
14271427
by the compiler can be found via `rustc -W help`.
1428+
* The `deriving` attribute, for automatically generating
1429+
implementations of certain traits.
14281430

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

@@ -1526,6 +1528,47 @@ A complete list of the built-in language items follows:
15261528
> **Note:** This list is likely to become out of date. We should auto-generate it
15271529
> from `librustc/middle/lang_items.rs`.
15281530
1531+
### Deriving
1532+
1533+
The `deriving` attribute allows certain traits to be automatically
1534+
implemented for data structures. For example, the following will
1535+
create an `impl` for the `Eq` and `Clone` traits for `Foo`, the type
1536+
parameter `T` will be given the `Eq` or `Clone` constraints for the
1537+
appropriate `impl`:
1538+
1539+
~~~
1540+
#[deriving(Eq, Clone)]
1541+
struct Foo<T> {
1542+
a: int,
1543+
b: T
1544+
}
1545+
~~~
1546+
1547+
The generated `impl` for `Eq` is equivalent to
1548+
1549+
~~~
1550+
# struct Foo<T> { a: int, b: T }
1551+
impl<T: Eq> Eq for Foo<T> {
1552+
fn eq(&self, other: &Foo<T>) -> bool {
1553+
self.a == other.a && self.b == other.b
1554+
}
1555+
1556+
fn ne(&self, other: &Foo<T>) -> bool {
1557+
self.a != other.a || self.b != other.b
1558+
}
1559+
}
1560+
~~~
1561+
1562+
Supported traits for `deriving` are:
1563+
1564+
* Comparison traits: `Eq`, `TotalEq`, `Ord`, `TotalOrd`.
1565+
* Serialization: `Encodable`, `Decodable`. These require `std`.
1566+
* `Clone`, to perform deep copies.
1567+
* `IterBytes`, to iterate over the bytes in a data type.
1568+
* `Rand`, to create a random instance of a data type.
1569+
* `ToStr`, to convert to a string. For a type with this instance,
1570+
`obj.to_str()` has the same output as `fmt!("%?", obj)`.
1571+
15291572
# Statements and expressions
15301573

15311574
Rust is _primarily_ an expression language. This means that most forms of

doc/tutorial.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,6 +2290,27 @@ let nonsense = mycircle.radius() * mycircle.area();
22902290

22912291
> ***Note:*** Trait inheritance does not actually work with objects yet
22922292
2293+
## Deriving implementations for traits
2294+
2295+
A small number of traits in `core` and `std` can have implementations
2296+
that can be automatically derived. These instances are specified by
2297+
placing the `deriving` attribute on a data type declaration. For
2298+
example, the following will mean that `Circle` has an implementation
2299+
for `Eq` and can be used with the equality operators, and that a value
2300+
of type `ABC` can be randomly generated and converted to a string:
2301+
2302+
~~~
2303+
#[deriving(Eq)]
2304+
struct Circle { radius: float }
2305+
2306+
#[deriving(Rand, ToStr)]
2307+
enum ABC { A, B, C }
2308+
~~~
2309+
2310+
The full list of derivable traits is `Eq`, `TotalEq`, `Ord`,
2311+
`TotalOrd`, `Encodable` `Decodable`, `Clone`, `IterBytes`, `Rand` and
2312+
`ToStr`.
2313+
22932314
# Modules and crates
22942315

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

0 commit comments

Comments
 (0)