uniffi_bindgen/src/bindings/swift/templates/HandleMap.swift (35 lines of code) (raw):

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 { lock.withLock { let handle = currentHandle currentHandle += 1 map[handle] = obj return handle } } func get(handle: UInt64) throws -> T { try lock.withLock { guard let obj = map[handle] else { throw UniffiInternalError.unexpectedStaleHandle } return obj } } @discardableResult func remove(handle: UInt64) throws -> T { try lock.withLock { guard let obj = map.removeValue(forKey: handle) else { throw UniffiInternalError.unexpectedStaleHandle } return obj } } var count: Int { get { map.count } } }