Skip to content

Commit c1981e4

Browse files
committed
Add derives
1 parent 0a82e9e commit c1981e4

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

text/0000-const-trait-impls.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ You first figure out which method you're calling, then you check its bounds.
292292
Otherwise it would at least seem like we'd have to allow some SFINAE or method overloading style things,
293293
which we definitely do not support and have historically rejected over and over again.
294294

295-
### Impls with const methods for conditionally const trait methods
295+
### Impls with const methods for conditionally const trait methods
296296

297297
trait impls with const methods for generic types work similarly to generic `const fn`.
298298
Any `impl Trait for Type` is allowed to have `(const)` trait bounds if it has `const` methods:
@@ -320,6 +320,32 @@ where
320320

321321
See [this playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=313a38ef5c36b2ddf489f74167c1ac8a) for an example that works on nightly today.
322322

323+
### Derives
324+
325+
Most of the time you don't want to write out your impls by hand, but instead derive them as the implementation is obvious from your data structure.
326+
327+
```rust
328+
#[const_derive(PartialEq, Eq)]
329+
struct MyStruct<T>(T);
330+
```
331+
332+
generates
333+
334+
```rust
335+
impl<T: PartialEq> PartialEq for MyStruct<T> {
336+
(const) fn eq(&self, other: &Rhs) -> bool {
337+
self.0 == other.0
338+
}
339+
}
340+
341+
impl<T: Eq> Eq for MyStruct<T> {}
342+
```
343+
344+
For THIS RFC, we stick with `derive_const`, because it interacts with other ongoing bits of design work (e.g., RFC 3715)
345+
and we don't want to have to resolve all design questions at once to do anything.
346+
We encourage another RFC to integrate const/unsafe and potentially other modifiers into the derive syntax in a better way.
347+
If this lands prior to stabilization, we should implement the const portion of it, otherwise we'll deprecate `derive_const`.
348+
323349
### `(const) Destruct` trait
324350

325351
The `Destruct` trait enables dropping types within a const context.
@@ -958,6 +984,12 @@ as they need to actually call the generic `FnOnce` argument or nested `PartialEq
958984
# Future possibilities
959985
[future-possibilities]: #future-possibilities
960986

987+
## Better derive syntax than `#[derive_const(Trait)]`
988+
989+
Once `unsafe` derives have been finalized, we can separately design const derives and
990+
deprecate `derive_const` at that time (mostly by just removing it from any documents explaining it,
991+
so that the ecosystem slowly migrates, maybe with an actual deprecation warning later).
992+
961993
## Migrate to `(const) fn`
962994

963995
`const fn` and `const` items have slightly different meanings for `const`:

0 commit comments

Comments
 (0)