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]
|
/// get '&mut EntryType' from cache file [ix]
|
||||||
fn get_mut(&mut self, ix: u64) -> &mut EntryType {
|
fn get_mut(&mut self, ix: u64) -> &mut EntryType {
|
||||||
let item_slice = self.get_slice_internal(ix);
|
let start = self.get_element_offset_byte(ix);
|
||||||
unsafe {
|
let end = start + std::mem::size_of::<EntryType>();
|
||||||
let item = item_slice.as_ptr() as *mut EntryType;
|
assert!(
|
||||||
&mut *item
|
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..]
|
/// get '&[EntryType]' from cache file [ix..]
|
||||||
fn get_slice(&self, ix: u64) -> &[EntryType] {
|
fn get_slice(&self, ix: u64) -> &[EntryType] {
|
||||||
let start = self.get_element_offset_byte(ix);
|
let start = self.get_element_offset_byte(ix);
|
||||||
let item_slice: &[u8] = &self.mmap[start..];
|
let bytes = &self.mmap[start..];
|
||||||
let remaining_elements = item_slice.len() / std::mem::size_of::<EntryType>();
|
// the `bytes` slice *must* contain whole `EntryType`s
|
||||||
unsafe {
|
debug_assert_eq!(bytes.len() % std::mem::size_of::<EntryType>(), 0);
|
||||||
let item = item_slice.as_ptr() as *const EntryType;
|
bytemuck::cast_slice(bytes)
|
||||||
std::slice::from_raw_parts(item, remaining_elements)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// return byte offset of entry 'ix' into a slice which contains a header and at least ix elements
|
/// 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
|
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 {
|
fn get_header_mut(&mut self) -> &mut Header {
|
||||||
let bytes = &mut self.mmap[..std::mem::size_of::<Header>()];
|
let bytes = &mut self.mmap[..std::mem::size_of::<Header>()];
|
||||||
bytemuck::from_bytes_mut(bytes)
|
bytemuck::from_bytes_mut(bytes)
|
||||||
|
|
Loading…
Reference in New Issue