UnsafeMutablePointer to [UInt8] without memory copy

后端 未结 2 1527
抹茶落季
抹茶落季 2021-02-05 23:15

Is it possible to create a [UInt8] from an UnsafeMutablePointer without copying the bytes?

In the NSData world I coul

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-05 23:34

    You can use UnsafeBufferPointer + map to achieve your goal.
    KEEP IN MIND: pointer boundary should be maintained by yourself, while arrays will do by itself
    let bytesArray = UnsafeBufferPointer(start: bts, count: bytes.count).map{$0}
    Swift 5, Xcode 11:

    var bytes: [UInt8] = [1, 3, 5, 7]
    let count = bytes.count
    print(bytes)
    
    // MARK: - [UInt8] -> UnsafePointer
    
    // producing dangling pointer warning bug?
    let bts1: UnsafePointer = UnsafePointer(bytes)
    print(bts1, bts1[0], bts1[1], bts1[2], bts1[3], bts1[4])
    
    let bts = withUnsafePointer(to: &bytes[0]) {
        $0.withMemoryRebound(to: UInt8.self, capacity: count) { $0 }
    }
    print(bts, bts[0], bts[1], bts[4])
    
    // MARK: - UnsafePointer -> [UInt8]
    let bytesArray = UnsafeBufferPointer(start: bts, count: bytes.count).map{$0}
    print(bytesArray, bytesArray[0], bytesArray[1]/*, bytesArray[4]*/)
    

    output:

    [1, 3, 5, 7]
    0x0000600001946840 1 3 5 7 0
    0x0000600001946840 1 3 0
    [1, 3, 5, 7] 1 3
    

    bad samples:

    /// invalid sample 1
    let testA = withUnsafePointer(to: &bytes) {
        $0.withMemoryRebound(to: UInt8.self, capacity: count) { $0 }
    }
    print(testA, testA[0], testA[1], testA[4])
    
    /// invalid sample 2
    let testB = withUnsafePointer(to: bytes[0]) {
        $0.withMemoryRebound(to: UInt8.self, capacity: count) { $0 }
    }
    print(testB, testB[0], testB[1], testB[4])
    

    output:

    0x0000000102b4f520 32 104 0
    0x00007ffeed203ac0 192 58 254
    

提交回复
热议问题