Skip to content

Commit

Permalink
Remove byteorder dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
de-vri-es committed Nov 26, 2023
1 parent 94b2a10 commit 10b421e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ unix-seqpacket = ["tokio-seqpacket"]
unix-stream = ["tokio/net"]

[dependencies]
byteorder = "1.4.3"
filedesc = { version = "0.6.1" }
tokio = { version = "1.32.0", features = ["rt", "sync"] }
tokio-seqpacket = { version = "0.7.0", optional = true }
Expand Down
34 changes: 26 additions & 8 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,9 @@ impl MessageHeader {
/// # Panic
/// This function panics if the buffer does not contain a full header.
pub fn decode(buffer: &[u8]) -> Result<Self, Error> {
use byteorder::{ByteOrder, LE};
let message_type = LE::read_u32(&buffer[0..]);
let request_id = LE::read_u32(&buffer[4..]);
let service_id = LE::read_i32(&buffer[8..]);
let message_type = read_u32_le(&buffer[0..]);
let request_id = read_u32_le(&buffer[4..]);
let service_id = read_i32_le(&buffer[8..]);

let message_type = MessageType::from_u32(message_type)?;
Ok(Self {
Expand All @@ -247,11 +246,10 @@ impl MessageHeader {
/// # Panic
/// This function panics if the buffer is not large enough to hold a full header.
pub fn encode(&self, buffer: &mut [u8]) {
use byteorder::{ByteOrder, LE};
assert!(buffer.len() >= 12);
LE::write_u32(&mut buffer[0..], self.message_type as u32);
LE::write_u32(&mut buffer[4..], self.request_id);
LE::write_i32(&mut buffer[8..], self.service_id);
write_u32_le(&mut buffer[0..], self.message_type as u32);
write_u32_le(&mut buffer[4..], self.request_id);
write_i32_le(&mut buffer[8..], self.service_id);
}
}

Expand All @@ -262,3 +260,23 @@ impl<Body> std::fmt::Debug for Message<Body> {
.finish_non_exhaustive()
}
}

/// Read a [`u32`] from a buffer in little endian format.
fn read_u32_le(buffer: &[u8]) -> u32 {
u32::from_le_bytes(buffer[0..4].try_into().unwrap())
}

/// Read a [`i32`] from a buffer in little endian format.
fn read_i32_le(buffer: &[u8]) -> i32 {
i32::from_le_bytes(buffer[0..4].try_into().unwrap())
}

/// Write a [`i32`] to a buffer in little endian format.
fn write_i32_le(buffer: &mut [u8], value: i32) {
buffer[0..4].copy_from_slice(&value.to_le_bytes());
}

/// Write a [`u32`] to a buffer in little endian format.
fn write_u32_le(buffer: &mut [u8], value: u32) {
buffer[0..4].copy_from_slice(&value.to_le_bytes());
}
16 changes: 12 additions & 4 deletions src/transport/stream/transport.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use byteorder::ByteOrder;
use byteorder::LE;
use std::io::IoSlice;
use std::pin::Pin;
use std::task::{Context, Poll};
Expand Down Expand Up @@ -173,7 +171,7 @@ where
// Check if we have the whole frame + header.
if this.bytes_read == FRAMED_HEADER_LEN {
// Parse frame and header.
let length = LE::read_u32(&this.header_buffer[0..]);
let length = read_u32_le(&this.header_buffer[0..]);
this.parsed_header = MessageHeader::decode(&this.header_buffer[4..])
.map_err(TransportError::new_fatal)?;

Expand Down Expand Up @@ -220,7 +218,7 @@ where
// Encode the header if we haven't done that yet.
let header_buffer = this.header_buffer.get_or_insert_with(|| {
let mut buffer = [0u8; FRAMED_HEADER_LEN];
LE::write_u32(&mut buffer[0..], body.len() as u32 + crate::HEADER_LEN);
write_u32_le(&mut buffer[0..], body.len() as u32 + crate::HEADER_LEN);
header.encode(&mut buffer[4..]);
buffer
});
Expand All @@ -243,3 +241,13 @@ where
Poll::Ready(Ok(()))
}
}

/// Read a [`u32`] from a buffer in little endian format.
fn read_u32_le(buffer: &[u8]) -> u32 {
u32::from_le_bytes(buffer[0..4].try_into().unwrap())
}

/// Write a [`u32`] to a buffer in little endian format.
fn write_u32_le(buffer: &mut [u8], value: u32) {
buffer[0..4].copy_from_slice(&value.to_le_bytes());
}

0 comments on commit 10b421e

Please sign in to comment.