@@ -3,17 +3,32 @@ use std::hash::{BuildHasher, Hash};
33
44/// The trait required to be able to use a type in `BytePool`.
55pub trait Poolable {
6+ fn empty ( & self ) -> bool ;
7+ fn len ( & self ) -> usize ;
68 fn capacity ( & self ) -> usize ;
79 fn alloc ( size : usize ) -> Self ;
10+ fn reset ( & mut self ) ;
811}
912
1013impl < 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 {
1219 self . len ( )
1320 }
1421
22+ fn capacity ( & self ) -> usize {
23+ self . capacity ( )
24+ }
25+
1526 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 ( ) ;
1732 }
1833}
1934
@@ -22,13 +37,25 @@ where
2237 K : Eq + Hash ,
2338 S : BuildHasher + Default ,
2439{
25- fn capacity ( & self ) -> usize {
40+ fn empty ( & self ) -> bool {
41+ self . len ( ) == 0
42+ }
43+
44+ fn len ( & self ) -> usize {
2645 self . len ( )
2746 }
2847
48+ fn capacity ( & self ) -> usize {
49+ self . capacity ( )
50+ }
51+
2952 fn alloc ( size : usize ) -> Self {
3053 HashMap :: with_capacity_and_hasher ( size, Default :: default ( ) )
3154 }
55+
56+ fn reset ( & mut self ) {
57+ self . clear ( ) ;
58+ }
3259}
3360
3461/// A trait allowing for efficient reallocation.
@@ -42,7 +69,7 @@ impl<T: Default + Clone> Realloc for Vec<T> {
4269
4370 assert ! ( new_size > 0 ) ;
4471 match new_size. cmp ( & self . capacity ( ) ) {
45- Greater => self . resize ( new_size, T :: default ( ) ) ,
72+ Greater => self . reserve ( new_size - self . capacity ( ) ) ,
4673 Less => {
4774 self . truncate ( new_size) ;
4875 self . shrink_to_fit ( ) ;
0 commit comments