From 85cbb0f8a89a8ef67642b39eb1925e3f749173c9 Mon Sep 17 00:00:00 2001 From: brooks Date: Mon, 11 Dec 2023 19:24:03 -0500 Subject: [PATCH] Adds `write_pod()` to tiered storage's ByteBlockWriter --- accounts-db/src/tiered_storage/byte_block.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/accounts-db/src/tiered_storage/byte_block.rs b/accounts-db/src/tiered_storage/byte_block.rs index 6ddd66c2875b24..0e4f344bea7c31 100644 --- a/accounts-db/src/tiered_storage/byte_block.rs +++ b/accounts-db/src/tiered_storage/byte_block.rs @@ -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(&mut self, value: &T) -> IoResult { + let bytes = bytemuck::bytes_of(value); + self.write(bytes)?; + Ok(mem::size_of::()) + } + /// 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(&mut self, value: &T) -> IoResult { let size = mem::size_of::(); let ptr = value as *const _ as *const u8; @@ -73,10 +85,10 @@ impl ByteBlockWriter { ) -> IoResult { 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()); @@ -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::()); let buffer = writer.finish().unwrap();