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

Mask values on write instead of read #65

Merged
merged 1 commit into from
Feb 23, 2024
Merged
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
90 changes: 47 additions & 43 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,58 +159,58 @@ macro_rules! implement_common {

impl PartialEq for $name {
fn eq(&self, other: &Self) -> bool {
self.mask().0 == other.mask().0
self.0 == other.0
}
}

impl Eq for $name {}

impl PartialOrd for $name {
fn partial_cmp(&self, other: &$name) -> Option<Ordering> {
self.mask().0.partial_cmp(&other.mask().0)
Some(self.cmp(other))
}
}

impl Ord for $name {
fn cmp(&self, other: &$name) -> Ordering {
self.mask().0.cmp(&other.mask().0)
self.0.cmp(&other.0)
}
}

impl Hash for $name {
fn hash<H: Hasher>(&self, h: &mut H) {
self.mask().0.hash(h)
self.0.hash(h)
}
}

// Implement formating functions
impl Display for $name {
fn fmt(&self, f: &mut Formatter) -> Result<(), lib::core::fmt::Error> {
let $name(ref value) = self.mask();
let $name(ref value) = self;
<$type as Display>::fmt(value, f)
}
}
impl UpperHex for $name {
fn fmt(&self, f: &mut Formatter) -> Result<(), lib::core::fmt::Error> {
let $name(ref value) = self.mask();
let $name(ref value) = self;
<$type as UpperHex>::fmt(value, f)
}
}
impl LowerHex for $name {
fn fmt(&self, f: &mut Formatter) -> Result<(), lib::core::fmt::Error> {
let $name(ref value) = self.mask();
let $name(ref value) = self;
<$type as LowerHex>::fmt(value, f)
}
}
impl Octal for $name {
fn fmt(&self, f: &mut Formatter) -> Result<(), lib::core::fmt::Error> {
let $name(ref value) = self.mask();
let $name(ref value) = self;
<$type as Octal>::fmt(value, f)
}
}
impl Binary for $name {
fn fmt(&self, f: &mut Formatter) -> Result<(), lib::core::fmt::Error> {
let $name(ref value) = self.mask();
let $name(ref value) = self;
<$type as Binary>::fmt(value, f)
}
}
Expand All @@ -222,7 +222,7 @@ macro_rules! implement_common {
type Output = $name;

fn shr(self, rhs: T) -> $name {
$name(self.mask().0.shr(rhs))
$name(self.0.shr(rhs))
}
}

Expand All @@ -233,7 +233,7 @@ macro_rules! implement_common {
type Output = $name;

fn shl(self, rhs: T) -> $name {
$name(self.mask().0.shl(rhs))
$name(self.0.shl(rhs)).mask()
}
}

Expand All @@ -242,7 +242,6 @@ macro_rules! implement_common {
$type: ShrAssign<T>,
{
fn shr_assign(&mut self, rhs: T) {
*self = self.mask();
self.0.shr_assign(rhs);
}
}
Expand All @@ -252,141 +251,138 @@ macro_rules! implement_common {
$type: ShlAssign<T>,
{
fn shl_assign(&mut self, rhs: T) {
*self = self.mask();
self.0.shl_assign(rhs);
*self = self.mask();
}
}

impl BitOr<$name> for $name {
type Output = $name;

fn bitor(self, rhs: $name) -> Self::Output {
$name(self.mask().0.bitor(rhs.mask().0))
$name(self.0.bitor(rhs.0))
}
}

impl<'a> BitOr<&'a $name> for $name {
type Output = <$name as BitOr<$name>>::Output;

fn bitor(self, rhs: &'a $name) -> Self::Output {
$name(self.mask().0.bitor(rhs.mask().0))
$name(self.0.bitor(rhs.0))
}
}

impl<'a> BitOr<$name> for &'a $name {
type Output = <$name as BitOr<$name>>::Output;

fn bitor(self, rhs: $name) -> Self::Output {
$name(self.mask().0.bitor(rhs.mask().0))
$name(self.0.bitor(rhs.0))
}
}

impl<'a> BitOr<&'a $name> for &'a $name {
type Output = <$name as BitOr<$name>>::Output;

fn bitor(self, rhs: &'a $name) -> Self::Output {
$name(self.mask().0.bitor(rhs.mask().0))
$name(self.0.bitor(rhs.0))
}
}

impl BitOrAssign<$name> for $name {
fn bitor_assign(&mut self, other: $name) {
*self = self.mask();
self.0.bitor_assign(other.mask().0)
self.0.bitor_assign(other.0)
}
}

impl BitXor<$name> for $name {
type Output = $name;

fn bitxor(self, rhs: $name) -> Self::Output {
$name(self.mask().0.bitxor(rhs.mask().0))
$name(self.0.bitxor(rhs.0))
}
}

impl<'a> BitXor<&'a $name> for $name {
type Output = <$name as BitOr<$name>>::Output;

fn bitxor(self, rhs: &'a $name) -> Self::Output {
$name(self.mask().0.bitxor(rhs.mask().0))
$name(self.0.bitxor(rhs.0))
}
}

impl<'a> BitXor<$name> for &'a $name {
type Output = <$name as BitOr<$name>>::Output;

fn bitxor(self, rhs: $name) -> Self::Output {
$name(self.mask().0.bitxor(rhs.mask().0))
$name(self.0.bitxor(rhs.0))
}
}

impl<'a> BitXor<&'a $name> for &'a $name {
type Output = <$name as BitOr<$name>>::Output;

fn bitxor(self, rhs: &'a $name) -> Self::Output {
$name(self.mask().0.bitxor(rhs.mask().0))
$name(self.0.bitxor(rhs.0))
}
}

impl BitXorAssign<$name> for $name {
fn bitxor_assign(&mut self, other: $name) {
*self = self.mask();
self.0.bitxor_assign(other.mask().0)
self.0.bitxor_assign(other.0)
}
}

impl Not for $name {
type Output = $name;

fn not(self) -> $name {
$name(self.mask().0.not())
$name(self.0.not()).mask()
}
}

impl<'a> Not for &'a $name {
type Output = <$name as Not>::Output;

fn not(self) -> $name {
$name(self.mask().0.not())
$name(self.0.not()).mask()
}
}

impl BitAnd<$name> for $name {
type Output = $name;

fn bitand(self, rhs: $name) -> Self::Output {
$name(self.mask().0.bitand(rhs.mask().0))
$name(self.0.bitand(rhs.0))
}
}

impl<'a> BitAnd<&'a $name> for $name {
type Output = <$name as BitOr<$name>>::Output;

fn bitand(self, rhs: &'a $name) -> Self::Output {
$name(self.mask().0.bitand(rhs.mask().0))
$name(self.0.bitand(rhs.0))
}
}

impl<'a> BitAnd<$name> for &'a $name {
type Output = <$name as BitOr<$name>>::Output;

fn bitand(self, rhs: $name) -> Self::Output {
$name(self.mask().0.bitand(rhs.mask().0))
$name(self.0.bitand(rhs.0))
}
}

impl<'a> BitAnd<&'a $name> for &'a $name {
type Output = <$name as BitOr<$name>>::Output;

fn bitand(self, rhs: &'a $name) -> Self::Output {
$name(self.mask().0.bitand(rhs.mask().0))
$name(self.0.bitand(rhs.0))
}
}

