[Pitch] Equatable and Hashable conformances for the span family

Hello everyone,

Span, MutableSpan and OutputSpan should conform to Equatable and Hashable when their Element does, and RawSpan, MutableRawSpan and OutputRawSpan should conform unconditionally. Values of these types should be considered equal when their elements/bytes are equal (not only when they view the same memory region).

Here is a proposal: Proposal for `Equatable` and `Hashable` conformances for the span family by clintonpi · Pull Request #3574 · swiftlang/swift-evolution · GitHub

7 Likes

I would be more comfortable with this if Spans also supported === so I could easily compare the memory regions without having to drop down to unsafe pointers.

1 Like

Um, they do, just not spelled with an operator:

5 Likes

Well, then. Full steam ahead, I say.

Two Output(Raw)Spans with the same initialized content are equal even if their capacities differ.

I feel a little weird about this one, though it's consistent with Array. It's always possible to say out.span == other.span, so the cost of omitting it isn't so terrible.

I also was a little surprised to see Hashable here (on all three types), but I suppose that's relevant both for eventually having hash-based data structures that work on data borrowed from backing storage, and for making it easier to implement hashing on custom structures.

1 Like

Does Ref already conform where its element does? If not, should it be added here?

1 Like

Ref and MutableRef do not conform and will be included in this pitch. Thanks for pointing that out.

1 Like

It's odd to me that the Mutable variants get implementations.

I think it's OK, because if the Ref or Span is mutable, there can be only one Ref or Span to that object, and therefore whatever owns the thing is "in control" of all mutations of the underlying object?

In that sense, MutableRef/MutableSpan aren't really like reference types at all.

Seems like at least there's intersection between this and the Cell proposal — spans of cells being Hashable would clearly be bad.