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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! Ranged integer types and math
//!
//! Do you ever need to restrict a [`u8`] from 0 to 100 or restrict any other
//! integer type to any other range? Then this crate is for you! The ranges
//! are encoded in the type system, so you only need to
//! [validate the range once] (and it can even be [at compile time]!). This
//! crate is sort of like a combination of similar crates [deranged] and [ux].
//!
//! This crate heavily leverages the type system to allow for powerful ranged
//! integer mathematics, covering [arbitrary `i{N}` / `u{N}` types](bitwise),
//! [unit integers](mod@unit), non-zero divisions, [ASCII](ascii), and const
//! operations. Enable the _**`serde`**_ feature for range-validated
//! deserialization / serialization (implements [`Serialize`] and
//! [`Deserialize`] for `Ranged*` types).
//!
//! # Types of operations
//!
//! Like the std library, ranch provide [`strict`](#strict),
//! [`checked`](#checked), and [`saturating`](#saturating) integer operations.
//! In addition, ranch also provides [`constant`](#constant) and
//! [`ranged`](#ranged) operations, which modify the output ranges.
//!
//! ## Strict
//!
//! Strict operations panic when out of range, or a division by nonzero occurs.
//! This is exposed in ranch with `+`, `-`, `/`, `*`, `%`. Using the other
//! provided operation methods will never result in UB (even if unsafe is used
//! to set the inner value to something out of range), but may result in logic
//! bugs and panics on invalid bit patterns.
//!
//! ```rust
//! # use ranch::RangedI32;
//! assert_eq!(
//! RangedI32::<2, 7>::new::<2>() + 5,
//! RangedI32::<2, 7>::new::<7>(),
//! );
//! ```
//!
//! Panics:
//!
//! ```rust,should_panic
//! # use ranch::RangedI32;
//! let _ = RangedI32::<2, 7>::new::<2>() + 6;
//! ```
//!
//! ## Checked
//!
//! Checked operations are methods starting with `checked_`; They return an
//! [`Option`] (unsigned) or [`Result`] (signed). Divisions by zero-able types
//! additionally wrap the result in a [`Quotient`].
//!
//! ```rust
//! # use ranch::RangedI32;
//! assert_eq!(
//! RangedI32::<2, 7>::new::<2>().checked_add(5).unwrap(),
//! RangedI32::<2, 7>::new::<7>(),
//! );
//! RangedI32::<2, 7>::new::<2>().checked_add(6).unwrap_err();
//! ```
//!
//! ## Saturating
//!
//! Saturating operations are similar to checked, except that the `Option` and
//! `Result` are stripped and `Self::MIN` or `Self::MAX` is returned on
//! overflow.
//!
//! ```rust
//! # use ranch::RangedI32;
//! assert_eq!(
//! RangedI32::<2, 7>::new::<2>().saturating_add(5),
//! RangedI32::<2, 7>::new::<7>(),
//! );
//! assert_eq!(
//! RangedI32::<2, 7>::new::<2>().saturating_add(6),
//! RangedI32::<2, 7>::new::<7>(),
//! );
//! ```
//!
//! ## Constant
//!
//! Constant operations add a constant value and modify the output's range
//! accordingly. Due to limitations in Rust, you have to specify the output
//! range, but ranch will check your work and tell you if you're wrong.
//!
//! ```rust
//! # use ranch::RangedI32;
//! assert_eq!(
//! RangedI32::<2, 7>::new::<2>().add::<5, 7, 12>(),
//! RangedI32::<7, 12>::new::<7>(),
//! );
//! assert_eq!(
//! RangedI32::<2, 7>::new::<2>().add::<6, 8, 13>(),
//! RangedI32::<8, 13>::new::<8>(),
//! );
//! ```
//!
//! ## Ranged
//!
//! Similar to constant operations, these modify the output's range, but also
//! allow for some runtime variation. In this example, the value added to a
//! number between 2 and 7 can be either 6 or 7:
//!
//! ```rust
//! # use ranch::RangedI32;
//! assert_eq!(
//! RangedI32::<2, 7>::new::<2>()
//! .add_ranged(RangedI32::<6, 7>::new::<6>()),
//! RangedI32::<8, 14>::new::<8>(),
//! );
//! assert_eq!(
//! RangedI32::<2, 7>::new::<2>()
//! .add_ranged(RangedI32::<6, 7>::new::<7>()),
//! RangedI32::<8, 14>::new::<9>(),
//! );
//! ```
//!
//! [deranged]: https://docs.rs/crate/deranged
//! [ux]: https://docs.rs/crate/ux
//! [validate the range once]: RangedI32::with_i32()
//! [at compile time]: RangedI32::new()
//! [`Serialize`]: serde_core::Serialize
//! [`Deserialize`]: serde_core::Deserialize
pub use ;
use crate;