Skip to content

Commit dc00eff

Browse files
Explain contract of {read, write}_target_uint
1 parent fe2a867 commit dc00eff

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

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

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

0 commit comments

Comments
 (0)