{"id":50807,"date":"2026-01-23T19:28:42","date_gmt":"2026-01-24T00:28:42","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=50807"},"modified":"2026-01-26T12:16:25","modified_gmt":"2026-01-26T17:16:25","slug":"improving-the-usability-of-c-libraries-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2026\/01\/23\/improving-the-usability-of-c-libraries-in-swift\/","title":{"rendered":"Improving the Usability of C Libraries in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/www.swift.org\/blog\/improving-usability-of-c-libraries-in-swift\/\">Doug Gregor<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.swift.org\/blog\/improving-usability-of-c-libraries-in-swift\/\">\n<p>The Swift code above has a very &ldquo;C&rdquo; feel to it. It has global function calls with prefixed names like <code>wgpuInstanceCreateSurface<\/code> and global integer constants like <code>WGPUStatus_Error<\/code>. It pervasively uses unsafe pointers, some of which are managed with explicit reference counting, where the user provides calls to <code>wpuXYZAddRef<\/code> and <code>wgpuXYZRelease<\/code> functions. It works, but it doesn&rsquo;t feel like Swift, and inherits various safety problems of C.<\/p>\n<p>Fortunately, we can improve this situation, providing a safer and more ergonomic interface to WebGPU from Swift that feels like it belongs in Swift. More importantly, we can do so without changing the WebGPU implementation: Swift provides a suite of annotations that you can apply to C headers to improve the way in which the C APIs are expressed in Swift. These annotations describe common conventions in C that match up with Swift constructs, projecting a more Swift-friendly interface on top of the C code.<\/p>\n<p>[&#8230;]<\/p>\n<p>The problem of needing to layer information on top of existing C headers is not a new one. As noted earlier, Swift relies on a Clang feature called <a href=\"https:\/\/clang.llvm.org\/docs\/APINotes.html\">API notes<\/a> to let us express this same information in a separate file, so we don&rsquo;t have to edit the header.<\/p>\n<\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2025\/02\/17\/swift-proposal-inlinearray\/\">Swift Proposal: InlineArray<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/03\/02\/c-string-functions-in-swift-a-mallocy-story\/\">C String Functions in Swift: a malloc&rsquo;y Story<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/02\/01\/c-string-functions-in-swift\/\">C String Functions in Swift<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/01\/31\/swift-tip-opaquepointer-vs-unsafepointer\/\">Swift Tip: OpaquePointer vs. UnsafePointer<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2018\/01\/31\/swift-imports-fixed-size-c-arrays-as-tuples\/\">Swift Imports Fixed-size C Arrays As Tuples<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/01\/06\/unsafe-swift-using-pointers-and-interacting-with-c\/\">Unsafe Swift: Using Pointers and Interacting With C<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/11\/08\/passing-an-array-of-strings-from-swift-to-c\/\">Passing an Array of Strings From Swift to C<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/12\/02\/swift-and-c-libraries\/\">Swift and C Libraries<\/a><\/li>\n<\/ul>\n\n<p id=\"improving-the-usability-of-c-libraries-in-swift-update-2026-01-26\">Update (<a href=\"#improving-the-usability-of-c-libraries-in-swift-update-2026-01-26\">2026-01-26<\/a>): <a href=\"https:\/\/mastodon.social\/@tonyarnold\/115947230381701845\">Tony Arnold<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mastodon.social\/@tonyarnold\/115947230381701845\">\n<p>I am really happy to see API notes documented here.<\/p>\n<p>I used this on a shared C++\/Swift project a few years back, and it massively improved the experience of writing code against the C++ types. It&rsquo;s well worth the investment of your time to do this if you&rsquo;re in a similar spot.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Doug Gregor: The Swift code above has a very &ldquo;C&rdquo; feel to it. It has global function calls with prefixed names like wgpuInstanceCreateSurface and global integer constants like WGPUStatus_Error. It pervasively uses unsafe pointers, some of which are managed with explicit reference counting, where the user provides calls to wpuXYZAddRef and wgpuXYZRelease functions. It works, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2026-01-24T00:28:46Z","apple_news_api_id":"24fbfa0f-aae2-4d4d-9bc3-dafd894f5bcc","apple_news_api_modified_at":"2026-01-26T17:16:28Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AJPv6D6riTU2bw9r9iU9bzA","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[45,230,46,71,901],"class_list":["post-50807","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-c","tag-clang","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/50807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=50807"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/50807\/revisions"}],"predecessor-version":[{"id":50814,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/50807\/revisions\/50814"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=50807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=50807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=50807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}