Debian Codegraph @

third_party/rust/framehop/src/add_signed.rs

raw
1 /// Add a signed integer to this unsigned integer, with wrapping.
2 #[allow(unused)]
3 pub fn wrapping_add_signed<T: AddSigned>(lhs: T, rhs: T::Signed) -> T {
4 lhs.wrapping_add_signed(rhs)
5 }
6
7 /// Add a signed integer to this unsigned integer, but only if doing so
8 /// does not cause underflow / overflow.
9 pub fn checked_add_signed<T: AddSigned>(lhs: T, rhs: T::Signed) -> Option<T> {
10 lhs.checked_add_signed(rhs)
11 }
12
13 /// A trait which adds method to unsigned integers which allow checked and
14 /// wrapping addition of the corresponding signed integer type.
15 /// Unfortunately, these methods conflict with the proposed standard rust
16 /// methods, so this trait isn't actually usable without risking build
17 /// errors once these methods are stabilized.
18 /// https://github.com/rust-lang/rust/issues/87840
19 pub trait AddSigned: Sized {
20 type Signed;
21
22 /// Add a signed integer to this unsigned integer, with wrapping.
23 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
24
25 /// Add a signed integer to this unsigned integer, but only if doing so
26 /// does not cause underflow / overflow.
27 fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self>;
28 }
29
30 impl AddSigned for u64 {
31 type Signed = i64;
32
33 fn wrapping_add_signed(self, rhs: i64) -> u64 {
34 self.wrapping_add(rhs as u64)
35 }
36
37 fn checked_add_signed(self, rhs: i64) -> Option<u64> {
38 let res = AddSigned::wrapping_add_signed(self, rhs);
39 if (rhs >= 0 && res >= self) || (rhs < 0 && res < self) {
40 Some(res)
41 } else {
42 None
43 }
44 }
45 }
46
47 impl AddSigned for u32 {
48 type Signed = i32;
49
50 fn wrapping_add_signed(self, rhs: i32) -> u32 {
51 self.wrapping_add(rhs as u32)
52 }
53
54 fn checked_add_signed(self, rhs: i32) -> Option<u32> {
55 let res = AddSigned::wrapping_add_signed(self, rhs);
56 if (rhs >= 0 && res >= self) || (rhs < 0 && res < self) {
57 Some(res)
58 } else {
59 None
60 }
61 }
62 }
63
64 #[cfg(test)]
65 mod test {
66 use super::{checked_add_signed, wrapping_add_signed};
67
68 #[test]
69 fn test_wrapping() {
70 assert_eq!(wrapping_add_signed(1, 2), 3u64);
71 assert_eq!(wrapping_add_signed(2, 1), 3u64);
72 assert_eq!(wrapping_add_signed(5, -4), 1u64);
73 assert_eq!(wrapping_add_signed(5, -5), 0u64);
74 assert_eq!(wrapping_add_signed(u64::MAX - 5, 3), u64::MAX - 2);
75 assert_eq!(wrapping_add_signed(u64::MAX - 5, 5), u64::MAX);
76 assert_eq!(wrapping_add_signed(u64::MAX - 5, -5), u64::MAX - 10);
77 assert_eq!(wrapping_add_signed(1, -2), u64::MAX);
78 assert_eq!(wrapping_add_signed(2, -4), u64::MAX - 1);
79 assert_eq!(wrapping_add_signed(u64::MAX, 1), 0);
80 assert_eq!(wrapping_add_signed(u64::MAX - 5, 6), 0);
81 assert_eq!(wrapping_add_signed(u64::MAX - 5, 9), 3);
82 }
83
84 #[test]
85 fn test_checked() {
86 assert_eq!(checked_add_signed(1, 2), Some(3u64));
87 assert_eq!(checked_add_signed(2, 1), Some(3u64));
88 assert_eq!(checked_add_signed(5, -4), Some(1u64));
89 assert_eq!(checked_add_signed(5, -5), Some(0u64));
90 assert_eq!(checked_add_signed(u64::MAX - 5, 3), Some(u64::MAX - 2));
91 assert_eq!(checked_add_signed(u64::MAX - 5, 5), Some(u64::MAX));
92 assert_eq!(checked_add_signed(u64::MAX - 5, -5), Some(u64::MAX - 10));
93 assert_eq!(checked_add_signed(1u64, -2), None);
94 assert_eq!(checked_add_signed(2u64, -4), None);
95 assert_eq!(checked_add_signed(u64::MAX, 1), None);
96 assert_eq!(checked_add_signed(u64::MAX - 5, 6), None);
97 assert_eq!(checked_add_signed(u64::MAX - 5, 9), None);
98 }
99 }