@@ -2,8 +2,62 @@ use std::collections::HashMap;
22use std:: collections:: hash_map:: Entry ;
33use std:: fmt:: { self , Debug } ;
44use std:: hash:: Hash ;
5+ use std:: ops:: { Index , IndexMut } ;
56
6- use slab:: Slab ;
7+ /// A simple allocator.
8+ struct Slab < T > {
9+ map : HashMap < usize , T > ,
10+ counter : usize ,
11+ }
12+
13+ impl < T > Slab < T > {
14+ fn new ( ) -> Self {
15+ Self { map : HashMap :: new ( ) , counter : 0 }
16+ }
17+
18+ #[ cfg( test) ]
19+ fn len ( & self ) -> usize {
20+ self . map . len ( )
21+ }
22+
23+ fn insert ( & mut self , value : T ) -> usize {
24+ self . counter += 1 ;
25+ self . map . insert ( self . counter , value) ;
26+ self . counter
27+ }
28+
29+ fn contains ( & self , i : usize ) -> bool {
30+ self . map . contains_key ( & i)
31+ }
32+
33+ fn retain ( & mut self , mut f : impl FnMut ( usize , & mut T ) -> bool ) {
34+ self . map . retain ( |i, v| f ( * i, v) ) ;
35+ }
36+
37+ fn remove ( & mut self , i : usize ) {
38+ self . map . remove ( & i) ;
39+ }
40+ }
41+
42+ impl < T > Index < usize > for Slab < T > {
43+ type Output = T ;
44+
45+ fn index ( & self , index : usize ) -> & Self :: Output {
46+ & self . map [ & index]
47+ }
48+ }
49+
50+ impl < T > IndexMut < usize > for Slab < T > {
51+ fn index_mut ( & mut self , index : usize ) -> & mut Self :: Output {
52+ self . map . get_mut ( & index) . unwrap ( )
53+ }
54+ }
55+
56+ impl < T > Default for Slab < T > {
57+ fn default ( ) -> Self {
58+ Self :: new ( )
59+ }
60+ }
761
862/// A deduplicated sequence of calls to tracked functions.
963///
0 commit comments