Borrowing nested withUnsafePointer: cannot be captured by an escaping closure since it is a borrowed parameter

have two borrowed parameters of type SDL_FRect, need to convert both to UnsafePointer<SDL_FRect>, but nested closures and borrowing parameters do not like each other

    func drawTexture(
        _ texture: SDLTexture,
        _ srcrect: borrowing SDL_FRect?,
        _ dstrect: borrowing SDL_FRect
    ) -> Bool {
        return withUnsafePointer(to: dstrect) { ptrDstRect in
            if srcrect == nil {
                return SDL_RenderTexture(ptr, texture.ptr, nil, ptrDstRect)
            } else {
                // this nested withUnsafePointer(to: localSrcRect) is not allowed
                return withUnsafePointer(to: srcrect!) { ptrSrcRect in
                    SDL_RenderTexture(ptr, texture.ptr, ptrSrcRect, ptrDstRect)
                }
            }
        }
    }

throws error

'srcrect' cannot be captured by an escaping closure since it is a borrowed parameterSourceKit

renderer.swift(36, 47): Closure capturing 'srcrect' here

Definition

public struct SDL_FRect {

    public init()

    public init(x: Float, y: Float, w: Float, h: Float)

    public var x: Float

    public var y: Float

    public var w: Float

    public var h: Float
}

is there a way to use borrowed parameters in nested withUnsafePointer closures?

1 Like

Are you sure that's where that error is coming from? The closure withUnsafePointer accepts isn't escaping. Is there self-contained code sample you can provide that results in the same error?