Swift Binding Generation via UniFFI #431
Open
+901
−168
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adds optional Swift binding generation using Mozilla's UniFFI behind a feature flag. Non-breaking, minimal changes
(~300 lines).
Approach
Uses Mozilla's UniFFI to automatically generate Swift bindings from Rust:
Changes
This PR will add 4 new files behind #[cfg(feature = "uniffi")]:
src/uniffi_bindings.rs
(~60 lines) - Thin Rust wrapper exposing functions to UniFFIsrc/tiktoken.udl
(~25 lines) - UniFFI interface definitionuniffi.toml
(~5 lines) - UniFFI configbuild_xcframework.sh
- Build script to build the Rust library for Apple platforms and bundle it into a cross-platformXCFramework
Usage
For Swift Developers
You can either add the XCFramework to your project directly, or use the Swift package I created: TiktokenSwift, which includes prebuilt bindings, tests, documentation, and an example project.To add the Swift package:
Swift API Example
Supports all encodings: cl100k_base, o200k_base, r50k_base, p50k_base
For Maintainers/Contributors
To regenerate bindings from source, first make sure you install the required Rust targets:
To build just the cross-device Rust framework, run:
To generate the bindings, run:
To build the frameworks, generate the bindings, and package everything together as an XCFramework, run:
Testing
Impact