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
For closures and them implementing the `Fn` traits, see the [Future possibilities](#future-possibilities) section.
378
+
344
379
## Crate authors: Making your own custom types easier to use
345
380
346
381
You can write const trait impls of many standard library traits for your own types.
@@ -681,6 +716,62 @@ trait Foo: InitFoo {
681
716
# Alternatives
682
717
[alternatives]: #alternatives
683
718
719
+
## What is the impact of not doing this?
720
+
721
+
We would require everything that wants a const-equivalent to have duplicated traits and not
722
+
use `const` fn at all, but use associated consts instead. Similarly this would likely forbid
723
+
invoking builtin operators. This same concern had been brought up for the `const fn` stabilization
724
+
[7 years ago](https://github.com/rust-lang/rust/issues/24111#issuecomment-385046163).
725
+
726
+
Basically what we can do is create
727
+
728
+
```rust
729
+
traitConstDefault {
730
+
constDEFAULT:Self;
731
+
}
732
+
```
733
+
734
+
and require users to use
735
+
736
+
```rust
737
+
constFOO:Vec<u8> =ConstDefault::DEFAULT;
738
+
```
739
+
740
+
instead of
741
+
742
+
```rust
743
+
constfnFOO:Vec<u8> =Default::default();
744
+
```
745
+
746
+
This duplication is what this RFC is suggesting to avoid.
747
+
748
+
Since it has already been possible to do all of this on stable Rust for years, and no major
749
+
crates have popped and gotten used widely, I assume that is either because
750
+
751
+
* it's too much duplication, or
752
+
* everyone was waiting for the work (that this RFC wants to stabilize) to finish, or
753
+
* both.
754
+
755
+
So while it is entirely possible that rejecting this RFC and deciding not to go down this route
756
+
will lead to an ecosystem for const operations to be created, it would result in duplication and
757
+
inconsistencies that we'd rather like to avoid.
758
+
759
+
Such an ecosystem would also make `const fn` obsolete, as every `const fn` can in theory be represented
760
+
as a trait, it would just be very different to use from normal rust code and not really allow nice abstractions to be built.
761
+
762
+
```rust
763
+
constfnadd(a:u32, b:u32) ->u32 { a+b }
764
+
765
+
structAdd<constA:u32, constB:u32>;
766
+
767
+
impl<constA:u32, constB:u32> Add<A, B> {
768
+
constRESULT:u32=A+B;
769
+
}
770
+
771
+
constFOO:u32=add(5, 6);
772
+
constBAR:u32=Add<5, 6>::RESULT;
773
+
```
774
+
684
775
## use `const Trait` bounds for conditionally-const, invent new syntax for always-const
685
776
686
777
It may seem tempting to use `const fn foo<T: const Trait>` to mean what in this RFC is `~const Trait`, and then add new syntax for bounds that allow using trait methods in const blocks.
0 commit comments