Skip to content

Commit ac16fbb

Browse files
authored
Merge pull request swiftlang#198 from dgrove-oss/SR-3477
SR-3477: replace unsafeBitCast by withoutActuallyEscaping
2 parents eb730eb + cc59902 commit ac16fbb

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/swift/Data.swift

+12-10
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,18 @@ public struct DispatchData : RandomAccessCollection {
9191
public func enumerateBytes(
9292
block: (_ buffer: UnsafeBufferPointer<UInt8>, _ byteIndex: Int, _ stop: inout Bool) -> Void)
9393
{
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+
}
102106
}
103107
}
104108

@@ -273,8 +277,6 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
273277
internal var _position: DispatchData.Index
274278
}
275279

276-
typealias _enumerateBytesBlock = (_ buffer: UnsafeBufferPointer<UInt8>, _ byteIndex: Int, _ stop: inout Bool) -> Void
277-
278280
@_silgen_name("_swift_dispatch_data_empty")
279281
internal func _swift_dispatch_data_empty() -> dispatch_data_t
280282

0 commit comments

Comments
 (0)