Skip to content

Commit 91c9351

Browse files
Explain contract of {read, write}_target_uint
1 parent 28d3100 commit 91c9351

File tree

1 file changed

+6
-0
lines changed
  • compiler/rustc_middle/src/mir/interpret

1 file changed

+6
-0
lines changed

compiler/rustc_middle/src/mir/interpret/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,17 +560,23 @@ pub fn write_target_uint(
560560
mut target: &mut [u8],
561561
data: u128,
562562
) -> Result<(), io::Error> {
563+
// This u128 holds an "any-size uint" (since smaller uints can fits in it)
564+
// So we do not write all bytes of the u128, just the "payload".
563565
match endianness {
564566
Endian::Little => target.write(&data.to_le_bytes())?,
565567
Endian::Big => target.write(&data.to_be_bytes())?,
566568
};
569+
debug_assert!(target.len() == 0); // We should have filled the target buffer.
567570
Ok(())
568571
}
569572

570573
#[inline]
571574
pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, io::Error> {
575+
// This u128 holds an "any-size uint" (since smaller uints can fits in it)
572576
let mut buf = [0u8; std::mem::size_of::<u128>()];
577+
// So we do not read exactly 16 bytes into the u128, just the "payload".
573578
source.read(&mut buf)?;
579+
debug_assert!(source.len() == 0); // We should have consumed the source buffer.
574580
match endianness {
575581
Endian::Little => Ok(u128::from_le_bytes(buf)),
576582
Endian::Big => Ok(u128::from_be_bytes(buf)),

0 commit comments

Comments
 (0)