To compute a hash, I want to loop through bytes of a trivial type. To get the bytes, I use withUnsafeBytes hands me a rawBufferPointer collection type. Great!
However this call also requires that the "of arg" be declared as a “var" instead of “let” because it is inout. So to do it generically, it forces me to make a copy to some var storage.
Since I am using withUnsafeBytes and not withUnsafeMutableBytes, though, it doesn’t seem like this copy should be necessary. Is there some way to avoid it? Is it something the compiler should be able to reason about and therefore elide? Possibly a future evolution (and something for that list)?
On a side note, wouldn’t it be cool if there where a way to enforce that a type was trivial?
On Jan 26, 2017, at 7:03 AM, Ray Fix via swift-users <swift-users@swift.org> wrote:
To compute a hash, I want to loop through bytes of a trivial type. To get the bytes, I use withUnsafeBytes hands me a rawBufferPointer collection type. Great!
However this call also requires that the "of arg" be declared as a “var" instead of “let” because it is inout. So to do it generically, it forces me to make a copy to some var storage.
Since I am using withUnsafeBytes and not withUnsafeMutableBytes, though, it doesn’t seem like this copy should be necessary. Is there some way to avoid it? Is it something the compiler should be able to reason about and therefore elide? Possibly a future evolution (and something for that list)?
On a side note, wouldn’t it be cool if there where a way to enforce that a type was trivial?