@@ -3,17 +3,32 @@ use std::hash::{BuildHasher, Hash};
3
3
4
4
/// The trait required to be able to use a type in `BytePool`.
5
5
pub trait Poolable {
6
+ fn empty ( & self ) -> bool ;
7
+ fn len ( & self ) -> usize ;
6
8
fn capacity ( & self ) -> usize ;
7
9
fn alloc ( size : usize ) -> Self ;
10
+ fn reset ( & mut self ) ;
8
11
}
9
12
10
13
impl < T : Default + Clone > Poolable for Vec < T > {
11
- fn capacity ( & self ) -> usize {
14
+ fn empty ( & self ) -> bool {
15
+ self . len ( ) == 0
16
+ }
17
+
18
+ fn len ( & self ) -> usize {
12
19
self . len ( )
13
20
}
14
21
22
+ fn capacity ( & self ) -> usize {
23
+ self . capacity ( )
24
+ }
25
+
15
26
fn alloc ( size : usize ) -> Self {
16
- vec ! [ T :: default ( ) ; size]
27
+ Vec :: < T > :: with_capacity ( size)
28
+ }
29
+
30
+ fn reset ( & mut self ) {
31
+ self . clear ( ) ;
17
32
}
18
33
}
19
34
@@ -22,13 +37,25 @@ where
22
37
K : Eq + Hash ,
23
38
S : BuildHasher + Default ,
24
39
{
25
- fn capacity ( & self ) -> usize {
40
+ fn empty ( & self ) -> bool {
41
+ self . len ( ) == 0
42
+ }
43
+
44
+ fn len ( & self ) -> usize {
26
45
self . len ( )
27
46
}
28
47
48
+ fn capacity ( & self ) -> usize {
49
+ self . capacity ( )
50
+ }
51
+
29
52
fn alloc ( size : usize ) -> Self {
30
53
HashMap :: with_capacity_and_hasher ( size, Default :: default ( ) )
31
54
}
55
+
56
+ fn reset ( & mut self ) {
57
+ self . clear ( ) ;
58
+ }
32
59
}
33
60
34
61
/// A trait allowing for efficient reallocation.
@@ -42,7 +69,7 @@ impl<T: Default + Clone> Realloc for Vec<T> {
42
69
43
70
assert ! ( new_size > 0 ) ;
44
71
match new_size. cmp ( & self . capacity ( ) ) {
45
- Greater => self . resize ( new_size, T :: default ( ) ) ,
72
+ Greater => self . reserve ( new_size - self . capacity ( ) ) ,
46
73
Less => {
47
74
self . truncate ( new_size) ;
48
75
self . shrink_to_fit ( ) ;
0 commit comments