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
//](https://crates.io/crates/safe-math)
//](https://docs.rs/safe-math)
//](README.md)
//!
//!A procedural macro-based library that transforms standard arithmetic operations into their checked equivalents at compile time, preventing overflow, underflow, and division by zero errors.
//!
//!# Overview
//!
//!`safe-math` provides:
//!- Compile-time transformation of arithmetic operations into checked variants without runtime overhead
//!- Comprehensive error handling via `Result` types
//!- Support for custom types through derive macros
//!
//!# Core Functionality
//!
//!## Basic Operations
//!
//!The `#[safe_math]` attribute transforms arithmetic operations into their checked equivalents:
//!
//!```rust
//!use safe_math::safe_math;
//!
//!#[safe_math]
//!fn add(a: u8, b: u8) -> Result<u8, safe_math::SafeMathError> {
//! Ok(a + b) // Automatically uses checked addition
//!}
//!
//!assert_eq!(add(10, 20), Ok(30));
//!assert_eq!(add(255, 1), Err(safe_math::SafeMathError::Overflow));
//!```
//!
//!## Supported Operations
//!
//!All basic arithmetic operations are supported:
//!- Addition (`+`, `+=`)
//!- Subtraction (`-`, `-=`)
//!- Multiplication (`*`, `*=`)
//!- Division (`/`, `/=`)
//!- Remainder (`%`, `%=`)
//!
//!## Error Handling
//!
//!Operations return `SafeMathError` for exceptional cases:
//!```rust
//!pub enum SafeMathError {
//! Overflow, // Result exceeds type bounds
//! DivisionByZero, // Division or remainder by zero
//! InfiniteOrNaN, // Result is infinite or NaN (floating-point types)
//! NotImplemented, // Missing trait implementation (derive feature)
//!}
//!```
//!
//!## Type Support
//!
//!Built-in support for:
//!- Unsigned integers: `u8` through `u128`, `usize`
//!- Signed integers: `i8` through `i128`, `isize`
//!- Floating point: `f32`, `f64` (with infinity/NaN handling)
//!
//!# Advanced Usage
//!
//!## Custom Types
//!
//!Enable the `derive` feature to implement safe arithmetic for custom types:
//!
//!```rust,ignore
//!use safe_math::SafeMathOps;
//!
//!#[derive(SafeMathOps)]
//!#[SafeMathOps(add, sub, mul, div, rem)]
//!struct MyNumber(u32);
//!
//!#[safe_math]
//!fn calculate(a: MyNumber, b: MyNumber) -> Result<MyNumber, safe_math::SafeMathError> {
//! Ok(a + b)
//!}
//!```
//!
//!**Note:** For the derive to work, your type must implement both the standard arithmetic traits
//!(like `Add`, `Sub`, `Mul`, `Div`, `Rem`) and their checked counterparts (like `CheckedAdd`,
//!`CheckedSub`, `CheckedMul`, `CheckedDiv`, `CheckedRem`) from the `num-traits` crate.
//!
//!This requirement exists because without knowing what a type represents, it's impossible to
//!determine what operations are safe to perform or what constitutes a "checked" operation.
//!
//!## Block-Level Safety
//!
//!Use `safe_math_block!` to apply checked operations to a specific block of code:
//!
//!```rust
//!use safe_math::safe_math_block;
//!
//!fn process_numbers(a: u32, b: u32, c: u32) -> Result<u32, safe_math::SafeMathError> {
//! // Only this block uses checked arithmetic
//! let result = safe_math_block!({
//! let product = a * b;
//! let sum = product + c;
//! sum / b
//! });
//! Ok(result)
//!}
//!```
//!
//!This is useful when you want to:
//!- Apply safe arithmetic to specific expression
//!- Mix checked and unchecked operations in the same function
//!
//!# Roadmap
//!
//!Planned upcoming features:
//!
//!- **Option-returning functions**
//! Support for functions that return `Option<T>` instead of `Result<T, SafeMathError>`.
//!
//!- **Crate-level macro support**
//! Ability to apply `#[safe_math]` to the entire crate with a single attribute:
//!
//!```rust,ignore
//!// main.rs or lib.rs
//!#![safe_math]
//!
//!fn demo(a: u32, b: u32) -> Result<u32, safe_math::SafeMathError> {
//! Ok(a * b + 1)
//!}
//!```
//!
//!# License
//!
//!Licensed under either:
//!- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE))
//!- MIT license ([LICENSE-MIT](LICENSE-MIT))
//!
//!at your option.
//!
//!# Contributing
//!
//!Unless you explicitly state otherwise, any contribution intentionally submitted
//!for inclusion in this crate by you shall be dual licensed as above, without any
//!additional terms or conditions.
// Re-export the procedural macro so users can simply `use safe_math::safe_math`.
pub use SafeMathOps;
pub use ;
// Re-export the most relevant items at the crate root for a clean API.
pub use SafeMathError;
pub use ;
// These helper functions are intentionally re-exported because the macro expands to them
pub use ;
// Internal modules