generated from rust-lang/project-group-template
-
Notifications
You must be signed in to change notification settings - Fork 9
add a general section for the reasoning behind const generics #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Why compile time evaluation? | ||
|
||
This section is, in many ways, the most important. It aims to clarify the reasoning about any future decisions about const generics and const evaluation. | ||
|
||
## Why do we want to evaluate things at compile time? | ||
|
||
People already precompute certain data structures, lookup tables, and so on, at compile time. | ||
For example the crate `regex` could be able to compile a regex at compile time, | ||
improving the performance when using `regex` and even removing the need for runtime allocations. | ||
Const evaluation is also very useful to assert correctness requirements at compile time, instead | ||
of using a runtime panic. | ||
|
||
Without good const evaluation support, is often easier to just use a `build.rs` file for this or compute it externally and generate the result outside of the ordinary compilation process. This is both worse for the user, as they essentially have to learn yet another | ||
meta language, and also negatively impacts the compilation speed and incremental compilation. If that isn't possible or sensible, one often just requires the computation to happen at runtime, negatively impacting startup performance. | ||
|
||
Computing things outside without using const eval makes it at lot more cumbersome to use and | ||
prevents the input for that computation to simply be a generic or function parameter. | ||
lcnr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Why const generics? | ||
|
||
Const evaluation by itself is good enough unless it should influence the type system. | ||
|
||
This is needed when computing the layout of types, most often by using arrays. | ||
Using arrays instead of dynamic allocations or slices can improve performance, reduces the dynamic memory footprint | ||
and can remove runtime checks and implicit invariants. | ||
|
||
Another reason is to be generic over configurations or to move certain conditions to compile time. Consider [image handles](https://github.com/EmbarkStudios/rust-gpu/blob/1431c18b9db70feafc64e5096a64e5fefffbed18/crates/spirv-std/src/image.rs#L31) in [`rust-gpu`](https://github.com/EmbarkStudios/rust-gpu) or TODO: example for state machines. | ||
|
||
## What are our goals? | ||
|
||
### Consistent: "just add `const`" | ||
|
||
Writing code that works at compile time should at most require manually adding the needed `const` annotations at suggested places (or even automatically using `rustfix` or IDE support) | ||
until everything compiles. One should not be required to avoid large parts of the language to do this. | ||
Adding and using const parameters should be straight forward without weird restrictions or a lot required knowledge. | ||
Using const generic items from different libraries should work without issues. | ||
|
||
### Approachable: "what is `[u8; my_expr]:,` and why is it needed" | ||
|
||
Reading or writing code which can be used at compile time should ideally not be more complex | ||
than writing other code. Requiring a lot of weird trait bounds or other hacks should be avoided as much as possible. | ||
Supporting compile time evaluation should not worsen the experience of users, especially for beginners. | ||
|
||
### Reliable: "if it passes `cargo check`, it works" | ||
|
||
Const evaluation failures should be detected as early as possible, even when using `cargo check`. When writing library code, incorrect constants | ||
should cause warnings and errors whenever possible. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.