Skip to content

Commit 921398a

Browse files
Remove byteorder dependency
1 parent de3edc5 commit 921398a

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

analyzeme/src/stringtable.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! See module-level documentation `measureme::stringtable`.
22
3-
use byteorder::{BigEndian, ByteOrder, LittleEndian};
43
use measureme::file_header::{
54
read_file_header, strip_file_header, CURRENT_FILE_FORMAT_VERSION, FILE_MAGIC_STRINGTABLE_DATA,
65
FILE_MAGIC_STRINGTABLE_INDEX,
@@ -10,12 +9,13 @@ use measureme::{Addr, StringId};
109
use memchr::memchr;
1110
use rustc_hash::FxHashMap;
1211
use std::borrow::Cow;
12+
use std::convert::TryInto;
1313
use std::error::Error;
1414

1515
fn deserialize_index_entry(bytes: &[u8]) -> (StringId, Addr) {
1616
(
17-
StringId::new(LittleEndian::read_u32(&bytes[0..4])),
18-
Addr(LittleEndian::read_u32(&bytes[4..8])),
17+
StringId::new(u32::from_le_bytes(bytes[0..4].try_into().unwrap())),
18+
Addr(u32::from_le_bytes(bytes[4..8].try_into().unwrap())),
1919
)
2020
}
2121

@@ -138,7 +138,7 @@ fn is_utf8_continuation_byte(byte: u8) -> bool {
138138
// String IDs in the table data are encoded in big endian format, while string
139139
// IDs in the index are encoded in little endian format. Don't mix the two up.
140140
fn decode_string_id_from_data(bytes: &[u8]) -> StringId {
141-
let id = BigEndian::read_u32(&bytes[0..4]);
141+
let id = u32::from_be_bytes(bytes[0..4].try_into().unwrap());
142142
// Mask off the `0b10` prefix
143143
StringId::new(id & STRING_ID_MASK)
144144
}

measureme/src/file_header.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! number.
44
55
use crate::serialization::SerializationSink;
6-
use byteorder::{ByteOrder, LittleEndian};
6+
use std::convert::TryInto;
77
use std::error::Error;
88

99
pub const CURRENT_FILE_FORMAT_VERSION: u32 = 5;
@@ -22,7 +22,7 @@ pub fn write_file_header<S: SerializationSink>(s: &S, file_magic: &[u8; 4]) {
2222

2323
s.write_atomic(FILE_HEADER_SIZE, |bytes| {
2424
bytes[0..4].copy_from_slice(file_magic);
25-
LittleEndian::write_u32(&mut bytes[4..8], CURRENT_FILE_FORMAT_VERSION);
25+
bytes[4..8].copy_from_slice(&CURRENT_FILE_FORMAT_VERSION.to_le_bytes());
2626
});
2727
}
2828

@@ -44,7 +44,7 @@ pub fn read_file_header(bytes: &[u8], expected_magic: &[u8; 4]) -> Result<u32, B
4444
return Err(From::from(msg));
4545
}
4646

47-
Ok(LittleEndian::read_u32(&bytes[4..8]))
47+
Ok(u32::from_le_bytes(bytes[4..8].try_into().unwrap()))
4848
}
4949

