Skip to content

Commit

Permalink
Merge pull request #373 from psychon/reduce-byteorder
Browse files Browse the repository at this point in the history
Reduce use of byteorder crate
  • Loading branch information
agalakhov authored Nov 17, 2023
2 parents a43bb49 + e4224ed commit fc17f73
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/protocol/frame/frame.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use byteorder::{ByteOrder, NetworkEndian, ReadBytesExt, WriteBytesExt};
use byteorder::{NetworkEndian, ReadBytesExt};
use log::*;
use std::{
borrow::Cow,
Expand Down Expand Up @@ -108,8 +108,12 @@ impl FrameHeader {
output.write_all(&[one, two])?;
match lenfmt {
LengthFormat::U8(_) => (),
LengthFormat::U16 => output.write_u16::<NetworkEndian>(length as u16)?,
LengthFormat::U64 => output.write_u64::<NetworkEndian>(length)?,
LengthFormat::U16 => {
output.write(&(length as u16).to_be_bytes())?;
}
LengthFormat::U64 => {
output.write(&length.to_be_bytes())?;
}
}

if let Some(ref mask) = self.mask {
Expand Down Expand Up @@ -295,7 +299,7 @@ impl Frame {
1 => Err(Error::Protocol(ProtocolError::InvalidCloseSequence)),
_ => {
let mut data = self.payload;
let code = NetworkEndian::read_u16(&data[0..2]).into();
let code = u16::from_be_bytes([data[0], data[1]]).into();
data.drain(0..2);
let text = String::from_utf8(data)?;
Ok(Some(CloseFrame { code, reason: text.into() }))
Expand Down Expand Up @@ -340,7 +344,7 @@ impl Frame {
pub fn close(msg: Option<CloseFrame>) -> Frame {
let payload = if let Some(CloseFrame { code, reason }) = msg {
let mut p = Vec::with_capacity(reason.as_bytes().len() + 2);
p.write_u16::<NetworkEndian>(code.into()).unwrap(); // can't fail
p.extend(u16::from(code).to_be_bytes());
p.extend_from_slice(reason.as_bytes());
p
} else {
Expand Down

0 comments on commit fc17f73

Please sign in to comment.