Expand the readability of code that works with SIMD and Pointers

I've been happily using the new SIMD type, but there's a small change I'd love to see when working with pointers specifically. Provided the following function:

func run(_ pointer: UnsafePointer<UInt8>) {
    ...
}

The following code works fine, which is a blessing for the readability of my code:

let array = [UInt8]()
run(array)

The following, however, isn't implemented in the compiler:

let simd = SIMD64<UInt8>(repeating: 0)
run(simd)

I think it'd also be useful to be able to use the following code:

func run(_ pointer: UnsafeMutablePointer<UInt8>) {
    ...
}

var simd = SIMD64<UInt8>(repeating: 0)
run(&simd)

I mostly don’t like the subtle magic pointer transformations that Swift can do, so I’m not really in favour of either of these. It seems to me that either the SIMD types should safely be usable with withUnsafeBytes(of:) or they should expose a .withUnsafeBytes method.

Any helper for using the & or magic transformation would want such a method as the baseline anyway, so I think it’s probably better to start with the simpler method and see if that addresses the use-case (which I think it basically will).

The .withUnsafeBytes route seems like a good alternative for sure.

I'm glad if withUnsafe*BufferPointer is also provided🙂

Terms of Service

Privacy Policy

Cookie Policy