File tree 2 files changed +9
-6
lines changed
2 files changed +9
-6
lines changed Original file line number Diff line number Diff line change @@ -117,8 +117,11 @@ impl Heap {
117
117
}
118
118
}
119
119
120
- /// Crates a new heap from a slice of raw memory.
121
- pub fn with_memory ( mem : & ' static mut [ MaybeUninit < u8 > ] ) -> Heap {
120
+ /// Creates a new heap from a slice of raw memory.
121
+ ///
122
+ /// This has the same effect as [`init_from_slice`] on an empty heap, but it is combined into a
123
+ /// single operation that can not panic.
124
+ pub fn from_slice ( mem : & ' static mut [ MaybeUninit < u8 > ] ) -> Heap {
122
125
let size = mem. len ( ) ;
123
126
let address = mem. as_ptr ( ) as usize ;
124
127
// SAFETY: The given address and size is valid according to the safety invariants of the
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ fn new_heap() -> Heap {
8
8
let heap_space = Box :: leak ( Box :: new ( [ MaybeUninit :: uninit ( ) ; HEAP_SIZE ] ) ) ;
9
9
let assumed_location = heap_space. as_ptr ( ) as usize ;
10
10
11
- let heap = Heap :: with_memory ( heap_space) ;
11
+ let heap = Heap :: from_slice ( heap_space) ;
12
12
assert ! ( heap. bottom == assumed_location) ;
13
13
assert ! ( heap. size == HEAP_SIZE ) ;
14
14
heap
@@ -18,11 +18,11 @@ fn new_max_heap() -> Heap {
18
18
const HEAP_SIZE : usize = 1024 ;
19
19
const HEAP_SIZE_MAX : usize = 2048 ;
20
20
let heap_space = Box :: leak ( Box :: new ( [ MaybeUninit :: < u8 > :: uninit ( ) ; HEAP_SIZE_MAX ] ) ) ;
21
- let assumed_location = heap_space. as_ptr ( ) as usize ;
21
+ let start_ptr = heap_space. as_ptr ( ) as usize ;
22
22
23
23
// Unsafe so that we have provenance over the whole allocation.
24
- let heap = unsafe { Heap :: new ( assumed_location , HEAP_SIZE ) } ;
25
- assert ! ( heap. bottom == assumed_location ) ;
24
+ let heap = unsafe { Heap :: new ( start_ptr , HEAP_SIZE ) } ;
25
+ assert ! ( heap. bottom == start_ptr ) ;
26
26
assert ! ( heap. size == HEAP_SIZE ) ;
27
27
heap
28
28
}
You can’t perform that action at this time.
0 commit comments