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
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
- 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