Skip to content

Commit e5ad40d

Browse files
committed
Use non-deprecated version of the Data.withUnsafeBytes function in Extensions
Also specialize the "toArray" function to return only UInt8 arrays, since it is an internal function and its only use is to convert a Data to an array of bytes.
1 parent 2487752 commit e5ad40d

4 files changed

Lines changed: 6 additions & 8 deletions

File tree

Sources/ByteReader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public class ByteReader {
9292
return { (data: Data, offset: inout Int) -> [UInt8] in
9393
precondition(data.endIndex - offset >= count)
9494
defer { offset += count }
95-
return data[offset..<offset + count].toArray(type: UInt8.self, count: count)
95+
return data[offset..<offset + count].toByteArray(count)
9696
} (self.data, &self.offset)
9797
#else
9898
precondition(bytesLeft >= count)

Sources/Extensions.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ extension Data {
99

1010
@inline(__always)
1111
func to<T>(type: T.Type) -> T {
12-
return self.withUnsafeBytes { $0.pointee }
12+
return self.withUnsafeBytes { $0.bindMemory(to: type)[0] }
1313
}
1414

1515
@inline(__always)
16-
func toArray<T>(type: T.Type, count: Int) -> [T] {
17-
return self.withUnsafeBytes {
18-
[T](UnsafeBufferPointer(start: $0, count: count))
19-
}
16+
func toByteArray(_ count: Int) -> [UInt8] {
17+
return self.withUnsafeBytes { $0.map { $0 } }
2018
}
2119

2220
}

Sources/LsbBitReader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public final class LsbBitReader: ByteReader, BitReader {
302302
precondition(bitMask == 1, "BitReader is not aligned.")
303303
precondition(data.endIndex - offset >= count)
304304
defer { offset += count }
305-
return data[offset..<offset + count].toArray(type: UInt8.self, count: count)
305+
return data[offset..<offset + count].toByteArray(count)
306306
} (self.data, &self.offset, self.bitMask)
307307
#else
308308
precondition(bitMask == 1, "BitReader is not aligned.")

Sources/MsbBitReader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public final class MsbBitReader: ByteReader, BitReader {
302302
precondition(bitMask == 128, "BitReader is not aligned.")
303303
precondition(data.endIndex - offset >= count)
304304
defer { offset += count }
305-
return data[offset..<offset + count].toArray(type: UInt8.self, count: count)
305+
return data[offset..<offset + count].toByteArray(count)
306306
} (self.data, &self.offset, self.bitMask)
307307
#else
308308
precondition(bitMask == 128, "BitReader is not aligned.")

0 commit comments

Comments
 (0)