• Improving the usability of C libraries in Swift There are many interesting, useful, and fun C libraries in the software ecosystem. • While one could go and rewrite these libraries in Swift, usually there is no need, because Swift provides direct interoperability with C. • With a little setup, you can directly use existing C libraries from your Swift code. • When you use a C library directly from Swift, it will look and feel similar to using it from C. • That can be useful if you’re following sample code or a tutorial written in C, but it can also feel out of place. • For example, here’s a small amount of code using a C API: var instanceDescriptor = WGPUInstanceDescriptor() let instance = wgpuCreateInstance(&instanceDescriptor) var surfaceDescriptor = WGPUSurfaceDescriptor() let surface = wgpuInstanceCreateSurface(instance, &surfaceDescriptor) if wgpuSurfacePresent(&surface) == WGPUStatus_Error { // report error } wgpuSurfaceRelease(surface) wgpuInstanceRelease(instance) The C library here that Swift is using comes from the webgpu-headers project, which vends a C header (webgpu.h ) that is used by several implementations of WebGPU.
Sources: