swiftgenericsunsafe-pointersunsafemutablepointer

Are these memory functions the same for Swift 5 Conversion UnsafeBufferPointer


I am doing some Swift 5 conversion on code that I don't quite understand, legacy code from a previous developer. I get:

'withUnsafeBytes' is deprecated: use withUnsafeBytes(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead

for:

func toArray<T>(type: T.Type) -> [T] {
    return self.withUnsafeBytes {
        [T](UnsafeBufferPointer(start: $0, count: self.count/MemoryLayout<T>.stride))
    }
}

I want to replace it with this but I am unsure if it does the same thing:

func toArray<T>(type: T.Type) -> [T] where T: ExpressibleByIntegerLiteral {
    var array = [T](repeating: 0, count: self.count/MemoryLayout<T>.stride)
    _ = array.withUnsafeMutableBytes { copyBytes(to: $0) }
    return array
}

Used in the context of these two fxs:

static func extractPacketSizeFromIV(iv: Data) -> Int32? {
    let array = iv.toArray(type: Int32.self)
    guard array.count == 4 else { return nil }

    let r0 = array[0].bigEndian
    let r1 = array[1].bigEndian
    let r2 = array[2].bigEndian

    return r2 ^ r1 ^ r0
}

static func extractGuidFromIV(iv: Data) -> Int32? {
    let array = iv.toArray(type: Int32.self)
    guard array.count == 4 else { return nil }

    let r0 = array[0].bigEndian
    let r1 = array[1].bigEndian
    let r2 = array[2].bigEndian
    let r3 = array[3].bigEndian

    return r3 ^ r2 ^ r1 ^ r0
}

Solution

  • First of all, your toArray is defined in an extension of Data, right?

    (Please clarify such things when you write questions.)

    Your code would work in the same way as the code from a previous developer in your use case, but I would write the equivalent in Swift 5 like this:

    func toArray<T>(type: T.Type) -> [T] {
        return self.withUnsafeBytes {
            [T]($0.bindMemory(to: type))
        }
    }
    

    bindMemory(to: type) creates an UnsafeBufferPointer<T> (as in the original code) from the parameter passed from new withUnsafeBytes - which is UnsafeRawBufferPointer.