11use super :: * ;
2+ use std:: cell:: Cell ;
23
34#[ test]
45fn allocator_param ( ) {
@@ -17,17 +18,17 @@ fn allocator_param() {
1718 // A dumb allocator that consumes a fixed amount of fuel
1819 // before allocation attempts start failing.
1920 struct BoundedAlloc {
20- fuel : usize ,
21+ fuel : Cell < usize > ,
2122 }
2223 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 > {
2425 let size = layout. size ( ) ;
25- if size > self . fuel {
26+ if size > self . fuel . get ( ) {
2627 return Err ( AllocErr ) ;
2728 }
2829 match Global . alloc ( layout) {
2930 ok @ Ok ( _) => {
30- self . fuel -= size;
31+ self . fuel . update ( |old| old - size) ;
3132 ok
3233 }
3334 err @ Err ( _) => err,
@@ -38,11 +39,11 @@ fn allocator_param() {
3839 }
3940 }
4041
41- let a = BoundedAlloc { fuel : 500 } ;
42+ let a = BoundedAlloc { fuel : Cell :: new ( 500 ) } ;
4243 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 ) ;
4445 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 ) ;
4647}
4748
4849#[ test]
0 commit comments