Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/python/templates/HandleMap.py

raw
1 class _UniffiHandleMap:
2 """
3 A map where inserting, getting and removing data is synchronized with a lock.
4 """
5
6 def __init__(self):
7 # type Handle = int
8 self._map = {} # type: Dict[Handle, Any]
9 self._lock = threading.Lock()
10 self._counter = itertools.count()
11
12 def insert(self, obj):
13 with self._lock:
14 handle = next(self._counter)
15 self._map[handle] = obj
16 return handle
17
18 def get(self, handle):
19 try:
20 with self._lock:
21 return self._map[handle]
22 except KeyError:
23 raise InternalError("_UniffiHandleMap.get: Invalid handle")
24
25 def remove(self, handle):
26 try:
27 with self._lock:
28 return self._map.pop(handle)
29 except KeyError:
30 raise InternalError("_UniffiHandleMap.remove: Invalid handle")
31
32 def __len__(self):
33 return len(self._map)