Skip to content

Commit

Permalink
add some asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-j97 committed Mar 13, 2024
1 parent abaa002 commit a9c26c5
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/segment/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,30 @@ impl Writer {
/// # Errors
///
/// Will return `Err` if an IO error occurs.
///
/// # Panics
///
/// Panics if the key length is empty or greater than 2^16, or the value length is greater than 2^32.
pub fn write(&mut self, key: &[u8], value: &[u8]) -> std::io::Result<u32> {
assert!(!key.is_empty());
assert!(key.len() <= u16::MAX.into());
assert!(u32::try_from(value.len()).is_ok());

#[cfg(feature = "lz4")]
let value = lz4_flex::compress_prepend_size(value);

let mut hasher = crc32fast::Hasher::new();
hasher.update(&value);
let crc = hasher.finalize();

// NOTE: Truncation is okay and actually needed
#[allow(clippy::cast_possible_truncation)]
self.inner.write_u16::<BigEndian>(key.len() as u16)?;
self.inner.write_all(key)?;
self.inner.write_u32::<BigEndian>(crc)?;

// NOTE: Truncation is okay and actually needed
#[allow(clippy::cast_possible_truncation)]
self.inner.write_u32::<BigEndian>(value.len() as u32)?;
self.inner.write_all(&value)?;

Expand All @@ -112,6 +125,8 @@ impl Writer {

self.item_count += 1;

// NOTE: Truncation is okay
#[allow(clippy::cast_possible_truncation)]
Ok(value.len() as u32)
}

Expand Down

0 comments on commit a9c26c5

Please sign in to comment.