Skip to content

Commit

Permalink
implement bit_or operation
Browse files Browse the repository at this point in the history
  • Loading branch information
anhelinakruk committed Oct 8, 2024
1 parent b0f1404 commit b6aaba0
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion crates/cairo-serde/src/types/u256.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::CairoSerde;
use starknet::core::types::Felt;
use std::{cmp::Ordering, ops::Add};
use std::{
cmp::Ordering,
ops::{Add, BitOr},
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct U256 {
Expand Down Expand Up @@ -29,6 +32,17 @@ impl Add for U256 {
}
}

impl BitOr for U256 {
type Output = Self;

fn bitor(self, other: Self) -> Self {
U256 {
low: self.low | other.low,
high: self.high | other.high,
}
}
}

impl CairoSerde for U256 {
type RustType = Self;

Expand Down Expand Up @@ -150,6 +164,21 @@ mod tests {
assert_eq!(u256_3.high, 0_u128);
}

#[test]
fn test_bit_or_u256() {
let u256_1 = U256 {
low: 0b1010_u128,
high: 0b1100_u128,
};
let u256_2 = U256 {
low: 0b0110_u128,
high: 0b0011_u128,
};
let u256_3 = u256_1 | u256_2;
assert_eq!(u256_3.low, 0b1110_u128);
assert_eq!(u256_3.high, 0b1111_u128);
}

#[test]
fn test_serialize_u256_max() {
let low = u128::MAX;
Expand Down

0 comments on commit b6aaba0

Please sign in to comment.