From: Chris Mikkelson Date: Thu, 8 Aug 2024 12:56:25 +0000 (-0600) Subject: block: use little-endian fixed representations X-Git-Url: https://git.mikk.net/?a=commitdiff_plain;h=09aee34262fee98beeff684547e70fc2c989cf8f;p=mtbl-rs block: use little-endian fixed representations Consistent with C mtbl --- diff --git a/src/reader/block.rs b/src/reader/block.rs index 73e2e56..bf130fb 100644 --- a/src/reader/block.rs +++ b/src/reader/block.rs @@ -19,7 +19,7 @@ impl RestartType { } else { let start = idx * size_of::(); let end = start + size_of::(); - Ok(u32::from_be_bytes(restarts[start..end].try_into()?) as usize) + Ok(u32::from_le_bytes(restarts[start..end].try_into()?) as usize) } } RestartType::U64 => { @@ -28,7 +28,7 @@ impl RestartType { } else { let start = idx * size_of::(); let end = start + size_of::(); - Ok(u64::from_be_bytes(restarts[start..end].try_into()?) as usize) + Ok(u64::from_le_bytes(restarts[start..end].try_into()?) as usize) } } } @@ -50,7 +50,7 @@ impl> Block { } let rc_off = data.as_ref().len() - size_of::(); - let restart_count = u32::from_be_bytes(data.as_ref()[rc_off..].try_into()?) as usize; + let restart_count = u32::from_le_bytes(data.as_ref()[rc_off..].try_into()?) as usize; // try 32-bit restarts if (restart_count * size_of::()) > rc_off { @@ -285,5 +285,7 @@ mod test { let mut bi = b.into_iter(); bi.seek(&u32::to_be_bytes(40)); assert_eq!(bi.next().unwrap().key.as_ref(), &u32::to_be_bytes(40)); + bi.seek(&u32::to_be_bytes(32)); + assert_eq!(bi.next().unwrap().key.as_ref(), &u32::to_be_bytes(40)); } } diff --git a/src/writer/block_builder.rs b/src/writer/block_builder.rs index 5652cf0..70b9761 100644 --- a/src/writer/block_builder.rs +++ b/src/writer/block_builder.rs @@ -88,19 +88,19 @@ impl BlockBuilder { .reserve(num_restarts * self.restart_size() + size_of::()); match self.restart_size() { 4 => { - for b in self.restarts.iter().map(|r| u32::to_be_bytes(*r as u32)) { + for b in self.restarts.iter().map(|r| u32::to_le_bytes(*r as u32)) { self.data.extend_from_slice(&b); } } 8 => { - for b in self.restarts.iter().map(|r| u64::to_be_bytes(*r as u64)) { + for b in self.restarts.iter().map(|r| u64::to_le_bytes(*r as u64)) { self.data.extend_from_slice(&b); } } _ => unreachable!(), }; self.data - .extend_from_slice(&u32::to_be_bytes(num_restarts as u32)); + .extend_from_slice(&u32::to_le_bytes(num_restarts as u32)); self.data.as_slice() }