refactor: mask: pass mask by value

Signed-off-by: Alexey Galakhov <agalakhov@snapview.de>
pull/43/head
Alexey Galakhov 7 years ago
parent e6ebf5ac76
commit a2e122ab9e
  1. 2
      src/protocol/frame/frame.rs
  2. 14
      src/protocol/frame/mask.rs

@ -269,7 +269,7 @@ impl Frame {
#[inline] #[inline]
pub(crate) fn apply_mask(&mut self) { pub(crate) fn apply_mask(&mut self) {
if let Some(mask) = self.header.mask.take() { if let Some(mask) = self.header.mask.take() {
apply_mask(&mut self.payload, &mask) apply_mask(&mut self.payload, mask)
} }
} }

@ -11,14 +11,14 @@ pub fn generate_mask() -> [u8; 4] {
/// Mask/unmask a frame. /// Mask/unmask a frame.
#[inline] #[inline]
pub fn apply_mask(buf: &mut [u8], mask: &[u8; 4]) { pub fn apply_mask(buf: &mut [u8], mask: [u8; 4]) {
apply_mask_fast32(buf, mask) apply_mask_fast32(buf, mask)
} }
/// A safe unoptimized mask application. /// A safe unoptimized mask application.
#[inline] #[inline]
#[allow(dead_code)] #[allow(dead_code)]
fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) { fn apply_mask_fallback(buf: &mut [u8], mask: [u8; 4]) {
for (i, byte) in buf.iter_mut().enumerate() { for (i, byte) in buf.iter_mut().enumerate() {
*byte ^= mask[i & 3]; *byte ^= mask[i & 3];
} }
@ -27,7 +27,7 @@ fn apply_mask_fallback(buf: &mut [u8], mask: &[u8; 4]) {
/// Faster version of `apply_mask()` which operates on 4-byte blocks. /// Faster version of `apply_mask()` which operates on 4-byte blocks.
#[inline] #[inline]
#[allow(dead_code)] #[allow(dead_code)]
fn apply_mask_fast32(buf: &mut [u8], mask: &[u8; 4]) { fn apply_mask_fast32(buf: &mut [u8], mask: [u8; 4]) {
let mask_u32: u32 = unsafe { let mask_u32: u32 = unsafe {
read_unaligned(mask.as_ptr() as *const u32) read_unaligned(mask.as_ptr() as *const u32)
}; };
@ -101,10 +101,10 @@ mod tests {
// Check masking with proper alignment. // Check masking with proper alignment.
{ {
let mut masked = unmasked.clone(); let mut masked = unmasked.clone();
apply_mask_fallback(&mut masked, &mask); apply_mask_fallback(&mut masked, mask);
let mut masked_fast = unmasked.clone(); let mut masked_fast = unmasked.clone();
apply_mask_fast32(&mut masked_fast, &mask); apply_mask_fast32(&mut masked_fast, mask);
assert_eq!(masked, masked_fast); assert_eq!(masked, masked_fast);
} }
@ -112,10 +112,10 @@ mod tests {
// Check masking without alignment. // Check masking without alignment.
{ {
let mut masked = unmasked.clone(); let mut masked = unmasked.clone();
apply_mask_fallback(&mut masked[1..], &mask); apply_mask_fallback(&mut masked[1..], mask);
let mut masked_fast = unmasked.clone(); let mut masked_fast = unmasked.clone();
apply_mask_fast32(&mut masked_fast[1..], &mask); apply_mask_fast32(&mut masked_fast[1..], mask);
assert_eq!(masked, masked_fast); assert_eq!(masked, masked_fast);
} }

Loading…
Cancel
Save