Skip to content

Commit e160d2b

Browse files
authored
Rollup merge of #75741 - workingjubilee:refactor-byteorder, r=matthewjasper
Refactor byteorder to std in rustc_middle Use std::io::{Read, Write} and {to, from}_{le, be}_bytes methods in order to remove byteorder from librustc_middle's dependency graph.
2 parents cb33a15 + 2df552b commit e160d2b

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -3722,7 +3722,6 @@ name = "rustc_middle"
37223722
version = "0.0.0"
37233723
dependencies = [
37243724
"bitflags",
3725-
"byteorder",
37263725
"chalk-ir",
37273726
"measureme",
37283727
"polonius-engine",

compiler/rustc_middle/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ rustc_index = { path = "../rustc_index" }
2626
rustc_serialize = { path = "../rustc_serialize" }
2727
rustc_ast = { path = "../rustc_ast" }
2828
rustc_span = { path = "../rustc_span" }
29-
byteorder = { version = "1.3" }
3029
chalk-ir = "0.21.0"
3130
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
3231
measureme = "0.7.1"

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,8 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
345345

346346
/// Reads a *non-ZST* scalar.
347347
///
348-
/// ZSTs can't be read for two reasons:
349-
/// * byte-order cannot work with zero-element buffers;
350-
/// * in order to obtain a `Pointer`, we need to check for ZSTness anyway due to integer
351-
/// pointers being valid for ZSTs.
348+
/// ZSTs can't be read because in order to obtain a `Pointer`, we need to check
349+
/// for ZSTness anyway due to integer pointers being valid for ZSTs.
352350
///
353351
/// It is the caller's responsibility to check bounds and alignment beforehand.
354352
/// Most likely, you want to call `InterpCx::read_scalar` instead of this method.
@@ -397,10 +395,8 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
397395

398396
/// Writes a *non-ZST* scalar.
399397
///
400-
/// ZSTs can't be read for two reasons:
401-
/// * byte-order cannot work with zero-element buffers;
402-
/// * in order to obtain a `Pointer`, we need to check for ZSTness anyway due to integer
403-
/// pointers being valid for ZSTs.
398+
/// ZSTs can't be read because in order to obtain a `Pointer`, we need to check
399+
/// for ZSTness anyway due to integer pointers being valid for ZSTs.
404400
///
405401
/// It is the caller's responsibility to check bounds and alignment beforehand.
406402
/// Most likely, you want to call `InterpCx::write_scalar` instead of this method.

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

+23-9
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ mod value;
9898
use std::convert::TryFrom;
9999
use std::fmt;
100100
use std::io;
101+
use std::io::{Read, Write};
101102
use std::num::NonZeroU32;
102103
use std::sync::atomic::{AtomicU32, Ordering};
103104

104-
use byteorder::{BigEndian, LittleEndian, ReadBytesExt, WriteBytesExt};
105105
use rustc_ast::LitKind;
106106
use rustc_data_structures::fx::FxHashMap;
107107
use rustc_data_structures::sync::{HashMapExt, Lock};
@@ -561,19 +561,33 @@ pub fn write_target_uint(
561561
mut target: &mut [u8],
562562
data: u128,
563563
) -> Result<(), io::Error> {
564-
let len = target.len();
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".
565566
match endianness {
566-
Endian::Little => target.write_uint128::<LittleEndian>(data, len),
567-
Endian::Big => target.write_uint128::<BigEndian>(data, len),
568-
}
567+
Endian::Little => target.write(&data.to_le_bytes())?,
568+
Endian::Big => target.write(&data.to_be_bytes()[16 - target.len()..])?,
569+
};
570+
debug_assert!(target.len() == 0); // We should have filled the target buffer.
571+
Ok(())
569572
}
570573

571574
#[inline]
572575
pub fn read_target_uint(endianness: Endian, mut source: &[u8]) -> Result<u128, io::Error> {
573-
match endianness {
574-
Endian::Little => source.read_uint128::<LittleEndian>(source.len()),
575-
Endian::Big => source.read_uint128::<BigEndian>(source.len()),
576-
}
576+
// This u128 holds an "any-size uint" (since smaller uints can fits in it)
577+
let mut buf = [0u8; std::mem::size_of::<u128>()];
578+
// So we do not read exactly 16 bytes into the u128, just the "payload".
579+
let uint = match endianness {
580+
Endian::Little => {
581+
source.read(&mut buf)?;
582+
Ok(u128::from_le_bytes(buf))
583+
}
584+
Endian::Big => {
585+
source.read(&mut buf[16 - source.len()..])?;
586+
Ok(u128::from_be_bytes(buf))
587+
}
588+
};
589+
debug_assert!(source.len() == 0); // We should have consumed the source buffer.
590+
uint
577591
}
578592

579593
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)