Coverage for wasmtime/_slab.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-12-22 16:28 +0000

1from typing import Generic, List, Union, cast, TypeVar 

2 

3 

4T = TypeVar('T') 

5 

6 

7class Slab(Generic[T]): 

8 list: List[Union[int, T]] 

9 next: int 

10 

11 def __init__(self) -> None: 

12 self.list = [] 

13 self.next = 0 

14 

15 def allocate(self, val: T) -> int: 

16 idx = self.next 

17 

18 if len(self.list) == idx: 

19 self.list.append(0) 

20 self.next += 1 

21 else: 

22 self.next = cast(int, self.list[idx]) 

23 

24 self.list[idx] = val 

25 return idx 

26 

27 def get(self, idx: int) -> T: 

28 return cast(T, self.list[idx]) 

29 

30 def deallocate(self, idx: int) -> None: 

31 self.list[idx] = self.next 

32 self.next = idx