Improve usability of spans for outparams. #7708
Merged
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.
A few changes here:
Allow setting the size of a Span (to a value no larger than its current size). This enables use of a Span over a non-const type as an outparam that you fill with data and then set the size of the data.
Allow converting a
Span<T>
to aSpan<const T>
, so once you fill such a non-const-type Span with data you can pass it to functions expecting a Span over a const type without having to manually create one.Change data_equal to allow comparing
Span<T>
toSpan<const T>
.Problem
Hard to use Span where we want to. See #7666 (comment)
Change overview
Address the shortcomings that comment pointed out.
Testing
Some automated tests included. I also did some manual tests that various things that should not compile do not:
Span<uint8_t>().data_equal(Span<uint16_t>())
MutableByteSpan s = ByteSpan()
Wish we had automated ways to test "this should not compile"....