Skip to content

Commit a2406f5

Browse files
authored
Merge pull request #1883 from ehuss/derive
Update `derive` to use the attribute template
2 parents 5b3ca00 + 46f5c66 commit a2406f5

File tree

2 files changed

+59
-31
lines changed

2 files changed

+59
-31
lines changed

src/attributes/derive.md

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,66 @@ r[attributes.derive]
22
# Derive
33

44
r[attributes.derive.intro]
5-
The *`derive` attribute* allows new [items] to be automatically generated for
6-
data structures.
5+
The *`derive` [attribute][attributes]* invokes one or more [derive macros], allowing new [items] to be automatically generated for data structures. You can create `derive` macros with [procedural macros].
6+
7+
> [!EXAMPLE]
8+
> The [`PartialEq`][macro@PartialEq] derive macro emits an [implementation] of [`PartialEq`] for `Foo<T> where T: PartialEq`. The [`Clone`][macro@Clone] derive macro does likewise for [`Clone`].
9+
>
10+
> ```rust
11+
> #[derive(PartialEq, Clone)]
12+
> struct Foo<T> {
13+
> a: i32,
14+
> b: T,
15+
> }
16+
> ```
17+
>
18+
> The generated `impl` items are equivalent to:
19+
>
20+
> ```rust
21+
> # struct Foo<T> { a: i32, b: T }
22+
> impl<T: PartialEq> PartialEq for Foo<T> {
23+
> fn eq(&self, other: &Foo<T>) -> bool {
24+
> self.a == other.a && self.b == other.b
25+
> }
26+
> }
27+
>
28+
> impl<T: Clone> Clone for Foo<T> {
29+
> fn clone(&self) -> Self {
30+
> Foo { a: self.a.clone(), b: self.b.clone() }
31+
> }
32+
> }
33+
> ```
734
835
r[attributes.derive.syntax]
9-
It uses the [MetaListPaths] syntax to specify a list of
10-
traits to implement or paths to [derive macros] to process.
11-
12-
For example, the following will create an [`impl` item] for the
13-
[`PartialEq`] and [`Clone`] traits for `Foo`, and the type parameter `T` will be
14-
given the `PartialEq` or `Clone` constraints for the appropriate `impl`:
15-
16-
```rust
17-
#[derive(PartialEq, Clone)]
18-
struct Foo<T> {
19-
a: i32,
20-
b: T,
21-
}
22-
```
23-
24-
The generated `impl` for `PartialEq` is equivalent to
25-
26-
```rust
27-
# struct Foo<T> { a: i32, b: T }
28-
impl<T: PartialEq> PartialEq for Foo<T> {
29-
fn eq(&self, other: &Foo<T>) -> bool {
30-
self.a == other.a && self.b == other.b
31-
}
32-
}
33-
```
34-
35-
r[attributes.derive.proc-macro]
36-
You can implement `derive` for your own traits through [procedural macros].
36+
The `derive` attribute uses the [MetaListPaths] syntax to specify a list of paths to [derive macros] to invoke.
37+
38+
r[attributes.derive.allowed-positions]
39+
The `derive` attribute may be applied to [structs][items.struct], [enums][items.enum], and [unions][items.union].
40+
41+
r[attributes.derive.duplicates]
42+
The `derive` attribute may be specified multiple times on an item, with all derive macros listed in all attributes being invoked.
43+
44+
r[attributes.derive.stdlib]
45+
The `derive` attribute is exported in the standard library prelude as [`core::prelude::v1::derive`].
46+
47+
r[attributes.derive.built-in]
48+
Built-in derives are defined in the [language prelude][names.preludes.lang]. The list of built-in derives are:
49+
50+
- [`Clone`]
51+
- [`Copy`]
52+
- [`Debug`]
53+
- [`Default`]
54+
- [`Eq`]
55+
- [`Hash`]
56+
- [`Ord`]
57+
- [`PartialEq`]
58+
- [`PartialOrd`]
59+
60+
r[attributes.derive.built-in-automatically_derived]
61+
The built-in derives include the [`automatically_derived` attribute][attributes.derive.automatically_derived] on the implementations they generate.
62+
63+
r[attributes.derive.behavior]
64+
During macro expansion, for each element in the list of derives, the corresponding derive macro expands to zero or more [items].
3765
3866
r[attributes.derive.automatically_derived]
3967
## The `automatically_derived` attribute
@@ -43,7 +71,6 @@ The *`automatically_derived` attribute* is automatically added to
4371
has no direct effect, but it may be used by tools and diagnostic lints to
4472
detect these automatically generated implementations.
4573
46-
[`impl` item]: ../items/implementations.md
4774
[items]: ../items.md
4875
[derive macros]: ../procedural-macros.md#derive-macros
4976
[implementations]: ../items/implementations.md

src/names/preludes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ It includes the following:
124124
* [floating-point types] --- `f32` and `f64`
125125
* [Macro namespace]
126126
* [Built-in attributes]
127+
* [Built-in derive macros][attributes.derive.built-in]
127128
128129
r[names.preludes.macro_use]
129130
## `macro_use` prelude

0 commit comments

Comments
 (0)