@@ -91,14 +91,18 @@ public struct DispatchData : RandomAccessCollection {
91
91
public func enumerateBytes(
92
92
block: ( _ buffer: UnsafeBufferPointer < UInt8 > , _ byteIndex: Int , _ stop: inout Bool ) -> Void )
93
93
{
94
- // FIXME: When SR-2313 (withoutActuallyEscaping) is implemented, use it to replace unsafeBitCast
95
- let nonEscapingBlock = unsafeBitCast ( block, to: _enumerateBytesBlock. self)
96
- _ = CDispatch . dispatch_data_apply ( __wrapped. __wrapped) { ( _, offset: Int , ptr: UnsafeRawPointer , size: Int ) in
97
- let bytePtr = ptr. bindMemory ( to: UInt8 . self, capacity: size)
98
- let bp = UnsafeBufferPointer ( start: bytePtr, count: size)
99
- var stop = false
100
- nonEscapingBlock ( bp, offset, & stop)
101
- return !stop
94
+ // we know that capturing block in the closure being created/passed to dispatch_data_apply
95
+ // does not cause block to escape because dispatch_data_apply does not allow its
96
+ // block argument to escape. Therefore, the usage of withoutActuallyEscaping to
97
+ // bypass the Swift type system is safe.
98
+ withoutActuallyEscaping ( block) { escapableBlock in
99
+ _ = CDispatch . dispatch_data_apply ( __wrapped. __wrapped) { ( _, offset: Int , ptr: UnsafeRawPointer , size: Int ) in
100
+ let bytePtr = ptr. bindMemory ( to: UInt8 . self, capacity: size)
101
+ let bp = UnsafeBufferPointer ( start: bytePtr, count: size)
102
+ var stop = false
103
+ escapableBlock ( bp, offset, & stop)
104
+ return !stop
105
+ }
102
106
}
103
107
}
104
108
@@ -273,8 +277,6 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
273
277
internal var _position : DispatchData . Index
274
278
}
275
279
276
- typealias _enumerateBytesBlock = ( _ buffer: UnsafeBufferPointer < UInt8 > , _ byteIndex: Int , _ stop: inout Bool ) -> Void
277
-
278
280
@_silgen_name ( " _swift_dispatch_data_empty " )
279
281
internal func _swift_dispatch_data_empty( ) -> dispatch_data_t
280
282
0 commit comments