diff --git a/Cargo.toml b/Cargo.toml index 65b1c54..fccd134 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/message.rs b/src/message.rs index 9f669d8..26666e5 100644 --- a/src/message.rs +++ b/src/message.rs @@ -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 { - 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 { @@ -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); } } @@ -262,3 +260,23 @@ impl std::fmt::Debug for Message { .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()); +} diff --git a/src/transport/stream/transport.rs b/src/transport/stream/transport.rs index 78597e8..ce45f21 100644 --- a/src/transport/stream/transport.rs +++ b/src/transport/stream/transport.rs @@ -1,5 +1,3 @@ -use byteorder::ByteOrder; -use byteorder::LE; use std::io::IoSlice; use std::pin::Pin; use std::task::{Context, Poll}; @@ -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)?; @@ -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 }); @@ -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()); +}