Refactors out `unsafe` from cache_hash_data.rs (#33271)
This commit is contained in:
parent
a47f65d882
commit
6283c1d568
|
@ -138,22 +138,25 @@ impl CacheHashDataFile {
|
|||
|
||||
/// get '&mut EntryType' from cache file [ix]
|
||||
fn get_mut(&mut self, ix: u64) -> &mut EntryType {
|
||||
let item_slice = self.get_slice_internal(ix);
|
||||
unsafe {
|
||||
let item = item_slice.as_ptr() as *mut EntryType;
|
||||
&mut *item
|
||||
}
|
||||
let start = self.get_element_offset_byte(ix);
|
||||
let end = start + std::mem::size_of::<EntryType>();
|
||||
assert!(
|
||||
end <= self.capacity as usize,
|
||||
"end: {end}, capacity: {}, ix: {ix}, cell size: {}",
|
||||
self.capacity,
|
||||
self.cell_size,
|
||||
);
|
||||
let bytes = &mut self.mmap[start..end];
|
||||
bytemuck::from_bytes_mut(bytes)
|
||||
}
|
||||
|
||||
/// get '&[EntryType]' from cache file [ix..]
|
||||
fn get_slice(&self, ix: u64) -> &[EntryType] {
|
||||
let start = self.get_element_offset_byte(ix);
|
||||
let item_slice: &[u8] = &self.mmap[start..];
|
||||
let remaining_elements = item_slice.len() / std::mem::size_of::<EntryType>();
|
||||
unsafe {
|
||||
let item = item_slice.as_ptr() as *const EntryType;
|
||||
std::slice::from_raw_parts(item, remaining_elements)
|
||||
}
|
||||
let bytes = &self.mmap[start..];
|
||||
// the `bytes` slice *must* contain whole `EntryType`s
|
||||
debug_assert_eq!(bytes.len() % std::mem::size_of::<EntryType>(), 0);
|
||||
bytemuck::cast_slice(bytes)
|
||||
}
|
||||
|
||||
/// return byte offset of entry 'ix' into a slice which contains a header and at least ix elements
|
||||
|
@ -163,21 +166,6 @@ impl CacheHashDataFile {
|
|||
start
|
||||
}
|
||||
|
||||
/// get the bytes representing cache file [ix]
|
||||
fn get_slice_internal(&self, ix: u64) -> &[u8] {
|
||||
let start = self.get_element_offset_byte(ix);
|
||||
let end = start + std::mem::size_of::<EntryType>();
|
||||
assert!(
|
||||
end <= self.capacity as usize,
|
||||
"end: {}, capacity: {}, ix: {}, cell size: {}",
|
||||
end,
|
||||
self.capacity,
|
||||
ix,
|
||||
self.cell_size
|
||||
);
|
||||
&self.mmap[start..end]
|
||||
}
|
||||
|
||||
fn get_header_mut(&mut self) -> &mut Header {
|
||||
let bytes = &mut self.mmap[..std::mem::size_of::<Header>()];
|
||||
bytemuck::from_bytes_mut(bytes)
|
||||
|
|
Loading…
Reference in New Issue