Skip to main content

rbatis_codegen/
ops_xor.rs

1use crate::ops::{AsProxy, BitXor};
2use crate::ops_macros::{
3    impl_bool_scalar_binary_op, impl_numeric_binary_op, impl_same_type_binary_op,
4    impl_value_binary_op,
5};
6use rbs::Value;
7
8fn op_bitxor_value(left: Value, rhs: Value) -> Value {
9    match left {
10        Value::Bool(s) => Value::Bool(s ^ rhs.bool()),
11        Value::I32(s) => Value::I32(s ^ rhs.i32()),
12        Value::I64(s) => Value::I64(s ^ rhs.i64()),
13        Value::U32(s) => Value::U32(s ^ rhs.u32()),
14        Value::U64(s) => Value::U64(s ^ rhs.u64()),
15        Value::Ext(_, e) => op_bitxor_value(*e, rhs),
16        _ => Value::Null,
17    }
18}
19
20fn op_bitxor_u64(value: &Value, other: u64) -> u64 {
21    value.u64() ^ other
22}
23
24fn op_bitxor_i64(value: &Value, other: i64) -> i64 {
25    value.i64() ^ other
26}
27
28fn op_bitxor_f64(value: &Value, other: f64) -> f64 {
29    ((value.f64() as i64) ^ (other as i64)) as f64
30}
31
32impl_value_binary_op!(BitXor, op_bitxor, op_bitxor_value);
33impl_bool_scalar_binary_op!(BitXor, op_bitxor, ^);
34impl_numeric_binary_op!(
35    BitXor,
36    op_bitxor,
37    op_bitxor_u64,
38    op_bitxor_u64,
39    [u8, u16, u32, u64],
40    u64
41);
42impl_numeric_binary_op!(
43    BitXor,
44    op_bitxor,
45    op_bitxor_i64,
46    op_bitxor_i64,
47    [i8, i16, i32, i64, isize, usize],
48    i64
49);
50impl_numeric_binary_op!(
51    BitXor,
52    op_bitxor,
53    op_bitxor_f64,
54    op_bitxor_f64,
55    [f32, f64],
56    f64
57);
58impl_same_type_binary_op!(
59    BitXor,
60    op_bitxor,
61    ^,
62    [u8, u16, u32, u64, i8, i16, i32, i64, isize, usize, bool]
63);
64
65#[cfg(test)]
66mod test {
67    use crate::ops::BitXor;
68    use rbs::Value;
69
70    #[test]
71    fn test_bitxor() {
72        assert_eq!(Value::I32(5).op_bitxor(3i32), 6i64);
73        assert_eq!((&Value::I32(5)).op_bitxor(&3i32), 6i64);
74        assert_eq!(Value::I32(5).op_bitxor(&&Value::I32(3)), Value::I32(6));
75        assert!(true.op_bitxor(&false));
76        assert!(Value::Bool(true).op_bitxor(false));
77    }
78}