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

**URL:** <https://forums.swift.org/t/borrowing-nested-withunsafepointer-cannot-be-captured-by-an-escaping-closure-since-it-is-a-borrowed-parameter/80210>\
**Category:** Using Swift\
**Tags:** unsafe, borrowing\
**Created:** [May 30, 2025, 1:54am UTC](https://forums.swift.org/t/borrowing-nested-withunsafepointer-cannot-be-captured-by-an-escaping-closure-since-it-is-a-borrowed-parameter/80210 "2025-05-30T01:54:21Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![saulpalv](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/saulpalv/32/42373_2.png) [@saulpalv](https://forums.swift.org/u/saulpalv)\
**Post date:** [July 19, 2025, 4:48pm UTC](https://forums.swift.org/t/borrowing-nested-withunsafepointer-cannot-be-captured-by-an-escaping-closure-since-it-is-a-borrowed-parameter/80210/8 "2025-07-19T16:48:42Z")

</div>

> [@ksluder](#):
>
> Please post code as text, not screenshots. Y

The point was to show the compiler error

here is the playground code without if `let/var` and borrowing, it compiles only for `~Copyable` structs, the issue is that even though this compiles, the real code does not work because structs are generated without `~Copyable` on swift [swift's C Import](https://github.com/swiftlang/swift/blob/main/docs/HowSwiftImportsCAPIs.md#structs)

```swift
struct NC: ~Copyable {}

func drawTexture(
    _ srcrect: borrowing NC?,
    _ dstrect: borrowing NC
) {
    withUnsafePointer(to: dstrect) { ptrDstRect in
      if srcrect != nil {
        withUnsafePointer(to: srcrect!) { ptrSrcRect in
            foo(ptrSrcRect, ptrDstRect)
        }
     } else {
        foo(nil, ptrDstRect)
     }
    }
}

func foo(_ ptr1: UnsafePointer<NC>!, _ ptr2: UnsafePointer<NC>!) {}

```

If this is changed the Swift, the final error appears

```diff
- struct NC: ~Copyable {}
+ struct NC {}

```

So in summary C Imports on swift are mutable, do not play well with `borrowing` and `~Copyable` structs, there is no way to customize the generated c bindings, and may be not that bad to use `inout` for C interop

---

_[View the full topic](https://forums.swift.org/t/borrowing-nested-withunsafepointer-cannot-be-captured-by-an-escaping-closure-since-it-is-a-borrowed-parameter/80210)._
