Skip to content

SR-3477: replace unsafeBitCast by withoutActuallyEscaping #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/swift/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,18 @@ public struct DispatchData : RandomAccessCollection {
public func enumerateBytes(
block: (_ buffer: UnsafeBufferPointer<UInt8>, _ byteIndex: Int, _ stop: inout Bool) -> Void)
{
// FIXME: When SR-2313 (withoutActuallyEscaping) is implemented, use it to replace unsafeBitCast
let nonEscapingBlock = unsafeBitCast(block, to: _enumerateBytesBlock.self)
_ = CDispatch.dispatch_data_apply(__wrapped.__wrapped) { (_, offset: Int, ptr: UnsafeRawPointer, size: Int) in
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: size)
let bp = UnsafeBufferPointer(start: bytePtr, count: size)
var stop = false
nonEscapingBlock(bp, offset, &stop)
return !stop
// we know that capturing block in the closure being created/passed to dispatch_data_apply
// does not cause block to escape because dispatch_data_apply does not allow its
// block argument to escape. Therefore, the usage of withoutActuallyEscaping to
// bypass the Swift type system is safe.
withoutActuallyEscaping(block) { escapableBlock in
_ = CDispatch.dispatch_data_apply(__wrapped.__wrapped) { (_, offset: Int, ptr: UnsafeRawPointer, size: Int) in
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: size)
let bp = UnsafeBufferPointer(start: bytePtr, count: size)
var stop = false
escapableBlock(bp, offset, &stop)
return !stop
}
}
}

Expand Down Expand Up @@ -273,8 +277,6 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
internal var _position: DispatchData.Index
}

typealias _enumerateBytesBlock = (_ buffer: UnsafeBufferPointer<UInt8>, _ byteIndex: Int, _ stop: inout Bool) -> Void

@_silgen_name("_swift_dispatch_data_empty")
internal func _swift_dispatch_data_empty() -> dispatch_data_t

Expand Down