@@ -63,6 +63,33 @@ impl const Hash for MyInt {
63
63
}
64
64
```
65
65
66
+ The same goes for associated types' bounds: all the bounds require ` impl const ` s for the type used
67
+ for the associated type:
68
+
69
+ ``` rust
70
+ trait Foo {
71
+ type Bar : Add ;
72
+ }
73
+ impl Foo for A {
74
+ type Bar = B ; // B must have an `impl const Add for B`
75
+ }
76
+ ```
77
+
78
+ If an associated type has no bounds in the trait, there are no restrictions to what types may be used
79
+ for it.
80
+
81
+ These rules for associated types exist to make this RFC forward compatible with adding const default bodies
82
+ for trait methods. These are further discussed in the "future work" section.
83
+
84
+ ## Generic ` impl ` blocks
85
+
86
+ Similar to generic parameters on ` const ` functions, one can have generic parameters on ` impl ` blocks.
87
+ These follow the same rules as bounds on ` const ` functions:
88
+
89
+ * all bounds are required to have ` impl const ` for substituted types if the impl is used in a const context
90
+ * except in the presence of ` ?const ` (see below)
91
+ * if the impl is used at runtime, there are no restrictions what kind of bounds are required
92
+
66
93
## Drop
67
94
68
95
A notable use case of ` impl const ` is defining ` Drop ` impls. If you write
@@ -329,6 +356,23 @@ const fn foo<T: ?const Foo>() -> i32 {
329
356
330
357
even though the ` ?const ` modifier explicitly opts out of constness.
331
358
359
+ The author of this RFC believes this feature to be unnecessary, since one can get the same effect
360
+ by splitting the trait into its const and nonconst parts:
361
+
362
+ ``` rust
363
+ trait FooA {
364
+ fn a () -> i32 ;
365
+ }
366
+ trait FooB {
367
+ fn b () -> i32 ;
368
+ }
369
+ const fn foo <T : FooA + ? const FooB >() -> i32 {
370
+ T :: a ()
371
+ }
372
+ ```
373
+
374
+ Impls of the two traits can then decide constness of either impl at their leasure.
375
+
332
376
### ` const ` traits
333
377
334
378
A further extension could be ` const trait ` declarations, which desugar to all methods being ` const ` :
@@ -410,6 +454,42 @@ fn foo(f: const fn() -> i32) -> i32 {
410
454
Which is useless except for ensuring some sense of "purity" of the function pointer ensuring that
411
455
subsequent calls will only modify global state if passed in via arguments.
412
456
457
+ ## explicit ` const ` bounds
458
+
459
+ ` const ` on the bounds (e.g. ` T: const Trait ` ) requires an ` impl const Trait ` for any types used to
460
+ replace ` T ` . This allows ` const ` trait bounds on any (even non-const) functions, e.g. in
461
+
462
+ ``` rust
463
+ fn foo <T : const Bar >() -> i32 {
464
+ const FOO : i32 = T :: bar ();
465
+ FOO
466
+ }
467
+ ```
468
+
469
+ Which, once ` const ` items and array lengths inside of functions can make use of the generics of
470
+ the function, would allow the above function to actually exist.
471
+
472
+ ## ` const ` default method bodies
473
+
474
+ Trait methods can have default bodies for methods that are used if the method is not mentioned
475
+ in an ` impl ` . This has several uses, most notably
476
+
477
+ * reducing code repetition between impls that are all the same
478
+ * adding new methods is not a breaking change if they also have a default body
479
+
480
+ In order to keep both advantages in the presence of ` impl const ` s, we need a way to declare the
481
+ method default body as being ` const ` . The author of this RFC considers prepending the default body's
482
+ method signature with ` const ` to be the most intuitive syntax.
483
+
484
+ ``` rust
485
+ trait Foo {
486
+ const fn bar () {}
487
+ }
488
+ ```
489
+
490
+ While this conflicts with other future work ideas like ` const ` trait methods or ` const trait ` declarations,
491
+ these features are unnecessary for full expressiveness as discussed in their respective sections.
492
+
413
493
# Unresolved questions
414
494
[ unresolved-questions ] : #unresolved-questions
415
495
0 commit comments