1
+ use std:: alloc:: Allocator ;
2
+
1
3
#[ repr( u8 ) ]
2
4
#[ derive( PartialEq , Eq , Clone , Debug , Copy , Hash ) ]
3
5
pub enum ChildPos {
@@ -10,25 +12,23 @@ pub enum ChildPos {
10
12
/// path is built from left to right, since it's parsed right to left when
11
13
/// followed).
12
14
#[ derive( Clone , Debug , PartialEq ) ]
13
- pub struct PathBuilder {
15
+ pub struct PathBuilder < A : Allocator > {
14
16
// TODO: It might make sense to implement small object optimization here.
15
17
// The vast majority of paths are just a single byte, statically allocate 8
16
18
// would seem reasonable
17
- store : Vec < u8 > ,
19
+ store : Vec < u8 , A > ,
18
20
/// the bit the next write will happen to (counts down)
19
21
bit_pos : u8 ,
20
22
}
21
23
22
- impl Default for PathBuilder {
23
- fn default ( ) -> Self {
24
+ impl < A : Allocator > PathBuilder < A > {
25
+ pub fn new ( allocator : A ) -> Self {
24
26
Self {
25
- store : Vec :: with_capacity ( 16 ) ,
27
+ store : Vec :: with_capacity_in ( 16 , allocator ) ,
26
28
bit_pos : 7 ,
27
29
}
28
30
}
29
- }
30
31
31
- impl PathBuilder {
32
32
pub fn clear ( & mut self ) {
33
33
self . bit_pos = 7 ;
34
34
self . store . clear ( ) ;
@@ -49,7 +49,7 @@ impl PathBuilder {
49
49
}
50
50
}
51
51
52
- pub fn done ( mut self ) -> Vec < u8 > {
52
+ pub fn done ( mut self ) -> Vec < u8 , A > {
53
53
if self . bit_pos < 7 {
54
54
let right_shift = self . bit_pos + 1 ;
55
55
let left_shift = 7 - self . bit_pos ;
@@ -138,9 +138,10 @@ mod tests {
138
138
use crate :: serde:: serialized_length_atom;
139
139
use hex;
140
140
use rstest:: rstest;
141
+ use std:: alloc:: System ;
141
142
142
- fn build_path ( input : & [ u8 ] ) -> PathBuilder {
143
- let mut path = PathBuilder :: default ( ) ;
143
+ fn build_path ( input : & [ u8 ] ) -> PathBuilder < System > {
144
+ let mut path = PathBuilder :: new ( System ) ;
144
145
// keep in mind that paths are built in reverse order (starting from the
145
146
// target).
146
147
for ( idx, b) in input. iter ( ) . enumerate ( ) {
@@ -215,7 +216,7 @@ mod tests {
215
216
#[ case( 80 , 80 , "ffffffffffffffffffff" ) ]
216
217
#[ case( 80 , 79 , "7fffffffffffffffffff" ) ]
217
218
fn test_truncate ( #[ case] num_bits : usize , #[ case] truncate : u32 , #[ case] expect : & str ) {
218
- let mut path = PathBuilder :: default ( ) ;
219
+ let mut path = PathBuilder :: new ( System ) ;
219
220
for _i in 0 ..num_bits {
220
221
path. push ( ChildPos :: Right ) ;
221
222
}
@@ -260,7 +261,7 @@ mod tests {
260
261
#[ case( 80 , 15 , "ffff" ) ]
261
262
#[ case( 80 , 79 , "ffffffffffffffffffff" ) ]
262
263
fn test_truncate_add ( #[ case] num_bits : usize , #[ case] truncate : u32 , #[ case] expect : & str ) {
263
- let mut path = PathBuilder :: default ( ) ;
264
+ let mut path = PathBuilder :: new ( System ) ;
264
265
for _i in 0 ..num_bits {
265
266
path. push ( ChildPos :: Right ) ;
266
267
}
@@ -274,7 +275,7 @@ mod tests {
274
275
fn test_clear (
275
276
#[ values( 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 ) ] num_bits : usize ,
276
277
) {
277
- let mut path = PathBuilder :: default ( ) ;
278
+ let mut path = PathBuilder :: new ( System ) ;
278
279
for _i in 0 ..num_bits {
279
280
path. push ( ChildPos :: Right ) ;
280
281
}
@@ -331,7 +332,7 @@ mod tests {
331
332
#[ case( 513 ) ]
332
333
#[ case( 0xfff9 ) ]
333
334
fn test_serialized_length ( #[ case] num_bits : u32 ) {
334
- let mut path = PathBuilder :: default ( ) ;
335
+ let mut path = PathBuilder :: new ( System ) ;
335
336
for _ in 0 ..num_bits {
336
337
path. push ( ChildPos :: Right ) ;
337
338
}
0 commit comments