CGPath.apply() needs a more Swift-friendly API

Originator:kristopherdjohnson
Number:rdar://29819632 Date Originated:28-Dec-2016 09:05 AM
Status:Duplicate/29603813 Resolved:
Product:iOS + SDK Product Version:Xcode 8.2.1 (8C1002)
Classification:Enhancement Reproducible:Always
 
Area:
Core Graphics

Summary:
CGPath.apply() has the following Swift signature:

    public func apply(info: UnsafeMutableRawPointer?, function: @escaping CoreGraphics.CGPathApplierFunction)

where CGPathApplierFunction is defined like this:

    public typealias CGPathApplierFunction = @convention(c) (UnsafeMutableRawPointer?, UnsafePointer<CGPathElement>) -> Swift.Void

All of these unsafe pointers make it difficult to write type-safe Swift code. In addition, the @convention(c) makes it impossible for the applier function to capture variables in its context, so it is necessary to define some sort of struct/class to pass data to the function and return results, and cast that to/from UnsafeMutableRawPointer.

CGPath.apply() should have a more Swift-friendly API, similar to that of the Array.forEach() function, which supports use of closures with captured variables, perhaps with a signature like this:

    func forEach(_ body: (CGPathElement) throws -> Swift.Void) rethrows

Note: A wrapper that provides a more Swift-friendly API can be defined by a Swift user like this:

extension CGPath {
    /// Call the given closure on each element of the path.
    func forEach(_ body: @escaping (CGPathElement) -> Void) {
        var info = body
        self.apply(info: &info) { (infoPtr, elementPtr) in
            let opaquePtr = OpaquePointer(infoPtr!)
            let body = UnsafeMutablePointer<(CGPathElement) -> Void>(opaquePtr).pointee
            body(elementPtr.pointee)
        }
    }
}

However, it would be preferable to be able to declare the closure non-escaping, and implementation within Core Graphics would probably provide better performance than a wrapper.


Steps to Reproduce:
N/A

Expected Results:
N/A

Actual Results:
N/A

Version:
Xcode Version 8.2.1 (8C1002)

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!