8000 Refactor byteorder to std in rustc_middle by workingjubilee · Pull Request #75741 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Refactor byteorder to std in rustc_middle #75741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 5, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Explain contract of {read, write}_target_uint
  • Loading branch information
workingjubilee committed Sep 5, 2020
commit dc00efff9f44ddda1ce318e28f69e716f58d39dd
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/mir/interpret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
0