diff options
| author | Jubilee Young <workingjubilee@gmail.com> | 2020-08-21 00:55:33 -0700 |
|---|---|---|
| committer | Jubilee Young <workingjubilee@gmail.com> | 2020-09-04 21:51:28 -0700 |
| commit | dc00efff9f44ddda1ce318e28f69e716f58d39dd (patch) | |
| tree | c4905ab0f98e2f1d4fe865518415e3a805be04d1 | |
| parent | fe2a867125ff80dcb12df19581bd28b342b15c28 (diff) | |
| download | rust-dc00efff9f44ddda1ce318e28f69e716f58d39dd.tar.gz rust-dc00efff9f44ddda1ce318e28f69e716f58d39dd.zip | |
Explain contract of {read, write}_target_uint
| -rw-r--r-- | compiler/rustc_middle/src/mir/interpret/mod.rs | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index 90f8195a50c..99966121e4d 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -561,17 +561,23 @@ pub fn write_target_uint( mut target: &mut [u8], data: u128, ) -> Result<(), io::Error> { + // This u128 holds an "any-size uint" (since smaller uints can fits in it) + // So we do not write all bytes of the u128, just the "payload". match endianness { Endian::Little => target.write(&data.to_le_bytes())?, Endian::Big => target.write(&data.to_be_bytes())?, }; + debug_assert!(target.len() == 0); // We should have filled the target buffer. Ok(()) } #[inline] pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, io::Error> { + // This u128 holds an "any-size uint" (since smaller uints can fits in it) let mut buf = [0u8; std::mem::size_of::<u128>()]; + // So we do not read exactly 16 bytes into the u128, just the "payload". source.read(&mut buf)?; + debug_assert!(source.len() == 0); // We should have consumed the source buffer. match endianness { Endian::Little => Ok(u128::from_le_bytes(buf)), Endian::Big => Ok(u128::from_be_bytes(buf)), |
