Coverage Report

Created: 2025-07-11 07:25

/rust/registry/src/index.crates.io-6f17d22bba15001f/num-bigint-0.4.6/src/biguint/bits.rs
Line
Count
Source (jump to first uncovered line)
1
use super::{BigUint, IntDigits};
2
3
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign};
4
5
forward_val_val_binop!(impl BitAnd for BigUint, bitand);
6
forward_ref_val_binop!(impl BitAnd for BigUint, bitand);
7
8
// do not use forward_ref_ref_binop_commutative! for bitand so that we can
9
// clone the smaller value rather than the larger, avoiding over-allocation
10
impl BitAnd<&BigUint> for &BigUint {
11
    type Output = BigUint;
12
13
    #[inline]
14
0
    fn bitand(self, other: &BigUint) -> BigUint {
15
0
        // forward to val-ref, choosing the smaller to clone
16
0
        if self.data.len() <= other.data.len() {
17
0
            self.clone() & other
18
        } else {
19
0
            other.clone() & self
20
        }
21
0
    }
22
}
23
24
forward_val_assign!(impl BitAndAssign for BigUint, bitand_assign);
25
26
impl BitAnd<&BigUint> for BigUint {
27
    type Output = BigUint;
28
29
    #[inline]
30
0
    fn bitand(mut self, other: &BigUint) -> BigUint {
31
0
        self &= other;
32
0
        self
33
0
    }
34
}
35
impl BitAndAssign<&BigUint> for BigUint {
36
    #[inline]
37
0
    fn bitand_assign(&mut self, other: &BigUint) {
38
0
        for (ai, &bi) in self.data.iter_mut().zip(other.data.iter()) {
39
0
            *ai &= bi;
40
0
        }
41
0
        self.data.truncate(other.data.len());
42
0
        self.normalize();
43
0
    }
44
}
45
46
forward_all_binop_to_val_ref_commutative!(impl BitOr for BigUint, bitor);
47
forward_val_assign!(impl BitOrAssign for BigUint, bitor_assign);
48
49
impl BitOr<&BigUint> for BigUint {
50
    type Output = BigUint;
51
52
0
    fn bitor(mut self, other: &BigUint) -> BigUint {
53
0
        self |= other;
54
0
        self
55
0
    }
56
}
57
impl BitOrAssign<&BigUint> for BigUint {
58
    #[inline]
59
0
    fn bitor_assign(&mut self, other: &BigUint) {
60
0
        for (ai, &bi) in self.data.iter_mut().zip(other.data.iter()) {
61
0
            *ai |= bi;
62
0
        }
63
0
        if other.data.len() > self.data.len() {
64
0
            let extra = &other.data[self.data.len()..];
65
0
            self.data.extend(extra.iter().cloned());
66
0
        }
67
0
    }
68
}
69
70
forward_all_binop_to_val_ref_commutative!(impl BitXor for BigUint, bitxor);
71
forward_val_assign!(impl BitXorAssign for BigUint, bitxor_assign);
72
73
impl BitXor<&BigUint> for BigUint {
74
    type Output = BigUint;
75
76
0
    fn bitxor(mut self, other: &BigUint) -> BigUint {
77
0
        self ^= other;
78
0
        self
79
0
    }
80
}
81
impl BitXorAssign<&BigUint> for BigUint {
82
    #[inline]
83
0
    fn bitxor_assign(&mut self, other: &BigUint) {
84
0
        for (ai, &bi) in self.data.iter_mut().zip(other.data.iter()) {
85
0
            *ai ^= bi;
86
0
        }
87
0
        if other.data.len() > self.data.len() {
88
0
            let extra = &other.data[self.data.len()..];
89
0
            self.data.extend(extra.iter().cloned());
90
0
        }
91
0
        self.normalize();
92
0
    }
93
}