@@ -761,8 +761,9 @@ extension TestNSData {
761
761
let expectedSize = MemoryLayout < UInt8 > . stride * a. count
762
762
XCTAssertEqual ( expectedSize, data. count)
763
763
764
- let underlyingBuffer = unsafeBitCast ( malloc ( expectedSize - 1 ) !, to: UnsafeMutablePointer< UInt8> . self )
765
- let buffer = UnsafeMutableBufferPointer ( start: underlyingBuffer, count: expectedSize - 1 )
764
+ let size = expectedSize - 1
765
+ let underlyingBuffer = malloc ( size) !
766
+ let buffer = UnsafeMutableBufferPointer ( start: underlyingBuffer. bindMemory ( to: UInt8 . self, capacity: size) , count: size)
766
767
767
768
// We should only copy in enough bytes that can fit in the buffer
768
769
let copiedCount = data. copyBytes ( to: buffer)
@@ -784,9 +785,10 @@ extension TestNSData {
784
785
}
785
786
let expectedSize = MemoryLayout < Int32 > . stride * a. count
786
787
XCTAssertEqual ( expectedSize, data. count)
787
-
788
- let underlyingBuffer = unsafeBitCast ( malloc ( expectedSize + 1 ) !, to: UnsafeMutablePointer< UInt8> . self )
789
- let buffer = UnsafeMutableBufferPointer ( start: underlyingBuffer, count: expectedSize + 1 )
788
+
789
+ let size = expectedSize + 1
790
+ let underlyingBuffer = malloc ( size) !
791
+ let buffer = UnsafeMutableBufferPointer ( start: underlyingBuffer. bindMemory ( to: UInt8 . self, capacity: size) , count: size)
790
792
791
793
let copiedCount = data. copyBytes ( to: buffer)
792
794
XCTAssertEqual ( expectedSize, copiedCount)
@@ -802,9 +804,10 @@ extension TestNSData {
802
804
var data = a. withUnsafeBufferPointer {
803
805
return Data ( buffer: $0)
804
806
}
805
-
806
- let underlyingBuffer = unsafeBitCast ( malloc ( data. count) !, to: UnsafeMutablePointer< UInt8> . self )
807
- let buffer = UnsafeMutableBufferPointer ( start: underlyingBuffer, count: data. count)
807
+
808
+ let size = data. count
809
+ let underlyingBuffer = malloc ( size) !
810
+ let buffer = UnsafeMutableBufferPointer ( start: underlyingBuffer. bindMemory ( to: UInt8 . self, capacity: size) , count: size)
808
811
809
812
var copiedCount : Int
810
813
@@ -831,10 +834,11 @@ extension TestNSData {
831
834
let data = a. withUnsafeBufferPointer {
832
835
return Data ( buffer: $0)
833
836
}
834
-
835
- let underlyingBuffer = unsafeBitCast ( malloc ( 10 ) !, to: UnsafeMutablePointer< UInt8> . self )
836
- let buffer = UnsafeMutableBufferPointer ( start: underlyingBuffer, count: 10 )
837
-
837
+
838
+ let size = 10
839
+ let underlyingBuffer = malloc ( size) !
840
+ let buffer = UnsafeMutableBufferPointer ( start: underlyingBuffer. bindMemory ( to: UInt8 . self, capacity: size) , count: size)
841
+
838
842
var copiedCount : Int
839
843
840
844
copiedCount = data. copyBytes ( to: buffer, from: 0 ..< 3 )
@@ -854,9 +858,10 @@ extension TestNSData {
854
858
let data = a. withUnsafeBufferPointer {
855
859
return Data ( buffer: $0)
856
860
}
857
-
858
- let underlyingBuffer = unsafeBitCast ( malloc ( 4 ) !, to: UnsafeMutablePointer< UInt8> . self )
859
- let buffer = UnsafeMutableBufferPointer ( start: underlyingBuffer, count: 4 )
861
+
862
+ let size = 4
863
+ let underlyingBuffer = malloc ( size) !
864
+ let buffer = UnsafeMutableBufferPointer ( start: underlyingBuffer. bindMemory ( to: UInt8 . self, capacity: size) , count: size)
860
865
861
866
var copiedCount : Int
862
867
@@ -983,9 +988,9 @@ extension TestNSData {
983
988
expectedSize += MemoryLayout < Bool > . stride * 2
984
989
XCTAssertEqual ( expectedSize, data. count)
985
990
986
- let underlyingBuffer = unsafeBitCast ( malloc ( expectedSize) ! , to : UnsafeMutablePointer < UInt8 > . self )
987
-
988
- let buffer = UnsafeMutableBufferPointer ( start: underlyingBuffer, count: expectedSize )
991
+ let size = expectedSize
992
+ let underlyingBuffer = malloc ( size ) !
993
+ let buffer = UnsafeMutableBufferPointer ( start: underlyingBuffer. bindMemory ( to : UInt8 . self , capacity : size ) , count: size )
989
994
let copiedCount = data. copyBytes ( to: buffer)
990
995
XCTAssertEqual ( copiedCount, expectedSize)
991
996
0 commit comments