Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding impl for core::ops::BitXor #297

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::buf::IntoIter;
use crate::debug;

use core::{cmp, fmt, mem, hash, slice, ptr, usize};
use core::ops::{Deref, DerefMut, RangeBounds};
use core::ops::{BitXor, Deref, DerefMut, RangeBounds};
use core::sync::atomic::{self, AtomicUsize, AtomicPtr};
use core::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel};
use core::iter::{FromIterator, Iterator};
Expand Down Expand Up @@ -2992,6 +2992,24 @@ impl PartialEq<Bytes> for BytesMut
}
}

impl BitXor for Bytes {
type Output = Option<Self>; //none when rhs length not the same as self length

fn bitxor(self, rhs: Self) -> Self::Output {
if self.len() != rhs.len() { return None; }
Some(self.iter().zip(rhs.iter()).map( |(l,r)| *l ^ *r ).collect())
}
}

impl BitXor for BytesMut {
type Output = Option<Self>; //none when rhs length not the same as self length

fn bitxor(self, rhs: Self) -> Self::Output {
if self.len() != rhs.len() { return None; }
Some(self.iter().zip(rhs.iter()).map( |(l,r)| *l ^ *r ).collect())
}
}

// While there is `std::process:abort`, it's only available in Rust 1.17, and
// our minimum supported version is currently 1.15. So, this acts as an abort
// by triggering a double panic, which always aborts in Rust.
Expand Down
42 changes: 42 additions & 0 deletions tests/test_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,48 @@ fn from_iter_no_size_hint() {
assert_eq!(&actual[..], &expect[..]);
}

#[test]
fn bytes_mut_xor_success() {
let left:&[u8] = &[0x01,0x01,0x00,0x00];
let right:&[u8] = &[0x00,0x01,0x00,0x01];
let expect:&[u8] = &[0x01,0x00,0x00,0x01];
assert_eq!(BytesMut::from(left) ^ BytesMut::from(right), Some(BytesMut::from(expect)));
// xor is commutative
assert_eq!(BytesMut::from(right) ^ BytesMut::from(left), Some(BytesMut::from(expect)));
}

#[test]
fn bytes_mut_xor_zero_lengths_return_zero() {
assert_eq!(BytesMut::new() ^ BytesMut::new(), Some(BytesMut::new()));
}

#[test]
fn bytes_mut_xor_different_lengths_returns_none() {
let left:&[u8] = &[0x01,0x01,0x00,0x00,0x00];
let right:&[u8] = &[0x00,0x01,0x00,0x01];
assert_eq!(BytesMut::from(left) ^ BytesMut::from(right), None);
}

#[test]
fn bytes_xor_success() {
let left = &[0x01,0x01,0x00,0x00];
let right = &[0x00,0x01,0x00,0x01];
let expect = &[0x01,0x00,0x00,0x01];
assert_eq!(Bytes::from_static(left) ^ Bytes::from_static(right), Some(Bytes::from_static(expect)));
// xor is commutative
assert_eq!(Bytes::from_static(right) ^ Bytes::from_static(left), Some(Bytes::from_static(expect)));
}

#[test]
fn bytes_xor_zero_lengths_return_zero() {
assert_eq!(Bytes::new() ^ Bytes::new(), Some(Bytes::new()));
}

#[test]
fn bytes_xor_different_lengths_returns_none() {
assert_eq!(Bytes::from_static(&[0x01,0x01,0x00,0x00,0x00]) ^ Bytes::from_static(&[0x00,0x01,0x00,0x01]), None);
}

fn test_slice_ref(bytes: &Bytes, start: usize, end: usize, expected: &[u8]) {
let slice = &(bytes.as_ref()[start..end]);
let sub = bytes.slice_ref(&slice);
Expand Down