Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/swift/templates/HandleMap.swift

raw
1 fileprivate final class UniffiHandleMap<T>: @unchecked Sendable {
2 // All mutation happens with this lock held, which is why we implement @unchecked Sendable.
3 private let lock = NSLock()
4 private var map: [UInt64: T] = [:]
5 private var currentHandle: UInt64 = 1
6
7 func insert(obj: T) -> UInt64 {
8 lock.withLock {
9 let handle = currentHandle
10 currentHandle += 1
11 map[handle] = obj
12 return handle
13 }
14 }
15
16 func get(handle: UInt64) throws -> T {
17 try lock.withLock {
18 guard let obj = map[handle] else {
19 throw UniffiInternalError.unexpectedStaleHandle
20 }
21 return obj
22 }
23 }
24
25 @discardableResult
26 func remove(handle: UInt64) throws -> T {
27 try lock.withLock {
28 guard let obj = map.removeValue(forKey: handle) else {
29 throw UniffiInternalError.unexpectedStaleHandle
30 }
31 return obj
32 }
33 }
34
35 var count: Int {
36 get {
37 map.count
38 }
39 }
40 }
41