Skip to content

Commit

Permalink
Adds write_pod() to tiered storage's ByteBlockWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo committed Dec 12, 2023
1 parent 39a3566 commit 85cbb0f
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions accounts-db/src/tiered_storage/byte_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,20 @@ impl ByteBlockWriter {
self.len
}

/// Write plain ol' data to the internal buffer of the ByteBlockWriter instance
///
/// Prefer this over `write_type()`, as it prevents some undefined behavior.
pub fn write_pod<T: bytemuck::NoUninit>(&mut self, value: &T) -> IoResult<usize> {
let bytes = bytemuck::bytes_of(value);
self.write(bytes)?;
Ok(mem::size_of::<T>())
}

/// Write the specified typed instance to the internal buffer of
/// the ByteBlockWriter instance.
///
/// Prefer `write_pod()` when possible, because `write_type()` may cause
/// undefined behavior if `value` contains uninitized bytes.
pub fn write_type<T>(&mut self, value: &T) -> IoResult<usize> {
let size = mem::size_of::<T>();
let ptr = value as *const _ as *const u8;
Expand All @@ -73,10 +85,10 @@ impl ByteBlockWriter {
) -> IoResult<usize> {
let mut size = 0;
if let Some(rent_epoch) = opt_fields.rent_epoch {
size += self.write_type(&rent_epoch)?;
size += self.write_pod(&rent_epoch)?;
}
if let Some(hash) = opt_fields.account_hash {
size += self.write_type(&hash)?;
size += self.write_pod(&hash)?;
}

debug_assert_eq!(size, opt_fields.size());
Expand Down Expand Up @@ -169,7 +181,7 @@ mod tests {
let mut writer = ByteBlockWriter::new(format);
let value: u32 = 42;

writer.write_type(&value).unwrap();
writer.write_pod(&value).unwrap();
assert_eq!(writer.raw_len(), mem::size_of::<u32>());

let buffer = writer.finish().unwrap();
Expand Down

0 comments on commit 85cbb0f

Please sign in to comment.