Skip to content
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

Improve usability of spans for outparams. #7708

Merged
merged 1 commit into from
Jun 17, 2021

Conversation

bzbarsky-apple
Copy link
Contributor

@bzbarsky-apple bzbarsky-apple commented Jun 17, 2021

A few changes here:

  1. 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.

  2. Allow converting a Span<T> to a Span<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.

  3. Change data_equal to allow comparing Span<T> to Span<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:

  1. Span<uint8_t>().data_equal(Span<uint16_t>())
  2. MutableByteSpan s = ByteSpan()

Wish we had automated ways to test "this should not compile"....

A few changes here:

1) 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.

2) Allow converting a Span<T> to a Span<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.

3) Change data_equal to allow comparing Span<T> to Span<const T>.
src/lib/support/Span.h Show resolved Hide resolved
@woody-apple woody-apple merged commit 5b9cbc3 into project-chip:master Jun 17, 2021
@bzbarsky-apple bzbarsky-apple deleted the span-updates branch June 17, 2021 15:27
nikita-s-wrk pushed a commit to nikita-s-wrk/connectedhomeip that referenced this pull request Sep 23, 2021
A few changes here:

1) 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.

2) Allow converting a Span<T> to a Span<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.

3) Change data_equal to allow comparing Span<T> to Span<const T>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants