[Pitch] Safe loading of integer values from `RawSpan`

The question I’m getting at is whether endianness is an irreducible complexity of reading even a single multibyte value from memory. If I understand correctly, the implication of your position is that endianness is only a concern when you consider higher-level context such as the provenance of the memory being read from or its participation in a larger data structure. This higher-level context will come with its own abstraction, where endianness is an appropriate API.

I keep getting stuck on how this intermediate abstraction will actually implement the endianness conversion. The solution of using UInt16.init(bigEndian:) makes me uncomfortable because it implies temporarily modeling data in the correct type but with an invalid value. When interpreting raw memory, I would like the type system to help me distinguish between ready-to-use values and dangerous, intermediate values. For non-integer types like Char or Float, trying to force the byte-swapped interpretation into a temporary value of the same type might be lossy or even impossible.

My counterposition is that while it is reasonable to assume native endianness by default, it is a possible concern at any point where a value can be created from raw memory. Therefore any operation that creates a value from raw memory ought to admit an endianness argument.

7 Likes