You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/0000-const-trait-impls.md
+33-1
Original file line number
Diff line number
Diff line change
@@ -292,7 +292,7 @@ You first figure out which method you're calling, then you check its bounds.
292
292
Otherwise it would at least seem like we'd have to allow some SFINAE or method overloading style things,
293
293
which we definitely do not support and have historically rejected over and over again.
294
294
295
-
### Impls with const methods for conditionally const trait methods
295
+
### Impls with const methods for conditionally const trait methods
296
296
297
297
trait impls with const methods for generic types work similarly to generic `const fn`.
298
298
Any `impl Trait for Type` is allowed to have `(const)` trait bounds if it has `const` methods:
@@ -320,6 +320,32 @@ where
320
320
321
321
See [this playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=313a38ef5c36b2ddf489f74167c1ac8a) for an example that works on nightly today.
322
322
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
+
structMyStruct<T>(T);
330
+
```
331
+
332
+
generates
333
+
334
+
```rust
335
+
impl<T:PartialEq> PartialEqforMyStruct<T> {
336
+
(const) fneq(&self, other:&Rhs) ->bool {
337
+
self.0==other.0
338
+
}
339
+
}
340
+
341
+
impl<T:Eq> EqforMyStruct<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
+
323
349
### `(const) Destruct` trait
324
350
325
351
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
958
984
# Future possibilities
959
985
[future-possibilities]: #future-possibilities
960
986
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
+
961
993
## Migrate to `(const) fn`
962
994
963
995
`const fn` and `const` items have slightly different meanings for `const`:
0 commit comments