@@ -35,37 +35,55 @@ impl<const N: usize> Chonk<N> {
35
35
}
36
36
}
37
37
38
- pub struct OwnedHeap < F > {
38
+ pub struct Dropper < const N : usize > {
39
+ putter : * mut Chonk < N > ,
40
+ }
41
+
42
+ impl < const N : usize > Dropper < N > {
43
+ fn new ( putter : * mut Chonk < N > ) -> Self {
44
+ Self { putter }
45
+ }
46
+ }
47
+
48
+ impl < const N : usize > Drop for Dropper < N > {
49
+ fn drop ( & mut self ) {
50
+ unsafe { Chonk :: unleak ( self . putter ) }
51
+ }
52
+ }
53
+
54
+ pub struct OwnedHeap < const N : usize > {
39
55
heap : Heap ,
40
- _drop : F ,
56
+ _drop : Dropper < N > ,
41
57
}
42
58
43
- impl < F > Deref for OwnedHeap < F > {
59
+ impl < const N : usize > Deref for OwnedHeap < N > {
44
60
type Target = Heap ;
45
61
46
62
fn deref ( & self ) -> & Self :: Target {
47
63
& self . heap
48
64
}
49
65
}
50
66
51
- impl < F > DerefMut for OwnedHeap < F > {
67
+ impl < const N : usize > DerefMut for OwnedHeap < N > {
52
68
fn deref_mut ( & mut self ) -> & mut Self :: Target {
53
69
& mut self . heap
54
70
}
55
71
}
56
72
57
- pub fn new_heap ( ) -> OwnedHeap < impl Sized > {
73
+ pub fn new_heap ( ) -> OwnedHeap < 1000 > {
58
74
const HEAP_SIZE : usize = 1000 ;
59
75
let ( heap_space_ptr, data_ptr) = Chonk :: < HEAP_SIZE > :: new ( ) ;
60
76
61
77
let heap = unsafe { Heap :: new ( data_ptr, HEAP_SIZE ) } ;
62
78
assert_eq ! ( heap. bottom( ) , data_ptr) ;
63
79
assert_eq ! ( heap. size( ) , align_down_size( HEAP_SIZE , size_of:: <usize >( ) ) ) ;
64
- let drop = move || unsafe { Chonk :: unleak ( heap_space_ptr) } ;
65
- OwnedHeap { heap, _drop : drop }
80
+ OwnedHeap {
81
+ heap,
82
+ _drop : Dropper :: new ( heap_space_ptr) ,
83
+ }
66
84
}
67
85
68
- fn new_max_heap ( ) -> OwnedHeap < impl Sized > {
86
+ fn new_max_heap ( ) -> OwnedHeap < 2048 > {
69
87
const HEAP_SIZE : usize = 1024 ;
70
88
const HEAP_SIZE_MAX : usize = 2048 ;
71
89
let ( heap_space_ptr, data_ptr) = Chonk :: < HEAP_SIZE_MAX > :: new ( ) ;
@@ -75,18 +93,21 @@ fn new_max_heap() -> OwnedHeap<impl Sized> {
75
93
assert_eq ! ( heap. bottom( ) , data_ptr) ;
76
94
assert_eq ! ( heap. size( ) , HEAP_SIZE ) ;
77
95
78
- let drop = move || unsafe { Chonk :: unleak ( heap_space_ptr) } ;
79
- OwnedHeap { heap, _drop : drop }
96
+ OwnedHeap {
97
+ heap,
98
+ _drop : Dropper :: new ( heap_space_ptr) ,
99
+ }
80
100
}
81
101
82
- fn new_heap_skip ( ct : usize ) -> OwnedHeap < impl Sized > {
102
+ fn new_heap_skip ( ct : usize ) -> OwnedHeap < 1000 > {
83
103
const HEAP_SIZE : usize = 1000 ;
84
104
let ( heap_space_ptr, data_ptr) = Chonk :: < HEAP_SIZE > :: new ( ) ;
85
105
86
106
let heap = unsafe { Heap :: new ( data_ptr. add ( ct) , HEAP_SIZE - ct) } ;
87
-
88
- let drop = move || unsafe { Chonk :: unleak ( heap_space_ptr) } ;
89
- OwnedHeap { heap, _drop : drop }
107
+ OwnedHeap {
108
+ heap,
109
+ _drop : Dropper :: new ( heap_space_ptr) ,
110
+ }
90
111
}
91
112
92
113
#[ test]
0 commit comments