Skip to content

Commit

Permalink
Fix some more Swift concurrency issues
Browse files Browse the repository at this point in the history
Converted some more vars to lets.  Swift still complains about async
calls.  I believe the next step would be to make all UniFFI objects
conform to `Sendable`.

See mozilla#2279.
  • Loading branch information
bendk committed Oct 31, 2024
1 parent 1c8dd50 commit a3032a3
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion uniffi_bindgen/src/bindings/swift/templates/Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private func uniffiTraitInterfaceCallAsyncWithError<T, E>(

// Borrow the callback handle map implementation to store foreign future handles
// TODO: consolidate the handle-map code (https://github.com/mozilla/uniffi-rs/pull/1823)
fileprivate var UNIFFI_FOREIGN_FUTURE_HANDLE_MAP = UniffiHandleMap<UniffiForeignFutureTask>()
fileprivate let UNIFFI_FOREIGN_FUTURE_HANDLE_MAP = UniffiHandleMap<UniffiForeignFutureTask>()

// Protocol for tasks that handle foreign futures.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ fileprivate struct {{ trait_impl }} {

// Create the VTable using a series of closures.
// Swift automatically converts these into C callback functions.
static var vtable: {{ vtable|ffi_type_name }} = {{ vtable|ffi_type_name }}(
//
// This is a var, since we pass a pointer to the Rust code, but it's not actually mutated in
// practice
static nonisolated(unsafe) var vtable: {{ vtable|ffi_type_name }} = {{ vtable|ffi_type_name }}(
{%- for (ffi_callback, meth) in vtable_methods %}
{{ meth.name()|fn_name }}: { (
{%- for arg in ffi_callback.arguments() %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@_documentation(visibility: private)
#endif
fileprivate struct {{ ffi_converter_name }} {
fileprivate static var handleMap = UniffiHandleMap<{{ type_name }}>()
fileprivate static let handleMap = UniffiHandleMap<{{ type_name }}>()
}

#if swift(>=5.8)
Expand Down
5 changes: 3 additions & 2 deletions uniffi_bindgen/src/bindings/swift/templates/HandleMap.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
fileprivate class UniffiHandleMap<T> {
private var map: [UInt64: T] = [:]
fileprivate final class UniffiHandleMap<T>: @unchecked Sendable {
// All mutation happens with this lock held, which is why we implement @unchecked Sendable.
private let lock = NSLock()
private var map: [UInt64: T] = [:]
private var currentHandle: UInt64 = 1

func insert(obj: T) -> UInt64 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ open class {{ impl_class_name }}:
#endif
public struct {{ ffi_converter_name }}: FfiConverter {
{%- if obj.has_callback_interface() %}
fileprivate static var handleMap = UniffiHandleMap<{{ type_name }}>()
fileprivate static let handleMap = UniffiHandleMap<{{ type_name }}>()
{%- endif %}

typealias FfiType = UnsafeMutableRawPointer
Expand Down
2 changes: 2 additions & 0 deletions uniffi_bindgen/src/bindings/swift/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ fn compile_swift_module<T: AsRef<OsStr>>(
command
.current_dir(out_dir)
.arg("-emit-module")
// TODO(2279): Fix concurrency issues and uncomment this
//.arg("-strict-concurrency=complete")
.arg("-module-name")
.arg(module_name)
.arg("-o")
Expand Down

0 comments on commit a3032a3

Please sign in to comment.