sway_types/
integer_bits.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::fmt;

#[derive(Eq, PartialEq, Hash, Debug, Clone, Copy, PartialOrd, Ord)]
pub enum IntegerBits {
    Eight,
    Sixteen,
    ThirtyTwo,
    SixtyFour,
    V256,
}

impl IntegerBits {
    /// Returns if `v` would overflow using `self` bits or not.
    pub fn would_overflow(&self, v: u64) -> bool {
        if v == 0 {
            return false;
        }

        let needed_bits = v.ilog2() + 1;
        let bits = match self {
            IntegerBits::Eight => 8,
            IntegerBits::Sixteen => 16,
            IntegerBits::ThirtyTwo => 32,
            IntegerBits::SixtyFour => 64,
            IntegerBits::V256 => return false,
        };

        needed_bits > bits
    }
}

#[test]
fn would_overflow_tests() {
    assert!(!IntegerBits::Eight.would_overflow(0));

    assert!(!IntegerBits::Eight.would_overflow(0xFF));
    assert!(IntegerBits::Eight.would_overflow(0x100));

    assert!(!IntegerBits::Sixteen.would_overflow(0xFFFF));
    assert!(IntegerBits::Sixteen.would_overflow(0x10000));

    assert!(!IntegerBits::ThirtyTwo.would_overflow(0xFFFFFFFF));
    assert!(IntegerBits::ThirtyTwo.would_overflow(0x100000000));

    assert!(!IntegerBits::SixtyFour.would_overflow(0xFFFFFFFFFFFFFFFF));
}

impl fmt::Display for IntegerBits {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use IntegerBits::{Eight, Sixteen, SixtyFour, ThirtyTwo, V256};
        let s = match self {
            Eight => "eight",
            Sixteen => "sixteen",
            ThirtyTwo => "thirty two",
            SixtyFour => "sixty four",
            V256 => "256",
        };
        write!(f, "{s}")
    }
}