1
1
use super :: * ;
2
+ use std:: cell:: Cell ;
2
3
3
4
#[ test]
4
5
fn allocator_param ( ) {
@@ -17,17 +18,17 @@ fn allocator_param() {
17
18
// A dumb allocator that consumes a fixed amount of fuel
18
19
// before allocation attempts start failing.
19
20
struct BoundedAlloc {
20
- fuel : usize ,
21
+ fuel : Cell < usize > ,
21
22
}
22
23
unsafe impl AllocRef for BoundedAlloc {
23
- fn alloc ( & mut self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocErr > {
24
+ fn alloc ( & self , layout : Layout ) -> Result < NonNull < [ u8 ] > , AllocErr > {
24
25
let size = layout. size ( ) ;
25
- if size > self . fuel {
26
+ if size > self . fuel . get ( ) {
26
27
return Err ( AllocErr ) ;
27
28
}
28
29
match Global . alloc ( layout) {
29
30
ok @ Ok ( _) => {
30
- self . fuel -= size;
31
+ self . fuel . update ( |old| old - size) ;
31
32
ok
32
33
}
33
34
err @ Err ( _) => err,
@@ -38,11 +39,11 @@ fn allocator_param() {
38
39
}
39
40
}
40
41
41
- let a = BoundedAlloc { fuel : 500 } ;
42
+ let a = BoundedAlloc { fuel : Cell :: new ( 500 ) } ;
42
43
let mut v: RawVec < u8 , _ > = RawVec :: with_capacity_in ( 50 , a) ;
43
- assert_eq ! ( v. alloc. fuel, 450 ) ;
44
+ assert_eq ! ( v. alloc. fuel. get ( ) , 450 ) ;
44
45
v. reserve ( 50 , 150 ) ; // (causes a realloc, thus using 50 + 150 = 200 units of fuel)
45
- assert_eq ! ( v. alloc. fuel, 250 ) ;
46
+ assert_eq ! ( v. alloc. fuel. get ( ) , 250 ) ;
46
47
}
47
48
48
49
#[ test]
0 commit comments