Where Swift Ends and Rust Begins
Designing a native interface around a portable systems engine.

A useful language boundary starts with responsibilities. When a user opens an archive, somebody has to handle the file picker, keep the interface responsive, read the archive, and decide what to show when the scan fails. Peel and Rind give us a context for asking where that work belongs.
Consider a native Swift interface backed by a portable Rust engine. Swift can own presentation, user interaction, and the platform lifecycle. Rust can own archive parsing, codec orchestration, and the rules for what counts as a complete result. The interface between them has to explain what happens when a call outlasts the screen that started it.
The language boundary
A foreign function interface, or FFI, lets code in one language call code in another. A C-compatible application binary interface, or ABI, sets the calling convention and data layout those calls use. It can carry a status code, a buffer with a length, or an opaque handle. Both sides still need a written agreement about how long each value lives, which threads may use it, and how it gets cleaned up (Figure 1). [1] [2]
Fig. 01
Who owns each decision?
Open each part of the proposed design.
Swift interface
Start the scan from a user action, manage its handle, and publish progress and results to the interface on the main actor.
C-compatible ABI
Define how handles, buffers, status codes and versioned records are represented. Specify the valid calls, ownership and release rules.
Rust engine
Run the portable scan, check for cancellation, and produce progress snapshots and an immutable final result or a structured error.
Errors need their own contract. An unreadable archive should come back as a structured failure that Swift can map to a message. A panic is a different event. If a Rust panic attempts to escape a non-unwinding extern "C" entry point, the process aborts. If the design calls for converting an internal panic to a failure, containment has to happen inside Rust, and catch_unwind only catches unwinding panics. An engine-owned worker also needs its own failure handling because it runs after the entry point returns. [1] [3]
Who can keep the bytes?
A pointer can describe where data lives without granting the receiver permission to keep it. Swift can temporarily lend bytes to a synchronous Rust call. If Rust queues the input for later work, it needs an owned copy or a separately defined lifetime agreement before the borrow ends. Apple documents that pointers Swift creates through its implicit argument conversions are only guaranteed valid during that call (Figure 2). [2]
Fig. 02 / Interactive
Follow a buffer across the boundary.
- Owner
- Lifetime
- Cleanup
Swift keeps ownership of borrowed input for a synchronous call. For an asynchronous job, the bytes are copied into Rust-owned storage before the start call returns. Swift copies an engine-owned result into a native value and releases the original through the ABI.
The same question applies to handles. An opaque scan handle gives the caller a reference to one job while Rust keeps its state private. Returning immutable records allows Swift to render results after the job is released, provided the Swift wrapper has copied the data it needs. If Rust allocated a result buffer, the ABI should expose the matching Rust release operation. Document any record layout, string encoding, and bounds so both sides agree on what the bytes represent. [1]
An asynchronous archive scan
Suppose the user clicks Cancel while a scan is running. Swift can relay a request, but the worker may still be reading a batch. The wrapper keeps the handle alive until the engine reaches a terminal state and has finished using job-owned resources. In this example Swift polls for progress on a path that keeps the UI responsive. The wrapper serializes foreign calls for a single handle, while Rust synchronizes those calls with its worker. Swift cancellation is cooperative too, so the wrapper has to forward it explicitly (Figure 3). [4]
Fig. 03 / Interactive
What happens after Cancel?
Starting creates a live handle. A cancellation request leaves it live until the worker acknowledges cancellation at a checkpoint. Completion, failure and acknowledged cancellation all let the wrapper release the handle.
Completion can win a real race if the engine publishes a terminal result before observing the cancellation request. Give each job one terminal outcome. After Swift replaces one scan with another, tag updates with their job identity so a late outcome from the old job cannot overwrite the new screen. The wrapper still owes the old job its release, even if its UI is gone.
How to investigate it
Prototype a small C-compatible interface around a harmless metadata reader. Use a deterministic fixture for each outcome and make the boundary rules testable from both languages.
- Write the contract. Record which calls accept an empty input or a null pointer, how lengths are checked, who owns each allocation, and which operations may overlap.
- Exercise the lifecycle. Cover normal completion, invalid metadata, repeated create and destroy cycles, cancellation before work, cancellation during work, and cancellation after the terminal state. Use controlled checkpoints to exercise race orderings reproducibly.
- Check concurrent use. Test independent jobs in parallel, attempts to overlap calls on a shared job through the wrapper, and teardown while updates are pending. Verify the UI observes one terminal outcome and releases each handle once.
- Measure before changing the interface. Compare per-record crossings with batched snapshots over the same fixture. Record call count, copied bytes, wall time, allocations, and cancellation latency. Validate that both versions return equivalent results.
This is a proposed experiment. A reproducible report would include the fixture manifest, Swift and Rust toolchain versions, build and run settings, raw observations, and verification criteria.
What this means
Keep native lifecycle and presentation in Swift and portable archive behavior in Rust where that split serves the product. Give the boundary enough vocabulary to explain who owns the work, when it ends, and what the caller can rely on afterward.
The boundary is complete when both sides can account for a job from the first call to the last release.
References and further reading
- The Rustonomicon. Foreign Function Interface.
- Apple Developer. Calling Functions With Pointer Parameters.
- Rust standard library.
std::panic::catch_unwind. - The Swift Programming Language. Concurrency.
- Swift. Documentation.