Skip to content

Add operators to Byte and Bool #655

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

Merged
merged 7 commits into from
Nov 28, 2023
Merged

Add operators to Byte and Bool #655

merged 7 commits into from
Nov 28, 2023

Conversation

bernsteining
Copy link
Contributor

Currently rune doesn't allow these bitwise operators on the Byte type:

  • BitAnd (u8 & u8)
  • BitAndAssign (u8 &= u8)
  • BitXor (u8 ^ u8)
  • BitXorAssign (u8 ^= u8)
  • BitOr (u8 | u8)
  • BitOrAssign (u8 |= u8)
  • Shl (u8 << u8)
  • ShlAssign (u8 <<= u8)
  • Shr (u8 >> u8)
  • ShrAssign (u8 >>= u8)

Also, rune doesn't allow these bitwise operators on the Bool type:

  • BitAndAssign (bool &= bool)
  • BitXorAssign (bool ^= bool)
  • BitOrAssign (bool |= bool)

The following commit adds all these bitwise operators.

Then, following rune script can be run as expected:

pub fn main() {

    // byte ops
    let byte = 128u8;

    println!("byte & 1u8 {}", byte & 1u8);
    byte &= 1u8;
    println!("{}", byte);

    println!("byte ^ 1u8: {}", byte ^ 1u8);
    byte ^= 1u8;
    println!("{}", byte);

    println!("byte | 1u8: {}", byte | 1u8 );
    byte |= 1u8;
    println!("{}", byte);

    println!("byte << 1u8: {}", byte << 1u8);
    byte <<= 1u8;
    println!("{}", byte);

    println!("byte >> 1u8: {}", byte >> 1u8);
    byte >>= 1u8;
    println!("{}", byte);


    // bool ops
    let mybool = true;

    mybool &= true;
    println!("{}", mybool);

    mybool ^= true;
    println!("{}", mybool);

    mybool |= true;
    println!("{}", mybool);
}

@udoprog udoprog added the enhancement New feature or request label Nov 16, 2023
@udoprog
Copy link
Collaborator

udoprog commented Nov 16, 2023

Build failure does not look like your fault. I'll check it out when I get a minute.

@udoprog
Copy link
Collaborator

udoprog commented Nov 16, 2023

The one part I'm missing is some tests in vm_arithmetic.

@bernsteining
Copy link
Contributor Author

The one part I'm missing is some tests in vm_arithmetic.

Indeed tests were missing, thanks for pointing where to write them.

I added a few tests (d10dbb5) for u8 ops in a rather verbose fashion since I'm not familiar with macros yet.

I also wanted to test for Underflow and Overflow errors as follow:

error_test!(1u8 << 8 = Overflow);
error_test!(255u8 >> 8 = Underflow);

But the tests wouldn't pass as expected.

Writing these tests made me realise that shifting u8 is way more natural with i64 directly so d10dbb5 takes care of that.

@udoprog
Copy link
Collaborator

udoprog commented Nov 28, 2023

Moved the tests to use op_tests! for better coverage (which had to be modified), and discovered that some assign ops did not use i64 as operand, so fixed that.

@udoprog udoprog merged commit 1fdc17f into rune-rs:main Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants