Bug 1870642 - Fix Collection deleted snackbar that overlaps the toolbar r=android...
[gecko.git] / third_party / rust / uniffi_bindgen / src / bindings / kotlin / templates / HandleMap.kt
blob3a5664819035a20c7a21f22c092fd534f1f83d47
1 // Map handles to objects
2 //
3 // This is used pass an opaque 64-bit handle representing a foreign object to the Rust code.
4 internal class UniffiHandleMap<T: Any> {
5     private val map = ConcurrentHashMap<Long, T>()
6     private val counter = java.util.concurrent.atomic.AtomicLong(0)
8     val size: Int
9         get() = map.size
11     // Insert a new object into the handle map and get a handle for it
12     fun insert(obj: T): Long {
13         val handle = counter.getAndAdd(1)
14         map.put(handle, obj)
15         return handle
16     }
18     // Get an object from the handle map
19     fun get(handle: Long): T {
20         return map.get(handle) ?: throw InternalException("UniffiHandleMap.get: Invalid handle")
21     }
23     // Remove an entry from the handlemap and get the Kotlin object back
24     fun remove(handle: Long): T {
25         return map.remove(handle) ?: throw InternalException("UniffiHandleMap: Invalid handle")
26     }