I had this this extensions that used to work before swift 5.7, now it throws a warning.
It is used to create a contiguous array from a buffer of C structs.
extension UnsafeMutablePointer {
func toContiguousArray<T>(count: Int) -> ContiguousArray<T> where Pointee == T {
let size = MemoryLayout<T>.size
let bufferPtr = UnsafeMutablePointer<T>.allocate(capacity: count)
memcpy(bufferPtr, self, count * size)
let buffer = UnsafeBufferPointer(start: self, count: count)
let values = ContiguousArray(buffer)
defer {
bufferPtr.deinitialize(count: count)
bufferPtr.deallocate()
}
return values
}
}
If I remove the constraint I get this error:
Cannot convert return expression of type 'ContiguousArray<Pointee>' to return type 'ContiguousArray<T>'
Event if I say that the type alias Pointee is equals to T I get the the same error.
typealias Pointee = T
Which is the correct way to fix that issue?