pub fn reserve(&mut self, num_cells: usize) -> Result<HeapWriter<'_>, AllocError> {
let section;
- let len = heap_index!(num_cells);
+ let len = heap_index_checked!(num_cells).ok_or(AllocError)?;
loop {
unsafe {
};
}
+macro_rules! heap_index_checked {
+ ($idx:expr) => {
+ std::mem::size_of::<HeapCellValue>().checked_mul($idx)
+ };
+}
+
+pub(crate) use heap_index_checked;
+
macro_rules! heap_index {
($idx:expr) => {{
let idx = $idx;
- std::mem::size_of::<HeapCellValue>()
- .checked_mul(idx)
- .expect(&format!(
- "overflow while calculating heap index {idx} * {} > {}",
- std::mem::size_of::<HeapCellValue>(),
- usize::MAX,
- ))
+ $crate::macros::heap_index_checked!(idx).expect(&format!(
+ "overflow while calculating heap index {idx} * {} > {}",
+ std::mem::size_of::<HeapCellValue>(),
+ usize::MAX,
+ ))
}};
}