Refactors out `unsafe` from MmapAccountHashesFile::read() (#33266)

This commit is contained in:
Brooks 2023-09-15 11:09:01 -04:00 committed by GitHub
parent d4946ddfaa
commit 4c42413c1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 6 deletions

View File

@ -42,12 +42,9 @@ impl MmapAccountHashesFile {
/// return a slice of account hashes starting at 'index'
fn read(&self, index: usize) -> &[Hash] {
let start = std::mem::size_of::<Hash>() * index;
let item_slice: &[u8] = &self.mmap[start..self.count * std::mem::size_of::<Hash>()];
let remaining_elements = item_slice.len() / std::mem::size_of::<Hash>();
unsafe {
let item = item_slice.as_ptr() as *const Hash;
std::slice::from_raw_parts(item, remaining_elements)
}
let end = std::mem::size_of::<Hash>() * self.count;
let bytes = &self.mmap[start..end];
bytemuck::cast_slice(bytes)
}
/// write a hash to the end of mmap file.