1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 01:32:57 +01:00

remove unsafe from mask

This commit is contained in:
Nikolay Kim 2018-06-23 11:51:02 +06:00
parent d1318a35a0
commit ff0ab733e4
3 changed files with 23 additions and 23 deletions

View File

@ -219,10 +219,10 @@ pub trait IoStream: AsyncRead + AsyncWrite + 'static {
fn read_available(&mut self, buf: &mut BytesMut) -> Poll<bool, io::Error> { fn read_available(&mut self, buf: &mut BytesMut) -> Poll<bool, io::Error> {
let mut read_some = false; let mut read_some = false;
loop { loop {
if buf.remaining_mut() < LW_BUFFER_SIZE {
buf.reserve(HW_BUFFER_SIZE);
}
unsafe { unsafe {
if buf.remaining_mut() < LW_BUFFER_SIZE {
buf.reserve(HW_BUFFER_SIZE);
}
match self.read(buf.bytes_mut()) { match self.read(buf.bytes_mut()) {
Ok(n) => { Ok(n) => {
if n == 0 { if n == 0 {

View File

@ -230,14 +230,16 @@ impl<H> WorkerSettings<H> {
} }
pub fn set_date(&self, dst: &mut BytesMut, full: bool) { pub fn set_date(&self, dst: &mut BytesMut, full: bool) {
if full { unsafe {
let mut buf: [u8; 39] = unsafe { mem::uninitialized() }; if full {
buf[..6].copy_from_slice(b"date: "); let mut buf: [u8; 39] = mem::uninitialized();
buf[6..35].copy_from_slice(&(unsafe { &*self.date.get() }.bytes)); buf[..6].copy_from_slice(b"date: ");
buf[35..].copy_from_slice(b"\r\n\r\n"); buf[6..35].copy_from_slice(&(*self.date.get()).bytes);
dst.extend_from_slice(&buf); buf[35..].copy_from_slice(b"\r\n\r\n");
} else { dst.extend_from_slice(&buf);
dst.extend_from_slice(&(unsafe { &*self.date.get() }.bytes)); } else {
dst.extend_from_slice(&(*self.date.get()).bytes);
}
} }
} }
} }

View File

@ -10,15 +10,6 @@ pub fn apply_mask(buf: &mut [u8], mask: u32) {
unsafe { apply_mask_fast32(buf, mask) } unsafe { apply_mask_fast32(buf, mask) }
} }
/// A safe unoptimized mask application.
#[inline]
#[allow(dead_code)]
fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) {
for (i, byte) in buf.iter_mut().enumerate() {
*byte ^= mask[i & 3];
}
}
/// Faster version of `apply_mask()` which operates on 8-byte blocks. /// Faster version of `apply_mask()` which operates on 8-byte blocks.
/// ///
/// unsafe because uses pointer math and bit operations for performance /// unsafe because uses pointer math and bit operations for performance
@ -99,13 +90,20 @@ unsafe fn xor_mem(ptr: *mut u8, mask: u32, len: usize) {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::{apply_mask, apply_mask_fallback}; use super::apply_mask;
use std::ptr; use byteorder::{ByteOrder, LittleEndian};
/// A safe unoptimized mask application.
fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) {
for (i, byte) in buf.iter_mut().enumerate() {
*byte ^= mask[i & 3];
}
}
#[test] #[test]
fn test_apply_mask() { fn test_apply_mask() {
let mask = [0x6d, 0xb6, 0xb2, 0x80]; let mask = [0x6d, 0xb6, 0xb2, 0x80];
let mask_u32: u32 = unsafe { ptr::read_unaligned(mask.as_ptr() as *const u32) }; let mask_u32: u32 = LittleEndian::read_u32(&mask);
let unmasked = vec![ let unmasked = vec![
0xf3, 0x00, 0x01, 0x02, 0x03, 0x80, 0x81, 0x82, 0xff, 0xfe, 0x00, 0x17, 0xf3, 0x00, 0x01, 0x02, 0x03, 0x80, 0x81, 0x82, 0xff, 0xfe, 0x00, 0x17,