impl BitAndAssign<$name> for $name {
fn bitand_assign(&mut self, other: $name) {
*self = self.mask();
self.0.bitand_assign(other.mask().0)
self.0.bitand_assign(other.0)
}
}

Expand Down Expand Up @@ -757,6 +753,12 @@ mod tests {
assert_eq!(i7::MAX.wrapping_add(i7(4)), i7(-61));
}

#[test]
fn test_wrapping_sub() {
assert_eq!(u1::MIN.wrapping_sub(u1(1)), u1(1));
assert_eq!(u3(1).wrapping_sub(u3(2)), u3::MAX);
}

#[test]
#[should_panic]
fn test_add_overflow_u5() {
Expand Down Expand Up @@ -847,16 +849,16 @@ mod tests {

#[test]
fn test_shl() {
assert_eq!(u5(16) << 1usize, u5(32));
assert_eq!(u5(16) << 1u8, u5(32));
assert_eq!(u5(16) << 1u16, u5(32));
assert_eq!(u5(16) << 1u32, u5(32));
assert_eq!(u5(16) << 1u64, u5(32));
assert_eq!(u5(16) << 1isize, u5(32));
assert_eq!(u5(16) << 1i8, u5(32));
assert_eq!(u5(16) << 1i16, u5(32));
assert_eq!(u5(16) << 1i32, u5(32));
assert_eq!(u5(16) << 1i64, u5(32));
assert_eq!(u5(16) << 1usize, u5(0));
assert_eq!(u5(16) << 1u8, u5(0));
assert_eq!(u5(16) << 1u16, u5(0));
assert_eq!(u5(16) << 1u32, u5(0));
assert_eq!(u5(16) << 1u64, u5(0));
assert_eq!(u5(16) << 1isize, u5(0));
assert_eq!(u5(16) << 1i8, u5(0));
assert_eq!(u5(16) << 1i16, u5(0));
assert_eq!(u5(16) << 1i32, u5(0));
assert_eq!(u5(16) << 1i64, u5(0));

assert_eq!(u5::MAX << 4, u5(16));

Expand Down Expand Up @@ -894,6 +896,8 @@ mod tests {
assert_eq!(x, u9(128));
x <<= 1u8;
assert_eq!(x, u9(256));
x <<= 1u8;
assert_eq!(x, u9(0));
}

#[test]
Expand Down
Loading