Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit 11bb5ca

Browse files
authored
Removed the use of OpaquePointer from the byte array conversion code. (#337)
1 parent ee09712 commit 11bb5ca

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Sources/TensorFlow/Core/Utilities.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,16 @@ internal extension FixedWidthInteger {
184184
}
185185

186186
func bytes(count byteCount: Int = MemoryLayout<Self>.size) -> [UInt8] {
187+
let actualByteCount = Swift.min(MemoryLayout<Self>.size, byteCount)
187188
var littleEndianValue = littleEndian
188-
return withUnsafePointer(to: &littleEndianValue) { pointer -> [UInt8] in
189-
let bytesPointer = UnsafeMutablePointer<UInt8>(OpaquePointer(pointer))
190-
var bytes = [UInt8](repeating: 0, count: byteCount)
191-
for i in 0..<Swift.min(MemoryLayout<Self>.size, byteCount) {
192-
bytes[byteCount - 1 - i] = (bytesPointer + i).pointee
189+
return withUnsafePointer(to: &littleEndianValue) {
190+
$0.withMemoryRebound(to: UInt8.self, capacity: actualByteCount) { pointer in
191+
var bytes = [UInt8](repeating: 0, count: byteCount)
192+
for i in 0..<actualByteCount {
193+
bytes[byteCount - 1 - i] = (pointer + i).pointee
194+
}
195+
return bytes
193196
}
194-
return bytes
195197
}
196198
}
197199
}

0 commit comments

Comments
 (0)