Skip to content

Commit a27e208

Browse files
committed
Offer a solution for deriving const impls
1 parent 239e6a1 commit a27e208

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

const-generic-const-fn-bounds.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,24 @@ impl would suddenly stop being `const`, without any visible change to the API. T
150150
be allowed for the same reason as why we're not inferring `const` on functions: changes to private
151151
things should not affect the constness of public things, because that is not compatible with semver.
152152

153+
One possible solution is to require an explicit `const` in the derive:
154+
155+
```rust
156+
#[derive(const Clone)]
157+
pub struct Foo(Bar);
158+
159+
struct Bar;
160+
161+
const impl Clone for Bar {
162+
fn clone(&self) -> Self { Bar }
163+
}
164+
```
165+
166+
which would generate a `const impl Clone for Foo` block which would fail to compile if any of `Foo`'s
167+
fields (so just `Bar` in this example) are not implementing `Clone` via `const impl`. The obligation is
168+
now on the crate author to keep the public API semver compatible, but they can't accidentally fail to
169+
uphold that obligation by changing private things.
170+
153171
## RPIT (Return position impl trait)
154172

155173
```rust

0 commit comments

Comments
 (0)