File tree Expand file tree Collapse file tree 2 files changed +9
-6
lines changed
Expand file tree Collapse file tree 2 files changed +9
-6
lines changed Original file line number Diff line number Diff line change @@ -117,8 +117,11 @@ impl Heap {
117117 }
118118 }
119119
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 {
122125 let size = mem. len ( ) ;
123126 let address = mem. as_ptr ( ) as usize ;
124127 // 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 {
88 let heap_space = Box :: leak ( Box :: new ( [ MaybeUninit :: uninit ( ) ; HEAP_SIZE ] ) ) ;
99 let assumed_location = heap_space. as_ptr ( ) as usize ;
1010
11- let heap = Heap :: with_memory ( heap_space) ;
11+ let heap = Heap :: from_slice ( heap_space) ;
1212 assert ! ( heap. bottom == assumed_location) ;
1313 assert ! ( heap. size == HEAP_SIZE ) ;
1414 heap
@@ -18,11 +18,11 @@ fn new_max_heap() -> Heap {
1818 const HEAP_SIZE : usize = 1024 ;
1919 const HEAP_SIZE_MAX : usize = 2048 ;
2020 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 ;
2222
2323 // 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 ) ;
2626 assert ! ( heap. size == HEAP_SIZE ) ;
2727 heap
2828}
You can’t perform that action at this time.
0 commit comments