1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +01:00

remove unnecessary unsafes

This commit is contained in:
Nikolay Kim 2018-06-23 10:29:23 +06:00
parent 756227896b
commit d1318a35a0
3 changed files with 11 additions and 19 deletions

View File

@ -85,7 +85,7 @@ flate2 = { version="1.0", optional = true, default-features = false }
mio = "^0.6.13"
net2 = "0.2"
bytes = "0.4"
byteorder = "1"
byteorder = "1.2"
futures = "0.1"
futures-cpupool = "0.1"
slab = "0.4"

View File

@ -1,7 +1,7 @@
use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::hash::{BuildHasherDefault, Hasher};
use std::fmt;
use std::hash::{BuildHasherDefault, Hasher};
struct IdHasher {
id: u64,
@ -16,7 +16,7 @@ impl Default for IdHasher {
impl Hasher for IdHasher {
fn write(&mut self, bytes: &[u8]) {
for &x in bytes {
self.id.wrapping_add(x as u64);
self.id.wrapping_add(u64::from(x));
}
}

View File

@ -1,9 +1,8 @@
#![cfg_attr(feature = "cargo-clippy", allow(cast_ptr_alignment))]
use byteorder::{ByteOrder, NetworkEndian};
use byteorder::{ByteOrder, LittleEndian, NetworkEndian};
use bytes::{BufMut, Bytes, BytesMut};
use futures::{Async, Poll, Stream};
use rand;
use std::{fmt, ptr};
use std::fmt;
use body::Binary;
use error::PayloadError;
@ -115,8 +114,7 @@ impl Frame {
};
let mask: &[u8] = &buf[idx..idx + 4];
let mask_u32: u32 =
unsafe { ptr::read_unaligned(mask.as_ptr() as *const u32) };
let mask_u32 = LittleEndian::read_u32(mask);
idx += 4;
Some(mask_u32)
} else {
@ -185,8 +183,7 @@ impl Frame {
}
let mask: &[u8] = &chunk[idx..idx + 4];
let mask_u32: u32 =
unsafe { ptr::read_unaligned(mask.as_ptr() as *const u32) };
let mask_u32 = LittleEndian::read_u32(mask);
idx += 4;
Some(mask_u32)
} else {
@ -323,15 +320,10 @@ impl Frame {
if genmask {
let mask = rand::random::<u32>();
unsafe {
{
let buf_mut = buf.bytes_mut();
*(buf_mut as *mut _ as *mut u32) = mask;
buf_mut[4..payload_len + 4].copy_from_slice(payload.as_ref());
apply_mask(&mut buf_mut[4..], mask);
}
buf.advance_mut(payload_len + 4);
}
buf.put_u32_le(mask);
buf.extend_from_slice(payload.as_ref());
let pos = buf.len() - payload_len;
apply_mask(&mut buf[pos..], mask);
buf.into()
} else {
buf.put_slice(payload.as_ref());