5050
pub fn strip_file_header(data: &[u8]) -> &[u8] {

measureme/src/profiler.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ pub struct Profiler<S: SerializationSink> {
3131
}
3232

3333
impl<S: SerializationSink> Profiler<S> {
34-
pub fn new<P: AsRef<Path>>(path_stem: P)
35-
-> Result<Profiler<S>, Box<dyn Error + Send + Sync>> {
34+
pub fn new<P: AsRef<Path>>(path_stem: P) -> Result<Profiler<S>, Box<dyn Error + Send + Sync>> {
3635
let paths = ProfilerFiles::new(path_stem.as_ref());
3736
let event_sink = Arc::new(S::from_path(&paths.events_file)?);
3837

measureme/src/raw_event.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::event_id::EventId;
22
use crate::stringtable::StringId;
3+
#[cfg(target_endian = "big")]
4+
use std::convert::TryInto;
35

46
/// `RawEvent` is how events are stored on-disk. If you change this struct,
57
/// make sure that you increment `file_header::CURRENT_FILE_FORMAT_VERSION`.
@@ -117,14 +119,12 @@ impl RawEvent {
117119
{
118120
// We always emit data as little endian, which we have to do
119121
// manually on big endian targets.
120-
use byteorder::{ByteOrder, LittleEndian};
121-
122-
LittleEndian::write_u32(&mut bytes[0..], self.event_kind.as_u32());
123-
LittleEndian::write_u32(&mut bytes[4..], self.event_id.as_u32());
124-
LittleEndian::write_u32(&mut bytes[8..], self.thread_id);
125-
LittleEndian::write_u32(&mut bytes[12..], self.start_time_lower);
126-
LittleEndian::write_u32(&mut bytes[16..], self.end_time_lower);
127-
LittleEndian::write_u32(&mut bytes[20..], self.start_and_end_upper);
122+
bytes[0..4].copy_from_slice(&self.event_kind.as_u32().to_le_bytes());
123+
bytes[4..8].copy_from_slice(&self.event_id.as_u32().to_le_bytes());
124+
bytes[8..12].copy_from_slice(&self.thread_id.to_le_bytes());
125+
bytes[12..16].copy_from_slice(&self.start_time_lower.to_le_bytes());
126+
bytes[16..20].copy_from_slice(&self.end_time_lower.to_le_bytes());
127+
bytes[20..24].copy_from_slice(&self.start_and_end_upper.to_le_bytes());
128128
}
129129
}
130130

@@ -147,14 +147,13 @@ impl RawEvent {
147147

148148
#[cfg(target_endian = "big")]
149149
{
150-
use byteorder::{ByteOrder, LittleEndian};
151150
RawEvent {
152-
event_kind: StringId::new(LittleEndian::read_u32(&bytes[0..])),
153-
event_id: EventId::from_u32(LittleEndian::read_u32(&bytes[4..])),
154-
thread_id: LittleEndian::read_u32(&bytes[8..]),
155-
start_time_lower: LittleEndian::read_u32(&bytes[12..]),
156-
end_time_lower: LittleEndian::read_u32(&bytes[16..]),
157-
start_and_end_upper: LittleEndian::read_u32(&bytes[20..]),
151+
event_kind: StringId::new(u32::from_le_bytes(bytes[0..4].try_into().unwrap())),
152+
event_id: EventId::from_u32(u32::from_le_bytes(bytes[4..8].try_into().unwrap())),
153+
thread_id: u32::from_le_bytes(bytes[8..12].try_into().unwrap()),
154+
start_time_lower: u32::from_le_bytes(bytes[12..16].try_into().unwrap()),
155+
end_time_lower: u32::from_le_bytes(bytes[16..20].try_into().unwrap()),
156+
start_and_end_upper: u32::from_le_bytes(bytes[20..24].try_into().unwrap()),
158157
}
159158
}
160159
}

measureme/src/stringtable.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use crate::file_header::{
6767
write_file_header, FILE_MAGIC_STRINGTABLE_DATA, FILE_MAGIC_STRINGTABLE_INDEX,
6868
};
6969
use crate::serialization::{Addr, SerializationSink};
70-
use byteorder::{BigEndian, ByteOrder, LittleEndian};
70+
// use byteorder::{BigEndian, ByteOrder};
7171
use std::sync::Arc;
7272

7373
/// A `StringId` is used to identify a string in the `StringTable`. It is
@@ -190,7 +190,7 @@ impl<'s> StringComponent<'s> {
190190
assert!(string_id.0 == string_id.0 & STRING_ID_MASK);
191191
let tagged = string_id.0 | (1u32 << 31);
192192

193-
BigEndian::write_u32(&mut bytes[0..4], tagged);
193+
&mut bytes[0..4].copy_from_slice(&tagged.to_be_bytes());
194194
&mut bytes[4..]
195195
}
196196
}
@@ -253,8 +253,8 @@ impl_serializable_string_for_fixed_size!(16);
253253

254254
fn serialize_index_entry<S: SerializationSink>(sink: &S, id: StringId, addr: Addr) {
255255
sink.write_atomic(8, |bytes| {
256-
LittleEndian::write_u32(&mut bytes[0..4], id.0);
257-
LittleEndian::write_u32(&mut bytes[4..8], addr.0);
256+
bytes[0..4].copy_from_slice(&id.0.to_le_bytes());
257+
bytes[4..8].copy_from_slice(&addr.0.to_le_bytes());
258258
});
259259
}
260260

0 commit comments

Comments
 (0)