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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use ;
use quote;
use ;
use MathBlockVisitor;
/// Defines a block of code where all mathematical operations are performed using checked methods.
///
/// If any operation overflows, it will panic with a corresponding error message.
///
/// # Example
///
/// ```rust
/// use overf::checked;
///
/// checked! {
/// let a = 10usize + 5usize;
/// let b = 20usize - 10usize;
/// let c = 3usize * 7usize;
/// }
/// ```
/// Defines a block of code where all mathematical operations use overflowing methods.
///
/// When an operation overflows, it will not panic; instead, it will return the result of the operation, wrapping around if necessary.
///
/// # Example
///
/// ```rust
/// use overf::overflowing;
///
/// overflowing! {
/// let a = 10usize + 5usize;
/// let b = 200usize - 300usize; // Overflows
/// }
/// ```
/// Defines a block of code where all mathematical operations use saturating methods.
///
/// When an operation would overflow, it will instead return the maximum (or minimum) value of the type.
///
/// # Example
///
/// ```rust
/// use overf::saturating;
///
/// saturating! {
/// let a = usize::MAX + 1; // Saturates to usize::MAX
/// let b = usize::MIN - 1; // Saturates to usize::MIN
/// }
/// ```
/// Defines a block of code where all mathematical operations use checked methods.
/// If any operation results in an overflow, it will return `None`, propagating the error using the `?` operator.
///
/// This macro is useful when you want to handle potential overflows in a concise way, allowing the calling function
/// to manage the error or terminate early.
///
/// # Example
///
/// ```rust
/// use overf::propagating;
///
/// fn example() -> Option<usize> {
/// propagating! {
/// // Returns `None` if any operation fails.
/// let a = 10usize + 5usize;
/// let b = 20usize - 10usize;
/// let c = 3usize * 7usize;
/// let d = 21usize / 3usize;
/// let e = a + b;
/// Some(e)
/// }
/// }
///
/// example().unwrap();
/// ```
/// Defines a block of code where all mathematical operations use wrapping methods.
///
/// When an operation overflows, it will not panic; instead, it will return the result of the operation, wrapping around if necessary.
///
/// # Example
///
/// ```rust
/// use overf::wrapping;
///
/// wrapping! {
/// let a = 10usize + 5usize;
/// let b = 200usize - 300usize; // Overflows
/// }
/// ```
/// Resets the overflow behavior to the default behavior of Rust.
///
/// This is useful when you want to exit a block with custom overflow handling and revert to the standard behavior.
///
/// # Example
///
/// ```rust
/// use overf::{checked, default};
///
/// checked! {
/// let a = 10usize + 5usize; // checked
///
/// default! {
/// let b = a + 1000; // Uses default behavior (may panic or overflow)
/// }
/// }
/// ```