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.
When porting from
codespan
0.3 Arret I needed to track theFileId
separately from theSpan
now that the file isn't implied in the span itself. Having a simple struct like this works for 90% of the cases:However, this requires that every time we parse any source we require a
FileId
which requires acodespan::Files
to create. This is overkill in a couple of cases:Unit tests that just need to parse simple Arret strings as a shorthand for building our AST
Syntax highlighting in the REPL
The compromise in etaoins/arret#160 is to use
Option<FileId>
. I'm not 100% happy with that as the ultimate solution but it was the easiest path to take during porting. However, this makes the size of the above struct 13 bytes which will typically be rounded up to 16 bytes for alignment and padding.We can use
std::num::NonZeroU32
instead and offset all of our indexes by 1. This allows the Rust compiler to use 0 to represent theNone
case and bring the size down to 12 bytes. Because the struct only needs an alignment of 4 bytes this is more likely to stay as 12 bytes in most contexts.A nicer solution would be if Rust had e.g.
NonMaxU32
but it appears the Rust team instead wants to use const generics so arbitrary niche values can be specified.