1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-23 23:51:06 +01:00

Merge pull request #1023 from lukaslueg/byteorder_removed

Remove byteorder-dependency
This commit is contained in:
leizzer 2019-08-07 12:28:23 -03:00 committed by GitHub
commit 80e1d16ab8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 14 deletions

View File

@ -1,5 +1,12 @@
# Changes
## [0.2.9] - 2019-08-xx
### Changed
* Dropped the `byteorder`-dependency in favor of `stdlib`-implementation
## [0.2.8] - 2019-08-01
### Added

View File

@ -57,7 +57,6 @@ actix-threadpool = "0.1.1"
base64 = "0.10"
bitflags = "1.0"
bytes = "0.4"
byteorder = "1.2"
copyless = "0.1.4"
derive_more = "0.15.0"
either = "1.5.2"

View File

@ -1,4 +1,5 @@
use byteorder::{ByteOrder, LittleEndian, NetworkEndian};
use std::convert::TryFrom;
use bytes::{BufMut, Bytes, BytesMut};
use log::debug;
use rand;
@ -48,14 +49,16 @@ impl Parser {
if chunk_len < 4 {
return Ok(None);
}
let len = NetworkEndian::read_uint(&src[idx..], 2) as usize;
let len = usize::from(u16::from_be_bytes(
TryFrom::try_from(&src[idx..idx + 2]).unwrap(),
));
idx += 2;
len
} else if len == 127 {
if chunk_len < 10 {
return Ok(None);
}
let len = NetworkEndian::read_uint(&src[idx..], 8);
let len = u64::from_be_bytes(TryFrom::try_from(&src[idx..idx + 8]).unwrap());
if len > max_size as u64 {
return Err(ProtocolError::Overflow);
}
@ -75,10 +78,10 @@ impl Parser {
return Ok(None);
}
let mask: &[u8] = &src[idx..idx + 4];
let mask_u32 = LittleEndian::read_u32(mask);
let mask =
u32::from_le_bytes(TryFrom::try_from(&src[idx..idx + 4]).unwrap());
idx += 4;
Some(mask_u32)
Some(mask)
} else {
None
};
@ -137,7 +140,7 @@ impl Parser {
/// Parse the payload of a close frame.
pub fn parse_close_payload(payload: &[u8]) -> Option<CloseReason> {
if payload.len() >= 2 {
let raw_code = NetworkEndian::read_u16(payload);
let raw_code = u16::from_be_bytes(TryFrom::try_from(&payload[..2]).unwrap());
let code = CloseCode::from(raw_code);
let description = if payload.len() > 2 {
Some(String::from_utf8_lossy(&payload[2..]).into())
@ -201,10 +204,7 @@ impl Parser {
let payload = match reason {
None => Vec::new(),
Some(reason) => {
let mut code_bytes = [0; 2];
NetworkEndian::write_u16(&mut code_bytes, reason.code.into());
let mut payload = Vec::from(&code_bytes[..]);
let mut payload = Into::<u16>::into(reason.code).to_be_bytes().to_vec();
if let Some(description) = reason.description {
payload.extend(description.as_bytes());
}

View File

@ -105,7 +105,6 @@ fn align_buf(buf: &mut [u8]) -> (ShortSlice, &mut [u64], ShortSlice) {
#[cfg(test)]
mod tests {
use super::apply_mask;
use byteorder::{ByteOrder, LittleEndian};
/// A safe unoptimized mask application.
fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) {
@ -117,7 +116,7 @@ mod tests {
#[test]
fn test_apply_mask() {
let mask = [0x6d, 0xb6, 0xb2, 0x80];
let mask_u32: u32 = LittleEndian::read_u32(&mask);
let mask_u32 = u32::from_le_bytes(mask);
let unmasked = vec![
0xf3, 0x00, 0x01, 0x02, 0x03, 0x80, 0x81, 0x82, 0xff, 0xfe, 0x00, 0x17,