]>
Commit | Line | Data |
---|---|---|
1b1a35ee XL |
1 | macro_rules! int_impl { |
2 | ($SelfT:ty, $ActualT:ident, $UnsignedT:ty, $BITS:expr, $Min:expr, $Max:expr, $Feature:expr, | |
3 | $EndFeature:expr, $rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr, | |
4 | $reversed:expr, $le_bytes:expr, $be_bytes:expr, | |
5 | $to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => { | |
74b04a01 XL |
6 | doc_comment! { |
7 | concat!("The smallest value that can be represented by this integer type. | |
f9f354fc XL |
8 | |
9 | # Examples | |
10 | ||
1b1a35ee | 11 | Basic usage: |
f9f354fc | 12 | |
f9f354fc | 13 | ``` |
1b1a35ee XL |
14 | ", $Feature, "assert_eq!(", stringify!($SelfT), "::MIN, ", stringify!($Min), ");", |
15 | $EndFeature, " | |
16 | ```"), | |
17 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
18 | pub const MIN: Self = !0 ^ ((!0 as $UnsignedT) >> 1) as Self; | |
74b04a01 XL |
19 | } |
20 | ||
21 | doc_comment! { | |
22 | concat!("The largest value that can be represented by this integer type. | |
f9f354fc XL |
23 | |
24 | # Examples | |
25 | ||
1b1a35ee XL |
26 | Basic usage: |
27 | ||
28 | ``` | |
29 | ", $Feature, "assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($Max), ");", | |
30 | $EndFeature, " | |
31 | ```"), | |
32 | #[stable(feature = "assoc_int_consts", since = "1.43.0")] | |
33 | pub const MAX: Self = !Self::MIN; | |
34 | } | |
35 | ||
36 | doc_comment! { | |
37 | concat!("The size of this integer type in bits. | |
38 | ||
39 | # Examples | |
40 | ||
41 | ``` | |
42 | ", $Feature, "#![feature(int_bits_const)] | |
43 | assert_eq!(", stringify!($SelfT), "::BITS, ", stringify!($BITS), ");", | |
44 | $EndFeature, " | |
45 | ```"), | |
46 | #[unstable(feature = "int_bits_const", issue = "76904")] | |
47 | pub const BITS: u32 = $BITS; | |
48 | } | |
49 | ||
50 | doc_comment! { | |
51 | concat!("Converts a string slice in a given base to an integer. | |
52 | ||
53 | The string is expected to be an optional `+` or `-` sign followed by digits. | |
54 | Leading and trailing whitespace represent an error. Digits are a subset of these characters, | |
55 | depending on `radix`: | |
56 | ||
57 | * `0-9` | |
58 | * `a-z` | |
59 | * `A-Z` | |
60 | ||
61 | # Panics | |
62 | ||
63 | This function panics if `radix` is not in the range from 2 to 36. | |
64 | ||
65 | # Examples | |
66 | ||
67 | Basic usage: | |
68 | ||
69 | ``` | |
70 | ", $Feature, "assert_eq!(", stringify!($SelfT), "::from_str_radix(\"A\", 16), Ok(10));", | |
71 | $EndFeature, " | |
72 | ```"), | |
73 | #[stable(feature = "rust1", since = "1.0.0")] | |
74 | pub fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> { | |
75 | from_str_radix(src, radix) | |
76 | } | |
77 | } | |
78 | ||
79 | doc_comment! { | |
80 | concat!("Returns the number of ones in the binary representation of `self`. | |
81 | ||
82 | # Examples | |
83 | ||
84 | Basic usage: | |
85 | ||
86 | ``` | |
87 | ", $Feature, "let n = 0b100_0000", stringify!($SelfT), "; | |
f9f354fc | 88 | |
1b1a35ee XL |
89 | assert_eq!(n.count_ones(), 1);", |
90 | $EndFeature, " | |
f9f354fc XL |
91 | ``` |
92 | "), | |
1b1a35ee XL |
93 | #[stable(feature = "rust1", since = "1.0.0")] |
94 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
95 | #[inline] | |
96 | pub const fn count_ones(self) -> u32 { (self as $UnsignedT).count_ones() } | |
97 | } | |
98 | ||
99 | doc_comment! { | |
100 | concat!("Returns the number of zeros in the binary representation of `self`. | |
101 | ||
102 | # Examples | |
103 | ||
104 | Basic usage: | |
105 | ||
106 | ``` | |
107 | ", $Feature, "assert_eq!(", stringify!($SelfT), "::MAX.count_zeros(), 1);", $EndFeature, " | |
108 | ```"), | |
109 | #[stable(feature = "rust1", since = "1.0.0")] | |
110 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
111 | #[inline] | |
112 | pub const fn count_zeros(self) -> u32 { | |
113 | (!self).count_ones() | |
114 | } | |
115 | } | |
116 | ||
117 | doc_comment! { | |
118 | concat!("Returns the number of leading zeros in the binary representation of `self`. | |
119 | ||
120 | # Examples | |
121 | ||
122 | Basic usage: | |
123 | ||
124 | ``` | |
125 | ", $Feature, "let n = -1", stringify!($SelfT), "; | |
126 | ||
127 | assert_eq!(n.leading_zeros(), 0);", | |
128 | $EndFeature, " | |
129 | ```"), | |
130 | #[stable(feature = "rust1", since = "1.0.0")] | |
131 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
132 | #[inline] | |
133 | pub const fn leading_zeros(self) -> u32 { | |
134 | (self as $UnsignedT).leading_zeros() | |
135 | } | |
136 | } | |
137 | ||
138 | doc_comment! { | |
139 | concat!("Returns the number of trailing zeros in the binary representation of `self`. | |
140 | ||
141 | # Examples | |
142 | ||
143 | Basic usage: | |
144 | ||
145 | ``` | |
146 | ", $Feature, "let n = -4", stringify!($SelfT), "; | |
147 | ||
148 | assert_eq!(n.trailing_zeros(), 2);", | |
149 | $EndFeature, " | |
150 | ```"), | |
151 | #[stable(feature = "rust1", since = "1.0.0")] | |
152 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
153 | #[inline] | |
154 | pub const fn trailing_zeros(self) -> u32 { | |
155 | (self as $UnsignedT).trailing_zeros() | |
156 | } | |
157 | } | |
158 | ||
159 | doc_comment! { | |
160 | concat!("Returns the number of leading ones in the binary representation of `self`. | |
161 | ||
162 | # Examples | |
163 | ||
164 | Basic usage: | |
165 | ||
166 | ``` | |
167 | ", $Feature, "let n = -1", stringify!($SelfT), "; | |
168 | ||
169 | assert_eq!(n.leading_ones(), ", stringify!($BITS), ");", | |
170 | $EndFeature, " | |
171 | ```"), | |
172 | #[stable(feature = "leading_trailing_ones", since = "1.46.0")] | |
173 | #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")] | |
174 | #[inline] | |
175 | pub const fn leading_ones(self) -> u32 { | |
176 | (self as $UnsignedT).leading_ones() | |
177 | } | |
178 | } | |
179 | ||
180 | doc_comment! { | |
181 | concat!("Returns the number of trailing ones in the binary representation of `self`. | |
182 | ||
183 | # Examples | |
184 | ||
185 | Basic usage: | |
186 | ||
187 | ``` | |
188 | ", $Feature, "let n = 3", stringify!($SelfT), "; | |
189 | ||
190 | assert_eq!(n.trailing_ones(), 2);", | |
191 | $EndFeature, " | |
192 | ```"), | |
193 | #[stable(feature = "leading_trailing_ones", since = "1.46.0")] | |
194 | #[rustc_const_stable(feature = "leading_trailing_ones", since = "1.46.0")] | |
195 | #[inline] | |
196 | pub const fn trailing_ones(self) -> u32 { | |
197 | (self as $UnsignedT).trailing_ones() | |
198 | } | |
199 | } | |
200 | ||
201 | doc_comment! { | |
202 | concat!("Shifts the bits to the left by a specified amount, `n`, | |
203 | wrapping the truncated bits to the end of the resulting integer. | |
204 | ||
205 | Please note this isn't the same operation as the `<<` shifting operator! | |
206 | ||
207 | # Examples | |
208 | ||
209 | Basic usage: | |
210 | ||
211 | ``` | |
212 | let n = ", $rot_op, stringify!($SelfT), "; | |
213 | let m = ", $rot_result, "; | |
214 | ||
215 | assert_eq!(n.rotate_left(", $rot, "), m); | |
216 | ```"), | |
217 | #[stable(feature = "rust1", since = "1.0.0")] | |
218 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
219 | #[must_use = "this returns the result of the operation, \ | |
220 | without modifying the original"] | |
221 | #[inline] | |
222 | pub const fn rotate_left(self, n: u32) -> Self { | |
223 | (self as $UnsignedT).rotate_left(n) as Self | |
224 | } | |
225 | } | |
226 | ||
227 | doc_comment! { | |
228 | concat!("Shifts the bits to the right by a specified amount, `n`, | |
229 | wrapping the truncated bits to the beginning of the resulting | |
230 | integer. | |
231 | ||
232 | Please note this isn't the same operation as the `>>` shifting operator! | |
233 | ||
234 | # Examples | |
235 | ||
236 | Basic usage: | |
237 | ||
238 | ``` | |
239 | let n = ", $rot_result, stringify!($SelfT), "; | |
240 | let m = ", $rot_op, "; | |
241 | ||
242 | assert_eq!(n.rotate_right(", $rot, "), m); | |
243 | ```"), | |
244 | #[stable(feature = "rust1", since = "1.0.0")] | |
245 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
246 | #[must_use = "this returns the result of the operation, \ | |
247 | without modifying the original"] | |
248 | #[inline] | |
249 | pub const fn rotate_right(self, n: u32) -> Self { | |
250 | (self as $UnsignedT).rotate_right(n) as Self | |
251 | } | |
252 | } | |
253 | ||
254 | doc_comment! { | |
255 | concat!("Reverses the byte order of the integer. | |
256 | ||
257 | # Examples | |
258 | ||
259 | Basic usage: | |
260 | ||
261 | ``` | |
262 | let n = ", $swap_op, stringify!($SelfT), "; | |
263 | ||
264 | let m = n.swap_bytes(); | |
265 | ||
266 | assert_eq!(m, ", $swapped, "); | |
267 | ```"), | |
268 | #[stable(feature = "rust1", since = "1.0.0")] | |
269 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
270 | #[inline] | |
271 | pub const fn swap_bytes(self) -> Self { | |
272 | (self as $UnsignedT).swap_bytes() as Self | |
273 | } | |
274 | } | |
275 | ||
276 | doc_comment! { | |
277 | concat!("Reverses the bit pattern of the integer. | |
278 | ||
279 | # Examples | |
280 | ||
281 | Basic usage: | |
282 | ||
283 | ``` | |
284 | let n = ", $swap_op, stringify!($SelfT), "; | |
285 | let m = n.reverse_bits(); | |
286 | ||
287 | assert_eq!(m, ", $reversed, "); | |
288 | ```"), | |
289 | #[stable(feature = "reverse_bits", since = "1.37.0")] | |
290 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
291 | #[inline] | |
292 | #[must_use] | |
293 | pub const fn reverse_bits(self) -> Self { | |
294 | (self as $UnsignedT).reverse_bits() as Self | |
295 | } | |
296 | } | |
297 | ||
298 | doc_comment! { | |
299 | concat!("Converts an integer from big endian to the target's endianness. | |
300 | ||
301 | On big endian this is a no-op. On little endian the bytes are swapped. | |
302 | ||
303 | # Examples | |
304 | ||
305 | Basic usage: | |
306 | ||
307 | ``` | |
308 | ", $Feature, "let n = 0x1A", stringify!($SelfT), "; | |
309 | ||
310 | if cfg!(target_endian = \"big\") { | |
311 | assert_eq!(", stringify!($SelfT), "::from_be(n), n) | |
312 | } else { | |
313 | assert_eq!(", stringify!($SelfT), "::from_be(n), n.swap_bytes()) | |
314 | }", | |
315 | $EndFeature, " | |
316 | ```"), | |
317 | #[stable(feature = "rust1", since = "1.0.0")] | |
318 | #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")] | |
319 | #[inline] | |
320 | pub const fn from_be(x: Self) -> Self { | |
321 | #[cfg(target_endian = "big")] | |
322 | { | |
323 | x | |
324 | } | |
325 | #[cfg(not(target_endian = "big"))] | |
326 | { | |
327 | x.swap_bytes() | |
328 | } | |
329 | } | |
330 | } | |
331 | ||
332 | doc_comment! { | |
333 | concat!("Converts an integer from little endian to the target's endianness. | |
334 | ||
335 | On little endian this is a no-op. On big endian the bytes are swapped. | |
336 | ||
337 | # Examples | |
338 | ||
339 | Basic usage: | |
340 | ||
341 | ``` | |
342 | ", $Feature, "let n = 0x1A", stringify!($SelfT), "; | |
343 | ||
344 | if cfg!(target_endian = \"little\") { | |
345 | assert_eq!(", stringify!($SelfT), "::from_le(n), n) | |
346 | } else { | |
347 | assert_eq!(", stringify!($SelfT), "::from_le(n), n.swap_bytes()) | |
348 | }", | |
349 | $EndFeature, " | |
350 | ```"), | |
351 | #[stable(feature = "rust1", since = "1.0.0")] | |
352 | #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")] | |
353 | #[inline] | |
354 | pub const fn from_le(x: Self) -> Self { | |
355 | #[cfg(target_endian = "little")] | |
356 | { | |
357 | x | |
358 | } | |
359 | #[cfg(not(target_endian = "little"))] | |
360 | { | |
361 | x.swap_bytes() | |
362 | } | |
363 | } | |
364 | } | |
365 | ||
366 | doc_comment! { | |
367 | concat!("Converts `self` to big endian from the target's endianness. | |
368 | ||
369 | On big endian this is a no-op. On little endian the bytes are swapped. | |
370 | ||
371 | # Examples | |
372 | ||
373 | Basic usage: | |
374 | ||
375 | ``` | |
376 | ", $Feature, "let n = 0x1A", stringify!($SelfT), "; | |
377 | ||
378 | if cfg!(target_endian = \"big\") { | |
379 | assert_eq!(n.to_be(), n) | |
380 | } else { | |
381 | assert_eq!(n.to_be(), n.swap_bytes()) | |
382 | }", | |
383 | $EndFeature, " | |
384 | ```"), | |
385 | #[stable(feature = "rust1", since = "1.0.0")] | |
386 | #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")] | |
387 | #[inline] | |
388 | pub const fn to_be(self) -> Self { // or not to be? | |
389 | #[cfg(target_endian = "big")] | |
390 | { | |
391 | self | |
392 | } | |
393 | #[cfg(not(target_endian = "big"))] | |
394 | { | |
395 | self.swap_bytes() | |
396 | } | |
397 | } | |
398 | } | |
399 | ||
400 | doc_comment! { | |
401 | concat!("Converts `self` to little endian from the target's endianness. | |
402 | ||
403 | On little endian this is a no-op. On big endian the bytes are swapped. | |
404 | ||
405 | # Examples | |
406 | ||
407 | Basic usage: | |
408 | ||
409 | ``` | |
410 | ", $Feature, "let n = 0x1A", stringify!($SelfT), "; | |
411 | ||
412 | if cfg!(target_endian = \"little\") { | |
413 | assert_eq!(n.to_le(), n) | |
414 | } else { | |
415 | assert_eq!(n.to_le(), n.swap_bytes()) | |
416 | }", | |
417 | $EndFeature, " | |
418 | ```"), | |
419 | #[stable(feature = "rust1", since = "1.0.0")] | |
420 | #[rustc_const_stable(feature = "const_int_conversions", since = "1.32.0")] | |
421 | #[inline] | |
422 | pub const fn to_le(self) -> Self { | |
423 | #[cfg(target_endian = "little")] | |
424 | { | |
425 | self | |
426 | } | |
427 | #[cfg(not(target_endian = "little"))] | |
428 | { | |
429 | self.swap_bytes() | |
430 | } | |
431 | } | |
432 | } | |
433 | ||
434 | doc_comment! { | |
435 | concat!("Checked integer addition. Computes `self + rhs`, returning `None` | |
436 | if overflow occurred. | |
437 | ||
438 | # Examples | |
439 | ||
440 | Basic usage: | |
441 | ||
442 | ``` | |
443 | ", $Feature, "assert_eq!((", stringify!($SelfT), | |
444 | "::MAX - 2).checked_add(1), Some(", stringify!($SelfT), "::MAX - 1)); | |
445 | assert_eq!((", stringify!($SelfT), "::MAX - 2).checked_add(3), None);", | |
446 | $EndFeature, " | |
447 | ```"), | |
448 | #[stable(feature = "rust1", since = "1.0.0")] | |
449 | #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")] | |
450 | #[must_use = "this returns the result of the operation, \ | |
451 | without modifying the original"] | |
452 | #[inline] | |
453 | pub const fn checked_add(self, rhs: Self) -> Option<Self> { | |
454 | let (a, b) = self.overflowing_add(rhs); | |
455 | if unlikely!(b) {None} else {Some(a)} | |
456 | } | |
457 | } | |
458 | ||
459 | doc_comment! { | |
460 | concat!("Unchecked integer addition. Computes `self + rhs`, assuming overflow | |
461 | cannot occur. This results in undefined behavior when `self + rhs > ", stringify!($SelfT), | |
462 | "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`."), | |
463 | #[unstable( | |
464 | feature = "unchecked_math", | |
465 | reason = "niche optimization path", | |
466 | issue = "none", | |
467 | )] | |
468 | #[must_use = "this returns the result of the operation, \ | |
469 | without modifying the original"] | |
470 | #[inline] | |
471 | pub unsafe fn unchecked_add(self, rhs: Self) -> Self { | |
472 | // SAFETY: the caller must uphold the safety contract for | |
473 | // `unchecked_add`. | |
474 | unsafe { intrinsics::unchecked_add(self, rhs) } | |
475 | } | |
476 | } | |
477 | ||
478 | doc_comment! { | |
479 | concat!("Checked integer subtraction. Computes `self - rhs`, returning `None` if | |
480 | overflow occurred. | |
481 | ||
482 | # Examples | |
483 | ||
484 | Basic usage: | |
485 | ||
486 | ``` | |
487 | ", $Feature, "assert_eq!((", stringify!($SelfT), | |
488 | "::MIN + 2).checked_sub(1), Some(", stringify!($SelfT), "::MIN + 1)); | |
489 | assert_eq!((", stringify!($SelfT), "::MIN + 2).checked_sub(3), None);", | |
490 | $EndFeature, " | |
491 | ```"), | |
492 | #[stable(feature = "rust1", since = "1.0.0")] | |
493 | #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")] | |
494 | #[must_use = "this returns the result of the operation, \ | |
495 | without modifying the original"] | |
496 | #[inline] | |
497 | pub const fn checked_sub(self, rhs: Self) -> Option<Self> { | |
498 | let (a, b) = self.overflowing_sub(rhs); | |
499 | if unlikely!(b) {None} else {Some(a)} | |
500 | } | |
501 | } | |
502 | ||
503 | doc_comment! { | |
504 | concat!("Unchecked integer subtraction. Computes `self - rhs`, assuming overflow | |
505 | cannot occur. This results in undefined behavior when `self - rhs > ", stringify!($SelfT), | |
506 | "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`."), | |
507 | #[unstable( | |
508 | feature = "unchecked_math", | |
509 | reason = "niche optimization path", | |
510 | issue = "none", | |
511 | )] | |
512 | #[must_use = "this returns the result of the operation, \ | |
513 | without modifying the original"] | |
514 | #[inline] | |
515 | pub unsafe fn unchecked_sub(self, rhs: Self) -> Self { | |
516 | // SAFETY: the caller must uphold the safety contract for | |
517 | // `unchecked_sub`. | |
518 | unsafe { intrinsics::unchecked_sub(self, rhs) } | |
519 | } | |
520 | } | |
521 | ||
522 | doc_comment! { | |
523 | concat!("Checked integer multiplication. Computes `self * rhs`, returning `None` if | |
524 | overflow occurred. | |
525 | ||
526 | # Examples | |
527 | ||
528 | Basic usage: | |
529 | ||
530 | ``` | |
531 | ", $Feature, "assert_eq!(", stringify!($SelfT), | |
532 | "::MAX.checked_mul(1), Some(", stringify!($SelfT), "::MAX)); | |
533 | assert_eq!(", stringify!($SelfT), "::MAX.checked_mul(2), None);", | |
534 | $EndFeature, " | |
535 | ```"), | |
536 | #[stable(feature = "rust1", since = "1.0.0")] | |
537 | #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")] | |
538 | #[must_use = "this returns the result of the operation, \ | |
539 | without modifying the original"] | |
540 | #[inline] | |
541 | pub const fn checked_mul(self, rhs: Self) -> Option<Self> { | |
542 | let (a, b) = self.overflowing_mul(rhs); | |
543 | if unlikely!(b) {None} else {Some(a)} | |
544 | } | |
545 | } | |
546 | ||
547 | doc_comment! { | |
548 | concat!("Unchecked integer multiplication. Computes `self * rhs`, assuming overflow | |
549 | cannot occur. This results in undefined behavior when `self * rhs > ", stringify!($SelfT), | |
550 | "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`."), | |
551 | #[unstable( | |
552 | feature = "unchecked_math", | |
553 | reason = "niche optimization path", | |
554 | issue = "none", | |
555 | )] | |
556 | #[must_use = "this returns the result of the operation, \ | |
557 | without modifying the original"] | |
558 | #[inline] | |
559 | pub unsafe fn unchecked_mul(self, rhs: Self) -> Self { | |
560 | // SAFETY: the caller must uphold the safety contract for | |
561 | // `unchecked_mul`. | |
562 | unsafe { intrinsics::unchecked_mul(self, rhs) } | |
563 | } | |
564 | } | |
565 | ||
566 | doc_comment! { | |
567 | concat!("Checked integer division. Computes `self / rhs`, returning `None` if `rhs == 0` | |
568 | or the division results in overflow. | |
569 | ||
570 | # Examples | |
571 | ||
572 | Basic usage: | |
573 | ||
574 | ``` | |
575 | ", $Feature, "assert_eq!((", stringify!($SelfT), | |
576 | "::MIN + 1).checked_div(-1), Some(", stringify!($Max), ")); | |
577 | assert_eq!(", stringify!($SelfT), "::MIN.checked_div(-1), None); | |
578 | assert_eq!((1", stringify!($SelfT), ").checked_div(0), None);", | |
579 | $EndFeature, " | |
580 | ```"), | |
581 | #[stable(feature = "rust1", since = "1.0.0")] | |
582 | #[rustc_const_unstable(feature = "const_checked_int_methods", issue = "53718")] | |
583 | #[must_use = "this returns the result of the operation, \ | |
584 | without modifying the original"] | |
585 | #[inline] | |
586 | pub const fn checked_div(self, rhs: Self) -> Option<Self> { | |
587 | if unlikely!(rhs == 0 || (self == Self::MIN && rhs == -1)) { | |
588 | None | |
589 | } else { | |
590 | // SAFETY: div by zero and by INT_MIN have been checked above | |
591 | Some(unsafe { intrinsics::unchecked_div(self, rhs) }) | |
592 | } | |
593 | } | |
594 | } | |
595 | ||
596 | doc_comment! { | |
597 | concat!("Checked Euclidean division. Computes `self.div_euclid(rhs)`, | |
598 | returning `None` if `rhs == 0` or the division results in overflow. | |
599 | ||
600 | # Examples | |
601 | ||
602 | Basic usage: | |
603 | ||
604 | ``` | |
605 | assert_eq!((", stringify!($SelfT), | |
606 | "::MIN + 1).checked_div_euclid(-1), Some(", stringify!($Max), ")); | |
607 | assert_eq!(", stringify!($SelfT), "::MIN.checked_div_euclid(-1), None); | |
608 | assert_eq!((1", stringify!($SelfT), ").checked_div_euclid(0), None); | |
609 | ```"), | |
610 | #[stable(feature = "euclidean_division", since = "1.38.0")] | |
611 | #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")] | |
612 | #[must_use = "this returns the result of the operation, \ | |
613 | without modifying the original"] | |
614 | #[inline] | |
615 | pub const fn checked_div_euclid(self, rhs: Self) -> Option<Self> { | |
616 | if unlikely!(rhs == 0 || (self == Self::MIN && rhs == -1)) { | |
617 | None | |
618 | } else { | |
619 | Some(self.div_euclid(rhs)) | |
620 | } | |
621 | } | |
622 | } | |
623 | ||
624 | doc_comment! { | |
625 | concat!("Checked integer remainder. Computes `self % rhs`, returning `None` if | |
626 | `rhs == 0` or the division results in overflow. | |
627 | ||
628 | # Examples | |
629 | ||
630 | Basic usage: | |
631 | ||
632 | ``` | |
633 | ", $Feature, " | |
634 | assert_eq!(5", stringify!($SelfT), ".checked_rem(2), Some(1)); | |
635 | assert_eq!(5", stringify!($SelfT), ".checked_rem(0), None); | |
636 | assert_eq!(", stringify!($SelfT), "::MIN.checked_rem(-1), None);", | |
637 | $EndFeature, " | |
638 | ```"), | |
639 | #[stable(feature = "wrapping", since = "1.7.0")] | |
640 | #[rustc_const_unstable(feature = "const_checked_int_methods", issue = "53718")] | |
641 | #[must_use = "this returns the result of the operation, \ | |
642 | without modifying the original"] | |
643 | #[inline] | |
644 | pub const fn checked_rem(self, rhs: Self) -> Option<Self> { | |
645 | if unlikely!(rhs == 0 || (self == Self::MIN && rhs == -1)) { | |
646 | None | |
647 | } else { | |
648 | // SAFETY: div by zero and by INT_MIN have been checked above | |
649 | Some(unsafe { intrinsics::unchecked_rem(self, rhs) }) | |
650 | } | |
651 | } | |
652 | } | |
653 | ||
654 | doc_comment! { | |
655 | concat!("Checked Euclidean remainder. Computes `self.rem_euclid(rhs)`, returning `None` | |
656 | if `rhs == 0` or the division results in overflow. | |
657 | ||
658 | # Examples | |
659 | ||
660 | Basic usage: | |
661 | ||
662 | ``` | |
663 | assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(2), Some(1)); | |
664 | assert_eq!(5", stringify!($SelfT), ".checked_rem_euclid(0), None); | |
665 | assert_eq!(", stringify!($SelfT), "::MIN.checked_rem_euclid(-1), None); | |
666 | ```"), | |
667 | #[stable(feature = "euclidean_division", since = "1.38.0")] | |
668 | #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")] | |
669 | #[must_use = "this returns the result of the operation, \ | |
670 | without modifying the original"] | |
671 | #[inline] | |
672 | pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> { | |
673 | if unlikely!(rhs == 0 || (self == Self::MIN && rhs == -1)) { | |
674 | None | |
675 | } else { | |
676 | Some(self.rem_euclid(rhs)) | |
677 | } | |
678 | } | |
679 | } | |
680 | ||
681 | doc_comment! { | |
682 | concat!("Checked negation. Computes `-self`, returning `None` if `self == MIN`. | |
683 | ||
684 | # Examples | |
685 | ||
686 | Basic usage: | |
687 | ||
688 | ``` | |
689 | ", $Feature, " | |
690 | assert_eq!(5", stringify!($SelfT), ".checked_neg(), Some(-5)); | |
691 | assert_eq!(", stringify!($SelfT), "::MIN.checked_neg(), None);", | |
692 | $EndFeature, " | |
693 | ```"), | |
694 | #[stable(feature = "wrapping", since = "1.7.0")] | |
695 | #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")] | |
696 | #[inline] | |
697 | pub const fn checked_neg(self) -> Option<Self> { | |
698 | let (a, b) = self.overflowing_neg(); | |
699 | if unlikely!(b) {None} else {Some(a)} | |
700 | } | |
701 | } | |
702 | ||
703 | doc_comment! { | |
704 | concat!("Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger | |
705 | than or equal to the number of bits in `self`. | |
706 | ||
707 | # Examples | |
708 | ||
709 | Basic usage: | |
710 | ||
711 | ``` | |
712 | ", $Feature, "assert_eq!(0x1", stringify!($SelfT), ".checked_shl(4), Some(0x10)); | |
713 | assert_eq!(0x1", stringify!($SelfT), ".checked_shl(129), None);", | |
714 | $EndFeature, " | |
715 | ```"), | |
716 | #[stable(feature = "wrapping", since = "1.7.0")] | |
717 | #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")] | |
718 | #[must_use = "this returns the result of the operation, \ | |
719 | without modifying the original"] | |
720 | #[inline] | |
721 | pub const fn checked_shl(self, rhs: u32) -> Option<Self> { | |
722 | let (a, b) = self.overflowing_shl(rhs); | |
723 | if unlikely!(b) {None} else {Some(a)} | |
724 | } | |
725 | } | |
726 | ||
727 | doc_comment! { | |
728 | concat!("Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is | |
729 | larger than or equal to the number of bits in `self`. | |
730 | ||
731 | # Examples | |
732 | ||
733 | Basic usage: | |
734 | ||
735 | ``` | |
736 | ", $Feature, "assert_eq!(0x10", stringify!($SelfT), ".checked_shr(4), Some(0x1)); | |
737 | assert_eq!(0x10", stringify!($SelfT), ".checked_shr(128), None);", | |
738 | $EndFeature, " | |
739 | ```"), | |
740 | #[stable(feature = "wrapping", since = "1.7.0")] | |
741 | #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")] | |
742 | #[must_use = "this returns the result of the operation, \ | |
743 | without modifying the original"] | |
744 | #[inline] | |
745 | pub const fn checked_shr(self, rhs: u32) -> Option<Self> { | |
746 | let (a, b) = self.overflowing_shr(rhs); | |
747 | if unlikely!(b) {None} else {Some(a)} | |
748 | } | |
749 | } | |
750 | ||
751 | doc_comment! { | |
752 | concat!("Checked absolute value. Computes `self.abs()`, returning `None` if | |
753 | `self == MIN`. | |
754 | ||
755 | # Examples | |
756 | ||
757 | Basic usage: | |
758 | ||
759 | ``` | |
760 | ", $Feature, " | |
761 | assert_eq!((-5", stringify!($SelfT), ").checked_abs(), Some(5)); | |
762 | assert_eq!(", stringify!($SelfT), "::MIN.checked_abs(), None);", | |
763 | $EndFeature, " | |
764 | ```"), | |
765 | #[stable(feature = "no_panic_abs", since = "1.13.0")] | |
766 | #[rustc_const_stable(feature = "const_checked_int_methods", since = "1.47.0")] | |
767 | #[inline] | |
768 | pub const fn checked_abs(self) -> Option<Self> { | |
769 | if self.is_negative() { | |
770 | self.checked_neg() | |
771 | } else { | |
772 | Some(self) | |
773 | } | |
774 | } | |
775 | } | |
776 | ||
777 | doc_comment! { | |
778 | concat!("Checked exponentiation. Computes `self.pow(exp)`, returning `None` if | |
779 | overflow occurred. | |
780 | ||
781 | # Examples | |
782 | ||
783 | Basic usage: | |
784 | ||
785 | ``` | |
786 | ", $Feature, "assert_eq!(8", stringify!($SelfT), ".checked_pow(2), Some(64)); | |
787 | assert_eq!(", stringify!($SelfT), "::MAX.checked_pow(2), None);", | |
788 | $EndFeature, " | |
789 | ```"), | |
790 | ||
791 | #[stable(feature = "no_panic_pow", since = "1.34.0")] | |
792 | #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")] | |
793 | #[must_use = "this returns the result of the operation, \ | |
794 | without modifying the original"] | |
795 | #[inline] | |
796 | pub const fn checked_pow(self, mut exp: u32) -> Option<Self> { | |
797 | if exp == 0 { | |
798 | return Some(1); | |
799 | } | |
800 | let mut base = self; | |
801 | let mut acc: Self = 1; | |
802 | ||
803 | while exp > 1 { | |
804 | if (exp & 1) == 1 { | |
805 | acc = try_opt!(acc.checked_mul(base)); | |
806 | } | |
807 | exp /= 2; | |
808 | base = try_opt!(base.checked_mul(base)); | |
809 | } | |
810 | // since exp!=0, finally the exp must be 1. | |
811 | // Deal with the final bit of the exponent separately, since | |
812 | // squaring the base afterwards is not necessary and may cause a | |
813 | // needless overflow. | |
814 | Some(try_opt!(acc.checked_mul(base))) | |
815 | } | |
816 | } | |
817 | ||
818 | doc_comment! { | |
819 | concat!("Saturating integer addition. Computes `self + rhs`, saturating at the numeric | |
820 | bounds instead of overflowing. | |
821 | ||
822 | # Examples | |
823 | ||
824 | Basic usage: | |
825 | ||
826 | ``` | |
827 | ", $Feature, "assert_eq!(100", stringify!($SelfT), ".saturating_add(1), 101); | |
828 | assert_eq!(", stringify!($SelfT), "::MAX.saturating_add(100), ", stringify!($SelfT), | |
829 | "::MAX); | |
830 | assert_eq!(", stringify!($SelfT), "::MIN.saturating_add(-1), ", stringify!($SelfT), | |
831 | "::MIN);", | |
832 | $EndFeature, " | |
833 | ```"), | |
834 | ||
835 | #[stable(feature = "rust1", since = "1.0.0")] | |
836 | #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")] | |
837 | #[must_use = "this returns the result of the operation, \ | |
838 | without modifying the original"] | |
839 | #[inline] | |
840 | pub const fn saturating_add(self, rhs: Self) -> Self { | |
841 | intrinsics::saturating_add(self, rhs) | |
842 | } | |
843 | } | |
844 | ||
845 | doc_comment! { | |
846 | concat!("Saturating integer subtraction. Computes `self - rhs`, saturating at the | |
847 | numeric bounds instead of overflowing. | |
848 | ||
849 | # Examples | |
850 | ||
851 | Basic usage: | |
852 | ||
853 | ``` | |
854 | ", $Feature, "assert_eq!(100", stringify!($SelfT), ".saturating_sub(127), -27); | |
855 | assert_eq!(", stringify!($SelfT), "::MIN.saturating_sub(100), ", stringify!($SelfT), | |
856 | "::MIN); | |
857 | assert_eq!(", stringify!($SelfT), "::MAX.saturating_sub(-1), ", stringify!($SelfT), | |
858 | "::MAX);", | |
859 | $EndFeature, " | |
860 | ```"), | |
861 | #[stable(feature = "rust1", since = "1.0.0")] | |
862 | #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")] | |
863 | #[must_use = "this returns the result of the operation, \ | |
864 | without modifying the original"] | |
865 | #[inline] | |
866 | pub const fn saturating_sub(self, rhs: Self) -> Self { | |
867 | intrinsics::saturating_sub(self, rhs) | |
868 | } | |
869 | } | |
870 | ||
871 | doc_comment! { | |
872 | concat!("Saturating integer negation. Computes `-self`, returning `MAX` if `self == MIN` | |
873 | instead of overflowing. | |
874 | ||
875 | # Examples | |
876 | ||
877 | Basic usage: | |
878 | ||
879 | ``` | |
880 | ", $Feature, "assert_eq!(100", stringify!($SelfT), ".saturating_neg(), -100); | |
881 | assert_eq!((-100", stringify!($SelfT), ").saturating_neg(), 100); | |
882 | assert_eq!(", stringify!($SelfT), "::MIN.saturating_neg(), ", stringify!($SelfT), | |
883 | "::MAX); | |
884 | assert_eq!(", stringify!($SelfT), "::MAX.saturating_neg(), ", stringify!($SelfT), | |
885 | "::MIN + 1);", | |
886 | $EndFeature, " | |
887 | ```"), | |
888 | ||
889 | #[stable(feature = "saturating_neg", since = "1.45.0")] | |
890 | #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")] | |
891 | #[inline] | |
892 | pub const fn saturating_neg(self) -> Self { | |
893 | intrinsics::saturating_sub(0, self) | |
894 | } | |
895 | } | |
896 | ||
897 | doc_comment! { | |
898 | concat!("Saturating absolute value. Computes `self.abs()`, returning `MAX` if `self == | |
899 | MIN` instead of overflowing. | |
900 | ||
901 | # Examples | |
902 | ||
903 | Basic usage: | |
904 | ||
905 | ``` | |
906 | ", $Feature, "assert_eq!(100", stringify!($SelfT), ".saturating_abs(), 100); | |
907 | assert_eq!((-100", stringify!($SelfT), ").saturating_abs(), 100); | |
908 | assert_eq!(", stringify!($SelfT), "::MIN.saturating_abs(), ", stringify!($SelfT), | |
909 | "::MAX); | |
910 | assert_eq!((", stringify!($SelfT), "::MIN + 1).saturating_abs(), ", stringify!($SelfT), | |
911 | "::MAX);", | |
912 | $EndFeature, " | |
913 | ```"), | |
914 | ||
915 | #[stable(feature = "saturating_neg", since = "1.45.0")] | |
916 | #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")] | |
917 | #[inline] | |
918 | pub const fn saturating_abs(self) -> Self { | |
919 | if self.is_negative() { | |
920 | self.saturating_neg() | |
921 | } else { | |
922 | self | |
923 | } | |
924 | } | |
925 | } | |
926 | ||
927 | doc_comment! { | |
928 | concat!("Saturating integer multiplication. Computes `self * rhs`, saturating at the | |
929 | numeric bounds instead of overflowing. | |
930 | ||
931 | # Examples | |
932 | ||
933 | Basic usage: | |
934 | ||
935 | ``` | |
936 | ", $Feature, " | |
937 | assert_eq!(10", stringify!($SelfT), ".saturating_mul(12), 120); | |
938 | assert_eq!(", stringify!($SelfT), "::MAX.saturating_mul(10), ", stringify!($SelfT), "::MAX); | |
939 | assert_eq!(", stringify!($SelfT), "::MIN.saturating_mul(10), ", stringify!($SelfT), "::MIN);", | |
940 | $EndFeature, " | |
941 | ```"), | |
942 | #[stable(feature = "wrapping", since = "1.7.0")] | |
943 | #[rustc_const_stable(feature = "const_saturating_int_methods", since = "1.47.0")] | |
944 | #[must_use = "this returns the result of the operation, \ | |
945 | without modifying the original"] | |
946 | #[inline] | |
947 | pub const fn saturating_mul(self, rhs: Self) -> Self { | |
948 | match self.checked_mul(rhs) { | |
949 | Some(x) => x, | |
950 | None => if (self < 0) == (rhs < 0) { | |
951 | Self::MAX | |
952 | } else { | |
953 | Self::MIN | |
954 | } | |
955 | } | |
956 | } | |
957 | } | |
958 | ||
959 | doc_comment! { | |
960 | concat!("Saturating integer exponentiation. Computes `self.pow(exp)`, | |
961 | saturating at the numeric bounds instead of overflowing. | |
962 | ||
963 | # Examples | |
964 | ||
965 | Basic usage: | |
966 | ||
967 | ``` | |
968 | ", $Feature, " | |
969 | assert_eq!((-4", stringify!($SelfT), ").saturating_pow(3), -64); | |
970 | assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(2), ", stringify!($SelfT), "::MAX); | |
971 | assert_eq!(", stringify!($SelfT), "::MIN.saturating_pow(3), ", stringify!($SelfT), "::MIN);", | |
972 | $EndFeature, " | |
973 | ```"), | |
974 | #[stable(feature = "no_panic_pow", since = "1.34.0")] | |
975 | #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")] | |
976 | #[must_use = "this returns the result of the operation, \ | |
977 | without modifying the original"] | |
978 | #[inline] | |
979 | pub const fn saturating_pow(self, exp: u32) -> Self { | |
980 | match self.checked_pow(exp) { | |
981 | Some(x) => x, | |
982 | None if self < 0 && exp % 2 == 1 => Self::MIN, | |
983 | None => Self::MAX, | |
984 | } | |
985 | } | |
986 | } | |
987 | ||
988 | doc_comment! { | |
989 | concat!("Wrapping (modular) addition. Computes `self + rhs`, wrapping around at the | |
990 | boundary of the type. | |
991 | ||
992 | # Examples | |
993 | ||
994 | Basic usage: | |
995 | ||
996 | ``` | |
997 | ", $Feature, "assert_eq!(100", stringify!($SelfT), ".wrapping_add(27), 127); | |
998 | assert_eq!(", stringify!($SelfT), "::MAX.wrapping_add(2), ", stringify!($SelfT), | |
999 | "::MIN + 1);", | |
1000 | $EndFeature, " | |
1001 | ```"), | |
1002 | #[stable(feature = "rust1", since = "1.0.0")] | |
1003 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1004 | #[must_use = "this returns the result of the operation, \ | |
1005 | without modifying the original"] | |
1006 | #[inline] | |
1007 | pub const fn wrapping_add(self, rhs: Self) -> Self { | |
1008 | intrinsics::wrapping_add(self, rhs) | |
1009 | } | |
1010 | } | |
1011 | ||
1012 | doc_comment! { | |
1013 | concat!("Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around at the | |
1014 | boundary of the type. | |
1015 | ||
1016 | # Examples | |
1017 | ||
1018 | Basic usage: | |
1019 | ||
1020 | ``` | |
1021 | ", $Feature, "assert_eq!(0", stringify!($SelfT), ".wrapping_sub(127), -127); | |
1022 | assert_eq!((-2", stringify!($SelfT), ").wrapping_sub(", stringify!($SelfT), "::MAX), ", | |
1023 | stringify!($SelfT), "::MAX);", | |
1024 | $EndFeature, " | |
1025 | ```"), | |
1026 | #[stable(feature = "rust1", since = "1.0.0")] | |
1027 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1028 | #[must_use = "this returns the result of the operation, \ | |
1029 | without modifying the original"] | |
1030 | #[inline] | |
1031 | pub const fn wrapping_sub(self, rhs: Self) -> Self { | |
1032 | intrinsics::wrapping_sub(self, rhs) | |
1033 | } | |
1034 | } | |
1035 | ||
1036 | doc_comment! { | |
1037 | concat!("Wrapping (modular) multiplication. Computes `self * rhs`, wrapping around at | |
1038 | the boundary of the type. | |
1039 | ||
1040 | # Examples | |
1041 | ||
1042 | Basic usage: | |
1043 | ||
1044 | ``` | |
1045 | ", $Feature, "assert_eq!(10", stringify!($SelfT), ".wrapping_mul(12), 120); | |
1046 | assert_eq!(11i8.wrapping_mul(12), -124);", | |
1047 | $EndFeature, " | |
1048 | ```"), | |
1049 | #[stable(feature = "rust1", since = "1.0.0")] | |
1050 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1051 | #[must_use = "this returns the result of the operation, \ | |
1052 | without modifying the original"] | |
1053 | #[inline] | |
1054 | pub const fn wrapping_mul(self, rhs: Self) -> Self { | |
1055 | intrinsics::wrapping_mul(self, rhs) | |
1056 | } | |
1057 | } | |
1058 | ||
1059 | doc_comment! { | |
1060 | concat!("Wrapping (modular) division. Computes `self / rhs`, wrapping around at the | |
1061 | boundary of the type. | |
1062 | ||
1063 | The only case where such wrapping can occur is when one divides `MIN / -1` on a signed type (where | |
1064 | `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value | |
1065 | that is too large to represent in the type. In such a case, this function returns `MIN` itself. | |
1066 | ||
1067 | # Panics | |
1068 | ||
1069 | This function will panic if `rhs` is 0. | |
1070 | ||
1071 | # Examples | |
1072 | ||
1073 | Basic usage: | |
1074 | ||
1075 | ``` | |
1076 | ", $Feature, "assert_eq!(100", stringify!($SelfT), ".wrapping_div(10), 10); | |
1077 | assert_eq!((-128i8).wrapping_div(-1), -128);", | |
1078 | $EndFeature, " | |
1079 | ```"), | |
1080 | #[stable(feature = "num_wrapping", since = "1.2.0")] | |
1081 | #[rustc_const_unstable(feature = "const_wrapping_int_methods", issue = "53718")] | |
1082 | #[must_use = "this returns the result of the operation, \ | |
1083 | without modifying the original"] | |
1084 | #[inline] | |
1085 | pub const fn wrapping_div(self, rhs: Self) -> Self { | |
1086 | self.overflowing_div(rhs).0 | |
1087 | } | |
1088 | } | |
1089 | ||
1090 | doc_comment! { | |
1091 | concat!("Wrapping Euclidean division. Computes `self.div_euclid(rhs)`, | |
1092 | wrapping around at the boundary of the type. | |
1093 | ||
1094 | Wrapping will only occur in `MIN / -1` on a signed type (where `MIN` is the negative minimal value | |
1095 | for the type). This is equivalent to `-MIN`, a positive value that is too large to represent in the | |
1096 | type. In this case, this method returns `MIN` itself. | |
1097 | ||
1098 | # Panics | |
1099 | ||
1100 | This function will panic if `rhs` is 0. | |
1101 | ||
1102 | # Examples | |
1103 | ||
1104 | Basic usage: | |
1105 | ||
1106 | ``` | |
1107 | assert_eq!(100", stringify!($SelfT), ".wrapping_div_euclid(10), 10); | |
1108 | assert_eq!((-128i8).wrapping_div_euclid(-1), -128); | |
1109 | ```"), | |
1110 | #[stable(feature = "euclidean_division", since = "1.38.0")] | |
1111 | #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")] | |
1112 | #[must_use = "this returns the result of the operation, \ | |
1113 | without modifying the original"] | |
1114 | #[inline] | |
1115 | pub const fn wrapping_div_euclid(self, rhs: Self) -> Self { | |
1116 | self.overflowing_div_euclid(rhs).0 | |
1117 | } | |
1118 | } | |
1119 | ||
1120 | doc_comment! { | |
1121 | concat!("Wrapping (modular) remainder. Computes `self % rhs`, wrapping around at the | |
1122 | boundary of the type. | |
1123 | ||
1124 | Such wrap-around never actually occurs mathematically; implementation artifacts make `x % y` | |
1125 | invalid for `MIN / -1` on a signed type (where `MIN` is the negative minimal value). In such a case, | |
1126 | this function returns `0`. | |
1127 | ||
1128 | # Panics | |
1129 | ||
1130 | This function will panic if `rhs` is 0. | |
1131 | ||
1132 | # Examples | |
1133 | ||
1134 | Basic usage: | |
1135 | ||
1136 | ``` | |
1137 | ", $Feature, "assert_eq!(100", stringify!($SelfT), ".wrapping_rem(10), 0); | |
1138 | assert_eq!((-128i8).wrapping_rem(-1), 0);", | |
1139 | $EndFeature, " | |
1140 | ```"), | |
1141 | #[stable(feature = "num_wrapping", since = "1.2.0")] | |
1142 | #[rustc_const_unstable(feature = "const_wrapping_int_methods", issue = "53718")] | |
1143 | #[must_use = "this returns the result of the operation, \ | |
1144 | without modifying the original"] | |
1145 | #[inline] | |
1146 | pub const fn wrapping_rem(self, rhs: Self) -> Self { | |
1147 | self.overflowing_rem(rhs).0 | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | doc_comment! { | |
1152 | concat!("Wrapping Euclidean remainder. Computes `self.rem_euclid(rhs)`, wrapping around | |
1153 | at the boundary of the type. | |
1154 | ||
1155 | Wrapping will only occur in `MIN % -1` on a signed type (where `MIN` is the negative minimal value | |
1156 | for the type). In this case, this method returns 0. | |
1157 | ||
1158 | # Panics | |
1159 | ||
1160 | This function will panic if `rhs` is 0. | |
1161 | ||
1162 | # Examples | |
1163 | ||
1164 | Basic usage: | |
1165 | ||
1166 | ``` | |
1167 | assert_eq!(100", stringify!($SelfT), ".wrapping_rem_euclid(10), 0); | |
1168 | assert_eq!((-128i8).wrapping_rem_euclid(-1), 0); | |
1169 | ```"), | |
1170 | #[stable(feature = "euclidean_division", since = "1.38.0")] | |
1171 | #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")] | |
1172 | #[must_use = "this returns the result of the operation, \ | |
1173 | without modifying the original"] | |
1174 | #[inline] | |
1175 | pub const fn wrapping_rem_euclid(self, rhs: Self) -> Self { | |
1176 | self.overflowing_rem_euclid(rhs).0 | |
1177 | } | |
1178 | } | |
1179 | ||
1180 | doc_comment! { | |
1181 | concat!("Wrapping (modular) negation. Computes `-self`, wrapping around at the boundary | |
1182 | of the type. | |
1183 | ||
1184 | The only case where such wrapping can occur is when one negates `MIN` on a signed type (where `MIN` | |
1185 | is the negative minimal value for the type); this is a positive value that is too large to represent | |
1186 | in the type. In such a case, this function returns `MIN` itself. | |
1187 | ||
1188 | # Examples | |
1189 | ||
1190 | Basic usage: | |
1191 | ||
1192 | ``` | |
1193 | ", $Feature, "assert_eq!(100", stringify!($SelfT), ".wrapping_neg(), -100); | |
1194 | assert_eq!(", stringify!($SelfT), "::MIN.wrapping_neg(), ", stringify!($SelfT), | |
1195 | "::MIN);", | |
1196 | $EndFeature, " | |
1197 | ```"), | |
1198 | #[stable(feature = "num_wrapping", since = "1.2.0")] | |
1199 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1200 | #[inline] | |
1201 | pub const fn wrapping_neg(self) -> Self { | |
1202 | self.overflowing_neg().0 | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | doc_comment! { | |
1207 | concat!("Panic-free bitwise shift-left; yields `self << mask(rhs)`, where `mask` removes | |
1208 | any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type. | |
1209 | ||
1210 | Note that this is *not* the same as a rotate-left; the RHS of a wrapping shift-left is restricted to | |
1211 | the range of the type, rather than the bits shifted out of the LHS being returned to the other end. | |
1212 | The primitive integer types all implement a `[`rotate_left`](#method.rotate_left) function, | |
1213 | which may be what you want instead. | |
1214 | ||
1215 | # Examples | |
1216 | ||
1217 | Basic usage: | |
1218 | ||
1219 | ``` | |
1220 | ", $Feature, "assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(7), -128); | |
1221 | assert_eq!((-1", stringify!($SelfT), ").wrapping_shl(128), -1);", | |
1222 | $EndFeature, " | |
1223 | ```"), | |
1224 | #[stable(feature = "num_wrapping", since = "1.2.0")] | |
1225 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1226 | #[must_use = "this returns the result of the operation, \ | |
1227 | without modifying the original"] | |
1228 | #[inline] | |
1229 | pub const fn wrapping_shl(self, rhs: u32) -> Self { | |
1230 | // SAFETY: the masking by the bitsize of the type ensures that we do not shift | |
1231 | // out of bounds | |
1232 | unsafe { | |
1233 | intrinsics::unchecked_shl(self, (rhs & ($BITS - 1)) as $SelfT) | |
1234 | } | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | doc_comment! { | |
1239 | concat!("Panic-free bitwise shift-right; yields `self >> mask(rhs)`, where `mask` | |
1240 | removes any high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type. | |
1241 | ||
1242 | Note that this is *not* the same as a rotate-right; the RHS of a wrapping shift-right is restricted | |
1243 | to the range of the type, rather than the bits shifted out of the LHS being returned to the other | |
1244 | end. The primitive integer types all implement a [`rotate_right`](#method.rotate_right) function, | |
1245 | which may be what you want instead. | |
1246 | ||
1247 | # Examples | |
1248 | ||
1249 | Basic usage: | |
1250 | ||
1251 | ``` | |
1252 | ", $Feature, "assert_eq!((-128", stringify!($SelfT), ").wrapping_shr(7), -1); | |
1253 | assert_eq!((-128i16).wrapping_shr(64), -128);", | |
1254 | $EndFeature, " | |
1255 | ```"), | |
1256 | #[stable(feature = "num_wrapping", since = "1.2.0")] | |
1257 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1258 | #[must_use = "this returns the result of the operation, \ | |
1259 | without modifying the original"] | |
1260 | #[inline] | |
1261 | pub const fn wrapping_shr(self, rhs: u32) -> Self { | |
1262 | // SAFETY: the masking by the bitsize of the type ensures that we do not shift | |
1263 | // out of bounds | |
1264 | unsafe { | |
1265 | intrinsics::unchecked_shr(self, (rhs & ($BITS - 1)) as $SelfT) | |
1266 | } | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | doc_comment! { | |
1271 | concat!("Wrapping (modular) absolute value. Computes `self.abs()`, wrapping around at | |
1272 | the boundary of the type. | |
1273 | ||
1274 | The only case where such wrapping can occur is when one takes the absolute value of the negative | |
1275 | minimal value for the type; this is a positive value that is too large to represent in the type. In | |
1276 | such a case, this function returns `MIN` itself. | |
1277 | ||
1278 | # Examples | |
1279 | ||
1280 | Basic usage: | |
1281 | ||
1282 | ``` | |
1283 | ", $Feature, "assert_eq!(100", stringify!($SelfT), ".wrapping_abs(), 100); | |
1284 | assert_eq!((-100", stringify!($SelfT), ").wrapping_abs(), 100); | |
1285 | assert_eq!(", stringify!($SelfT), "::MIN.wrapping_abs(), ", stringify!($SelfT), | |
1286 | "::MIN); | |
1287 | assert_eq!((-128i8).wrapping_abs() as u8, 128);", | |
1288 | $EndFeature, " | |
1289 | ```"), | |
1290 | #[stable(feature = "no_panic_abs", since = "1.13.0")] | |
1291 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1292 | #[allow(unused_attributes)] | |
1293 | #[inline] | |
1294 | pub const fn wrapping_abs(self) -> Self { | |
1295 | if self.is_negative() { | |
1296 | self.wrapping_neg() | |
1297 | } else { | |
1298 | self | |
1299 | } | |
1300 | } | |
1301 | } | |
1302 | ||
1303 | doc_comment! { | |
1304 | concat!("Computes the absolute value of `self` without any wrapping | |
1305 | or panicking. | |
1306 | ||
1307 | ||
1308 | # Examples | |
1309 | ||
1310 | Basic usage: | |
1311 | ||
1312 | ``` | |
1313 | ", $Feature, "#![feature(unsigned_abs)] | |
1314 | assert_eq!(100", stringify!($SelfT), ".unsigned_abs(), 100", stringify!($UnsignedT), "); | |
1315 | assert_eq!((-100", stringify!($SelfT), ").unsigned_abs(), 100", stringify!($UnsignedT), "); | |
1316 | assert_eq!((-128i8).unsigned_abs(), 128u8);", | |
1317 | $EndFeature, " | |
1318 | ```"), | |
1319 | #[unstable(feature = "unsigned_abs", issue = "74913")] | |
1320 | #[inline] | |
1321 | pub const fn unsigned_abs(self) -> $UnsignedT { | |
1322 | self.wrapping_abs() as $UnsignedT | |
1323 | } | |
1324 | } | |
1325 | ||
1326 | doc_comment! { | |
1327 | concat!("Wrapping (modular) exponentiation. Computes `self.pow(exp)`, | |
1328 | wrapping around at the boundary of the type. | |
1329 | ||
1330 | # Examples | |
1331 | ||
1332 | Basic usage: | |
1333 | ||
1334 | ``` | |
1335 | ", $Feature, "assert_eq!(3", stringify!($SelfT), ".wrapping_pow(4), 81); | |
1336 | assert_eq!(3i8.wrapping_pow(5), -13); | |
1337 | assert_eq!(3i8.wrapping_pow(6), -39);", | |
1338 | $EndFeature, " | |
1339 | ```"), | |
1340 | #[stable(feature = "no_panic_pow", since = "1.34.0")] | |
1341 | #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")] | |
1342 | #[must_use = "this returns the result of the operation, \ | |
1343 | without modifying the original"] | |
1344 | #[inline] | |
1345 | pub const fn wrapping_pow(self, mut exp: u32) -> Self { | |
1346 | if exp == 0 { | |
1347 | return 1; | |
1348 | } | |
1349 | let mut base = self; | |
1350 | let mut acc: Self = 1; | |
1351 | ||
1352 | while exp > 1 { | |
1353 | if (exp & 1) == 1 { | |
1354 | acc = acc.wrapping_mul(base); | |
1355 | } | |
1356 | exp /= 2; | |
1357 | base = base.wrapping_mul(base); | |
1358 | } | |
1359 | ||
1360 | // since exp!=0, finally the exp must be 1. | |
1361 | // Deal with the final bit of the exponent separately, since | |
1362 | // squaring the base afterwards is not necessary and may cause a | |
1363 | // needless overflow. | |
1364 | acc.wrapping_mul(base) | |
1365 | } | |
1366 | } | |
1367 | ||
1368 | doc_comment! { | |
1369 | concat!("Calculates `self` + `rhs` | |
1370 | ||
1371 | Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would | |
1372 | occur. If an overflow would have occurred then the wrapped value is returned. | |
1373 | ||
1374 | # Examples | |
1375 | ||
1376 | Basic usage: | |
1377 | ||
1378 | ``` | |
1379 | ", $Feature, " | |
1380 | assert_eq!(5", stringify!($SelfT), ".overflowing_add(2), (7, false)); | |
1381 | assert_eq!(", stringify!($SelfT), "::MAX.overflowing_add(1), (", stringify!($SelfT), | |
1382 | "::MIN, true));", $EndFeature, " | |
1383 | ```"), | |
1384 | #[stable(feature = "wrapping", since = "1.7.0")] | |
1385 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1386 | #[must_use = "this returns the result of the operation, \ | |
1387 | without modifying the original"] | |
1388 | #[inline] | |
1389 | pub const fn overflowing_add(self, rhs: Self) -> (Self, bool) { | |
1390 | let (a, b) = intrinsics::add_with_overflow(self as $ActualT, rhs as $ActualT); | |
1391 | (a as Self, b) | |
1392 | } | |
1393 | } | |
1394 | ||
1395 | doc_comment! { | |
1396 | concat!("Calculates `self` - `rhs` | |
1397 | ||
1398 | Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow | |
1399 | would occur. If an overflow would have occurred then the wrapped value is returned. | |
1400 | ||
1401 | # Examples | |
1402 | ||
1403 | Basic usage: | |
1404 | ||
1405 | ``` | |
1406 | ", $Feature, " | |
1407 | assert_eq!(5", stringify!($SelfT), ".overflowing_sub(2), (3, false)); | |
1408 | assert_eq!(", stringify!($SelfT), "::MIN.overflowing_sub(1), (", stringify!($SelfT), | |
1409 | "::MAX, true));", $EndFeature, " | |
1410 | ```"), | |
1411 | #[stable(feature = "wrapping", since = "1.7.0")] | |
1412 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1413 | #[must_use = "this returns the result of the operation, \ | |
1414 | without modifying the original"] | |
1415 | #[inline] | |
1416 | pub const fn overflowing_sub(self, rhs: Self) -> (Self, bool) { | |
1417 | let (a, b) = intrinsics::sub_with_overflow(self as $ActualT, rhs as $ActualT); | |
1418 | (a as Self, b) | |
1419 | } | |
1420 | } | |
1421 | ||
1422 | doc_comment! { | |
1423 | concat!("Calculates the multiplication of `self` and `rhs`. | |
1424 | ||
1425 | Returns a tuple of the multiplication along with a boolean indicating whether an arithmetic overflow | |
1426 | would occur. If an overflow would have occurred then the wrapped value is returned. | |
1427 | ||
1428 | # Examples | |
1429 | ||
1430 | Basic usage: | |
1431 | ||
1432 | ``` | |
1433 | ", $Feature, "assert_eq!(5", stringify!($SelfT), ".overflowing_mul(2), (10, false)); | |
1434 | assert_eq!(1_000_000_000i32.overflowing_mul(10), (1410065408, true));", | |
1435 | $EndFeature, " | |
1436 | ```"), | |
1437 | #[stable(feature = "wrapping", since = "1.7.0")] | |
1438 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1439 | #[must_use = "this returns the result of the operation, \ | |
1440 | without modifying the original"] | |
1441 | #[inline] | |
1442 | pub const fn overflowing_mul(self, rhs: Self) -> (Self, bool) { | |
1443 | let (a, b) = intrinsics::mul_with_overflow(self as $ActualT, rhs as $ActualT); | |
1444 | (a as Self, b) | |
1445 | } | |
1446 | } | |
1447 | ||
1448 | doc_comment! { | |
1449 | concat!("Calculates the divisor when `self` is divided by `rhs`. | |
1450 | ||
1451 | Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would | |
1452 | occur. If an overflow would occur then self is returned. | |
1453 | ||
1454 | # Panics | |
1455 | ||
1456 | This function will panic if `rhs` is 0. | |
1457 | ||
1458 | # Examples | |
1459 | ||
1460 | Basic usage: | |
1461 | ||
1462 | ``` | |
1463 | ", $Feature, " | |
1464 | assert_eq!(5", stringify!($SelfT), ".overflowing_div(2), (2, false)); | |
1465 | assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div(-1), (", stringify!($SelfT), | |
1466 | "::MIN, true));", | |
1467 | $EndFeature, " | |
1468 | ```"), | |
1469 | #[inline] | |
1470 | #[stable(feature = "wrapping", since = "1.7.0")] | |
1471 | #[rustc_const_unstable(feature = "const_overflowing_int_methods", issue = "53718")] | |
1472 | #[must_use = "this returns the result of the operation, \ | |
1473 | without modifying the original"] | |
1474 | pub const fn overflowing_div(self, rhs: Self) -> (Self, bool) { | |
1475 | if unlikely!(self == Self::MIN && rhs == -1) { | |
1476 | (self, true) | |
1477 | } else { | |
1478 | (self / rhs, false) | |
1479 | } | |
1480 | } | |
1481 | } | |
1482 | ||
1483 | doc_comment! { | |
1484 | concat!("Calculates the quotient of Euclidean division `self.div_euclid(rhs)`. | |
1485 | ||
1486 | Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would | |
1487 | occur. If an overflow would occur then `self` is returned. | |
1488 | ||
1489 | # Panics | |
1490 | ||
1491 | This function will panic if `rhs` is 0. | |
1492 | ||
1493 | # Examples | |
1494 | ||
1495 | Basic usage: | |
1496 | ||
1497 | ``` | |
1498 | assert_eq!(5", stringify!($SelfT), ".overflowing_div_euclid(2), (2, false)); | |
1499 | assert_eq!(", stringify!($SelfT), "::MIN.overflowing_div_euclid(-1), (", stringify!($SelfT), | |
1500 | "::MIN, true)); | |
1501 | ```"), | |
1502 | #[inline] | |
1503 | #[stable(feature = "euclidean_division", since = "1.38.0")] | |
1504 | #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")] | |
1505 | #[must_use = "this returns the result of the operation, \ | |
1506 | without modifying the original"] | |
1507 | pub const fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) { | |
1508 | if unlikely!(self == Self::MIN && rhs == -1) { | |
1509 | (self, true) | |
1510 | } else { | |
1511 | (self.div_euclid(rhs), false) | |
1512 | } | |
1513 | } | |
1514 | } | |
1515 | ||
1516 | doc_comment! { | |
1517 | concat!("Calculates the remainder when `self` is divided by `rhs`. | |
1518 | ||
1519 | Returns a tuple of the remainder after dividing along with a boolean indicating whether an | |
1520 | arithmetic overflow would occur. If an overflow would occur then 0 is returned. | |
1521 | ||
1522 | # Panics | |
1523 | ||
1524 | This function will panic if `rhs` is 0. | |
1525 | ||
1526 | # Examples | |
1527 | ||
1528 | Basic usage: | |
1529 | ||
1530 | ``` | |
1531 | ", $Feature, " | |
1532 | assert_eq!(5", stringify!($SelfT), ".overflowing_rem(2), (1, false)); | |
1533 | assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem(-1), (0, true));", | |
1534 | $EndFeature, " | |
1535 | ```"), | |
1536 | #[inline] | |
1537 | #[stable(feature = "wrapping", since = "1.7.0")] | |
1538 | #[rustc_const_unstable(feature = "const_overflowing_int_methods", issue = "53718")] | |
1539 | #[must_use = "this returns the result of the operation, \ | |
1540 | without modifying the original"] | |
1541 | pub const fn overflowing_rem(self, rhs: Self) -> (Self, bool) { | |
1542 | if unlikely!(self == Self::MIN && rhs == -1) { | |
1543 | (0, true) | |
1544 | } else { | |
1545 | (self % rhs, false) | |
1546 | } | |
1547 | } | |
1548 | } | |
1549 | ||
1550 | ||
1551 | doc_comment! { | |
1552 | concat!("Overflowing Euclidean remainder. Calculates `self.rem_euclid(rhs)`. | |
1553 | ||
1554 | Returns a tuple of the remainder after dividing along with a boolean indicating whether an | |
1555 | arithmetic overflow would occur. If an overflow would occur then 0 is returned. | |
1556 | ||
1557 | # Panics | |
1558 | ||
1559 | This function will panic if `rhs` is 0. | |
1560 | ||
1561 | # Examples | |
1562 | ||
1563 | Basic usage: | |
1564 | ||
1565 | ``` | |
1566 | assert_eq!(5", stringify!($SelfT), ".overflowing_rem_euclid(2), (1, false)); | |
1567 | assert_eq!(", stringify!($SelfT), "::MIN.overflowing_rem_euclid(-1), (0, true)); | |
1568 | ```"), | |
1569 | #[stable(feature = "euclidean_division", since = "1.38.0")] | |
1570 | #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")] | |
1571 | #[must_use = "this returns the result of the operation, \ | |
1572 | without modifying the original"] | |
1573 | #[inline] | |
1574 | pub const fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) { | |
1575 | if unlikely!(self == Self::MIN && rhs == -1) { | |
1576 | (0, true) | |
1577 | } else { | |
1578 | (self.rem_euclid(rhs), false) | |
1579 | } | |
1580 | } | |
1581 | } | |
1582 | ||
1583 | ||
1584 | doc_comment! { | |
1585 | concat!("Negates self, overflowing if this is equal to the minimum value. | |
1586 | ||
1587 | Returns a tuple of the negated version of self along with a boolean indicating whether an overflow | |
1588 | happened. If `self` is the minimum value (e.g., `i32::MIN` for values of type `i32`), then the | |
1589 | minimum value will be returned again and `true` will be returned for an overflow happening. | |
1590 | ||
1591 | # Examples | |
1592 | ||
1593 | Basic usage: | |
1594 | ||
1595 | ``` | |
1596 | assert_eq!(2", stringify!($SelfT), ".overflowing_neg(), (-2, false)); | |
1597 | assert_eq!(", stringify!($SelfT), "::MIN.overflowing_neg(), (", stringify!($SelfT), | |
1598 | "::MIN, true));", $EndFeature, " | |
1599 | ```"), | |
1600 | #[inline] | |
1601 | #[stable(feature = "wrapping", since = "1.7.0")] | |
1602 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1603 | #[allow(unused_attributes)] | |
1604 | pub const fn overflowing_neg(self) -> (Self, bool) { | |
1605 | if unlikely!(self == Self::MIN) { | |
1606 | (Self::MIN, true) | |
1607 | } else { | |
1608 | (-self, false) | |
1609 | } | |
1610 | } | |
1611 | } | |
1612 | ||
1613 | doc_comment! { | |
1614 | concat!("Shifts self left by `rhs` bits. | |
1615 | ||
1616 | Returns a tuple of the shifted version of self along with a boolean indicating whether the shift | |
1617 | value was larger than or equal to the number of bits. If the shift value is too large, then value is | |
1618 | masked (N-1) where N is the number of bits, and this value is then used to perform the shift. | |
1619 | ||
1620 | # Examples | |
1621 | ||
1622 | Basic usage: | |
1623 | ||
1624 | ``` | |
1625 | ", $Feature, "assert_eq!(0x1", stringify!($SelfT),".overflowing_shl(4), (0x10, false)); | |
1626 | assert_eq!(0x1i32.overflowing_shl(36), (0x10, true));", | |
1627 | $EndFeature, " | |
1628 | ```"), | |
1629 | #[stable(feature = "wrapping", since = "1.7.0")] | |
1630 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1631 | #[must_use = "this returns the result of the operation, \ | |
1632 | without modifying the original"] | |
1633 | #[inline] | |
1634 | pub const fn overflowing_shl(self, rhs: u32) -> (Self, bool) { | |
1635 | (self.wrapping_shl(rhs), (rhs > ($BITS - 1))) | |
1636 | } | |
1637 | } | |
1638 | ||
1639 | doc_comment! { | |
1640 | concat!("Shifts self right by `rhs` bits. | |
1641 | ||
1642 | Returns a tuple of the shifted version of self along with a boolean indicating whether the shift | |
1643 | value was larger than or equal to the number of bits. If the shift value is too large, then value is | |
1644 | masked (N-1) where N is the number of bits, and this value is then used to perform the shift. | |
1645 | ||
1646 | # Examples | |
1647 | ||
1648 | Basic usage: | |
1649 | ||
1650 | ``` | |
1651 | ", $Feature, "assert_eq!(0x10", stringify!($SelfT), ".overflowing_shr(4), (0x1, false)); | |
1652 | assert_eq!(0x10i32.overflowing_shr(36), (0x1, true));", | |
1653 | $EndFeature, " | |
1654 | ```"), | |
1655 | #[stable(feature = "wrapping", since = "1.7.0")] | |
1656 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1657 | #[must_use = "this returns the result of the operation, \ | |
1658 | without modifying the original"] | |
1659 | #[inline] | |
1660 | pub const fn overflowing_shr(self, rhs: u32) -> (Self, bool) { | |
1661 | (self.wrapping_shr(rhs), (rhs > ($BITS - 1))) | |
1662 | } | |
1663 | } | |
1664 | ||
1665 | doc_comment! { | |
1666 | concat!("Computes the absolute value of `self`. | |
1667 | ||
1668 | Returns a tuple of the absolute version of self along with a boolean indicating whether an overflow | |
1669 | happened. If self is the minimum value (e.g., ", stringify!($SelfT), "::MIN for values of type | |
1670 | ", stringify!($SelfT), "), then the minimum value will be returned again and true will be returned | |
1671 | for an overflow happening. | |
1672 | ||
1673 | # Examples | |
1674 | ||
1675 | Basic usage: | |
1676 | ||
1677 | ``` | |
1678 | ", $Feature, "assert_eq!(10", stringify!($SelfT), ".overflowing_abs(), (10, false)); | |
1679 | assert_eq!((-10", stringify!($SelfT), ").overflowing_abs(), (10, false)); | |
1680 | assert_eq!((", stringify!($SelfT), "::MIN).overflowing_abs(), (", stringify!($SelfT), | |
1681 | "::MIN, true));", | |
1682 | $EndFeature, " | |
1683 | ```"), | |
1684 | #[stable(feature = "no_panic_abs", since = "1.13.0")] | |
1685 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1686 | #[inline] | |
1687 | pub const fn overflowing_abs(self) -> (Self, bool) { | |
1688 | (self.wrapping_abs(), self == Self::MIN) | |
1689 | } | |
1690 | } | |
1691 | ||
1692 | doc_comment! { | |
1693 | concat!("Raises self to the power of `exp`, using exponentiation by squaring. | |
1694 | ||
1695 | Returns a tuple of the exponentiation along with a bool indicating | |
1696 | whether an overflow happened. | |
1697 | ||
1698 | # Examples | |
1699 | ||
1700 | Basic usage: | |
1701 | ||
1702 | ``` | |
1703 | ", $Feature, "assert_eq!(3", stringify!($SelfT), ".overflowing_pow(4), (81, false)); | |
1704 | assert_eq!(3i8.overflowing_pow(5), (-13, true));", | |
1705 | $EndFeature, " | |
1706 | ```"), | |
1707 | #[stable(feature = "no_panic_pow", since = "1.34.0")] | |
1708 | #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")] | |
1709 | #[must_use = "this returns the result of the operation, \ | |
1710 | without modifying the original"] | |
1711 | #[inline] | |
1712 | pub const fn overflowing_pow(self, mut exp: u32) -> (Self, bool) { | |
1713 | if exp == 0 { | |
1714 | return (1,false); | |
1715 | } | |
1716 | let mut base = self; | |
1717 | let mut acc: Self = 1; | |
1718 | let mut overflown = false; | |
1719 | // Scratch space for storing results of overflowing_mul. | |
1720 | let mut r; | |
1721 | ||
1722 | while exp > 1 { | |
1723 | if (exp & 1) == 1 { | |
1724 | r = acc.overflowing_mul(base); | |
1725 | acc = r.0; | |
1726 | overflown |= r.1; | |
1727 | } | |
1728 | exp /= 2; | |
1729 | r = base.overflowing_mul(base); | |
1730 | base = r.0; | |
1731 | overflown |= r.1; | |
1732 | } | |
1733 | ||
1734 | // since exp!=0, finally the exp must be 1. | |
1735 | // Deal with the final bit of the exponent separately, since | |
1736 | // squaring the base afterwards is not necessary and may cause a | |
1737 | // needless overflow. | |
1738 | r = acc.overflowing_mul(base); | |
1739 | r.1 |= overflown; | |
1740 | r | |
1741 | } | |
1742 | } | |
1743 | ||
1744 | doc_comment! { | |
1745 | concat!("Raises self to the power of `exp`, using exponentiation by squaring. | |
1746 | ||
1747 | # Examples | |
1748 | ||
1749 | Basic usage: | |
1750 | ||
1751 | ``` | |
1752 | ", $Feature, "let x: ", stringify!($SelfT), " = 2; // or any other integer type | |
1753 | ||
1754 | assert_eq!(x.pow(5), 32);", | |
1755 | $EndFeature, " | |
1756 | ```"), | |
1757 | #[stable(feature = "rust1", since = "1.0.0")] | |
1758 | #[rustc_const_unstable(feature = "const_int_pow", issue = "53718")] | |
1759 | #[must_use = "this returns the result of the operation, \ | |
1760 | without modifying the original"] | |
1761 | #[inline] | |
1762 | #[rustc_inherit_overflow_checks] | |
1763 | pub const fn pow(self, mut exp: u32) -> Self { | |
1764 | if exp == 0 { | |
1765 | return 1; | |
1766 | } | |
1767 | let mut base = self; | |
1768 | let mut acc = 1; | |
1769 | ||
1770 | while exp > 1 { | |
1771 | if (exp & 1) == 1 { | |
1772 | acc = acc * base; | |
1773 | } | |
1774 | exp /= 2; | |
1775 | base = base * base; | |
1776 | } | |
1777 | ||
1778 | // since exp!=0, finally the exp must be 1. | |
1779 | // Deal with the final bit of the exponent separately, since | |
1780 | // squaring the base afterwards is not necessary and may cause a | |
1781 | // needless overflow. | |
1782 | acc * base | |
1783 | } | |
1784 | } | |
1785 | ||
1786 | doc_comment! { | |
1787 | concat!("Calculates the quotient of Euclidean division of `self` by `rhs`. | |
1788 | ||
1789 | This computes the integer `n` such that `self = n * rhs + self.rem_euclid(rhs)`, | |
1790 | with `0 <= self.rem_euclid(rhs) < rhs`. | |
1791 | ||
1792 | In other words, the result is `self / rhs` rounded to the integer `n` | |
1793 | such that `self >= n * rhs`. | |
1794 | If `self > 0`, this is equal to round towards zero (the default in Rust); | |
1795 | if `self < 0`, this is equal to round towards +/- infinity. | |
1796 | ||
1797 | # Panics | |
1798 | ||
1799 | This function will panic if `rhs` is 0 or the division results in overflow. | |
1800 | ||
1801 | # Examples | |
1802 | ||
1803 | Basic usage: | |
1804 | ||
1805 | ``` | |
1806 | let a: ", stringify!($SelfT), " = 7; // or any other integer type | |
1807 | let b = 4; | |
1808 | ||
1809 | assert_eq!(a.div_euclid(b), 1); // 7 >= 4 * 1 | |
1810 | assert_eq!(a.div_euclid(-b), -1); // 7 >= -4 * -1 | |
1811 | assert_eq!((-a).div_euclid(b), -2); // -7 >= 4 * -2 | |
1812 | assert_eq!((-a).div_euclid(-b), 2); // -7 >= -4 * 2 | |
1813 | ```"), | |
1814 | #[stable(feature = "euclidean_division", since = "1.38.0")] | |
1815 | #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")] | |
1816 | #[must_use = "this returns the result of the operation, \ | |
1817 | without modifying the original"] | |
1818 | #[inline] | |
1819 | #[rustc_inherit_overflow_checks] | |
1820 | pub const fn div_euclid(self, rhs: Self) -> Self { | |
1821 | let q = self / rhs; | |
1822 | if self % rhs < 0 { | |
1823 | return if rhs > 0 { q - 1 } else { q + 1 } | |
1824 | } | |
1825 | q | |
1826 | } | |
1827 | } | |
1828 | ||
1829 | ||
1830 | doc_comment! { | |
1831 | concat!("Calculates the least nonnegative remainder of `self (mod rhs)`. | |
1832 | ||
1833 | This is done as if by the Euclidean division algorithm -- given | |
1834 | `r = self.rem_euclid(rhs)`, `self = rhs * self.div_euclid(rhs) + r`, and | |
1835 | `0 <= r < abs(rhs)`. | |
1836 | ||
1837 | # Panics | |
1838 | ||
1839 | This function will panic if `rhs` is 0 or the division results in overflow. | |
1840 | ||
1841 | # Examples | |
1842 | ||
1843 | Basic usage: | |
1844 | ||
1845 | ``` | |
1846 | let a: ", stringify!($SelfT), " = 7; // or any other integer type | |
1847 | let b = 4; | |
1848 | ||
1849 | assert_eq!(a.rem_euclid(b), 3); | |
1850 | assert_eq!((-a).rem_euclid(b), 1); | |
1851 | assert_eq!(a.rem_euclid(-b), 3); | |
1852 | assert_eq!((-a).rem_euclid(-b), 1); | |
1853 | ```"), | |
1854 | #[stable(feature = "euclidean_division", since = "1.38.0")] | |
1855 | #[rustc_const_unstable(feature = "const_euclidean_int_methods", issue = "53718")] | |
1856 | #[must_use = "this returns the result of the operation, \ | |
1857 | without modifying the original"] | |
1858 | #[inline] | |
1859 | #[rustc_inherit_overflow_checks] | |
1860 | pub const fn rem_euclid(self, rhs: Self) -> Self { | |
1861 | let r = self % rhs; | |
1862 | if r < 0 { | |
1863 | if rhs < 0 { | |
1864 | r - rhs | |
1865 | } else { | |
1866 | r + rhs | |
1867 | } | |
1868 | } else { | |
1869 | r | |
1870 | } | |
1871 | } | |
1872 | } | |
1873 | ||
1874 | doc_comment! { | |
1875 | concat!("Computes the absolute value of `self`. | |
1876 | ||
1877 | # Overflow behavior | |
1878 | ||
1879 | The absolute value of `", stringify!($SelfT), "::MIN` cannot be represented as an | |
1880 | `", stringify!($SelfT), "`, and attempting to calculate it will cause an overflow. This means that | |
1881 | code in debug mode will trigger a panic on this case and optimized code will return `", | |
1882 | stringify!($SelfT), "::MIN` without a panic. | |
1883 | ||
1884 | # Examples | |
1885 | ||
1886 | Basic usage: | |
1887 | ||
1888 | ``` | |
1889 | ", $Feature, "assert_eq!(10", stringify!($SelfT), ".abs(), 10); | |
1890 | assert_eq!((-10", stringify!($SelfT), ").abs(), 10);", | |
1891 | $EndFeature, " | |
1892 | ```"), | |
1893 | #[stable(feature = "rust1", since = "1.0.0")] | |
1894 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1895 | #[allow(unused_attributes)] | |
1896 | #[inline] | |
1897 | #[rustc_inherit_overflow_checks] | |
1898 | pub const fn abs(self) -> Self { | |
1899 | // Note that the #[inline] above means that the overflow | |
1900 | // semantics of the subtraction depend on the crate we're being | |
1901 | // inlined into. | |
1902 | if self.is_negative() { | |
1903 | -self | |
1904 | } else { | |
1905 | self | |
1906 | } | |
1907 | } | |
1908 | } | |
1909 | ||
1910 | doc_comment! { | |
1911 | concat!("Returns a number representing sign of `self`. | |
1912 | ||
1913 | - `0` if the number is zero | |
1914 | - `1` if the number is positive | |
1915 | - `-1` if the number is negative | |
1916 | ||
1917 | # Examples | |
1918 | ||
1919 | Basic usage: | |
1920 | ||
1921 | ``` | |
1922 | ", $Feature, "assert_eq!(10", stringify!($SelfT), ".signum(), 1); | |
1923 | assert_eq!(0", stringify!($SelfT), ".signum(), 0); | |
1924 | assert_eq!((-10", stringify!($SelfT), ").signum(), -1);", | |
1925 | $EndFeature, " | |
1926 | ```"), | |
1927 | #[stable(feature = "rust1", since = "1.0.0")] | |
1928 | #[rustc_const_stable(feature = "const_int_sign", since = "1.47.0")] | |
1929 | #[inline] | |
1930 | pub const fn signum(self) -> Self { | |
1931 | match self { | |
1932 | n if n > 0 => 1, | |
1933 | 0 => 0, | |
1934 | _ => -1, | |
1935 | } | |
1936 | } | |
1937 | } | |
1938 | ||
1939 | doc_comment! { | |
1940 | concat!("Returns `true` if `self` is positive and `false` if the number is zero or | |
1941 | negative. | |
1942 | ||
1943 | # Examples | |
1944 | ||
1945 | Basic usage: | |
1946 | ||
1947 | ``` | |
1948 | ", $Feature, "assert!(10", stringify!($SelfT), ".is_positive()); | |
1949 | assert!(!(-10", stringify!($SelfT), ").is_positive());", | |
1950 | $EndFeature, " | |
1951 | ```"), | |
1952 | #[stable(feature = "rust1", since = "1.0.0")] | |
1953 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1954 | #[inline] | |
1955 | pub const fn is_positive(self) -> bool { self > 0 } | |
1956 | } | |
1957 | ||
1958 | doc_comment! { | |
1959 | concat!("Returns `true` if `self` is negative and `false` if the number is zero or | |
1960 | positive. | |
1961 | ||
1962 | # Examples | |
1963 | ||
1964 | Basic usage: | |
1965 | ||
1966 | ``` | |
1967 | ", $Feature, "assert!((-10", stringify!($SelfT), ").is_negative()); | |
1968 | assert!(!10", stringify!($SelfT), ".is_negative());", | |
1969 | $EndFeature, " | |
1970 | ```"), | |
1971 | #[stable(feature = "rust1", since = "1.0.0")] | |
1972 | #[rustc_const_stable(feature = "const_int_methods", since = "1.32.0")] | |
1973 | #[inline] | |
1974 | pub const fn is_negative(self) -> bool { self < 0 } | |
1975 | } | |
1976 | ||
1977 | doc_comment! { | |
1978 | concat!("Return the memory representation of this integer as a byte array in | |
1979 | big-endian (network) byte order. | |
1980 | ", | |
1981 | $to_xe_bytes_doc, | |
1982 | " | |
1983 | # Examples | |
1984 | ||
1985 | ``` | |
1986 | let bytes = ", $swap_op, stringify!($SelfT), ".to_be_bytes(); | |
1987 | assert_eq!(bytes, ", $be_bytes, "); | |
1988 | ```"), | |
1989 | #[stable(feature = "int_to_from_bytes", since = "1.32.0")] | |
1990 | #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")] | |
1991 | #[inline] | |
1992 | pub const fn to_be_bytes(self) -> [u8; mem::size_of::<Self>()] { | |
1993 | self.to_be().to_ne_bytes() | |
1994 | } | |
1995 | } | |
1996 | ||
1997 | doc_comment! { | |
1998 | concat!("Return the memory representation of this integer as a byte array in | |
1999 | little-endian byte order. | |
2000 | ", | |
2001 | $to_xe_bytes_doc, | |
2002 | " | |
2003 | # Examples | |
2004 | ||
2005 | ``` | |
2006 | let bytes = ", $swap_op, stringify!($SelfT), ".to_le_bytes(); | |
2007 | assert_eq!(bytes, ", $le_bytes, "); | |
2008 | ```"), | |
2009 | #[stable(feature = "int_to_from_bytes", since = "1.32.0")] | |
2010 | #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")] | |
2011 | #[inline] | |
2012 | pub const fn to_le_bytes(self) -> [u8; mem::size_of::<Self>()] { | |
2013 | self.to_le().to_ne_bytes() | |
2014 | } | |
2015 | } | |
2016 | ||
2017 | doc_comment! { | |
2018 | concat!(" | |
2019 | Return the memory representation of this integer as a byte array in | |
2020 | native byte order. | |
2021 | ||
2022 | As the target platform's native endianness is used, portable code | |
2023 | should use [`to_be_bytes`] or [`to_le_bytes`], as appropriate, | |
2024 | instead. | |
2025 | ", | |
2026 | $to_xe_bytes_doc, | |
2027 | " | |
2028 | [`to_be_bytes`]: #method.to_be_bytes | |
2029 | [`to_le_bytes`]: #method.to_le_bytes | |
2030 | ||
2031 | # Examples | |
2032 | ||
2033 | ``` | |
2034 | let bytes = ", $swap_op, stringify!($SelfT), ".to_ne_bytes(); | |
2035 | assert_eq!( | |
2036 | bytes, | |
2037 | if cfg!(target_endian = \"big\") { | |
2038 | ", $be_bytes, " | |
2039 | } else { | |
2040 | ", $le_bytes, " | |
2041 | } | |
2042 | ); | |
2043 | ```"), | |
2044 | #[stable(feature = "int_to_from_bytes", since = "1.32.0")] | |
2045 | #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")] | |
2046 | // SAFETY: const sound because integers are plain old datatypes so we can always | |
2047 | // transmute them to arrays of bytes | |
2048 | #[allow_internal_unstable(const_fn_transmute)] | |
2049 | #[inline] | |
2050 | pub const fn to_ne_bytes(self) -> [u8; mem::size_of::<Self>()] { | |
2051 | // SAFETY: integers are plain old datatypes so we can always transmute them to | |
2052 | // arrays of bytes | |
2053 | unsafe { mem::transmute(self) } | |
2054 | } | |
2055 | } | |
2056 | ||
2057 | doc_comment! { | |
2058 | concat!("Create an integer value from its representation as a byte array in | |
2059 | big endian. | |
2060 | ", | |
2061 | $from_xe_bytes_doc, | |
2062 | " | |
2063 | # Examples | |
2064 | ||
2065 | ``` | |
2066 | let value = ", stringify!($SelfT), "::from_be_bytes(", $be_bytes, "); | |
2067 | assert_eq!(value, ", $swap_op, "); | |
2068 | ``` | |
2069 | ||
2070 | When starting from a slice rather than an array, fallible conversion APIs can be used: | |
2071 | ||
2072 | ``` | |
2073 | use std::convert::TryInto; | |
2074 | ||
2075 | fn read_be_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { | |
2076 | let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">()); | |
2077 | *input = rest; | |
2078 | ", stringify!($SelfT), "::from_be_bytes(int_bytes.try_into().unwrap()) | |
2079 | } | |
2080 | ```"), | |
2081 | #[stable(feature = "int_to_from_bytes", since = "1.32.0")] | |
2082 | #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")] | |
2083 | #[inline] | |
2084 | pub const fn from_be_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { | |
2085 | Self::from_be(Self::from_ne_bytes(bytes)) | |
2086 | } | |
2087 | } | |
2088 | ||
2089 | doc_comment! { | |
2090 | concat!(" | |
2091 | Create an integer value from its representation as a byte array in | |
2092 | little endian. | |
2093 | ", | |
2094 | $from_xe_bytes_doc, | |
2095 | " | |
2096 | # Examples | |
2097 | ||
2098 | ``` | |
2099 | let value = ", stringify!($SelfT), "::from_le_bytes(", $le_bytes, "); | |
2100 | assert_eq!(value, ", $swap_op, "); | |
2101 | ``` | |
2102 | ||
2103 | When starting from a slice rather than an array, fallible conversion APIs can be used: | |
2104 | ||
2105 | ``` | |
2106 | use std::convert::TryInto; | |
2107 | ||
2108 | fn read_le_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { | |
2109 | let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">()); | |
2110 | *input = rest; | |
2111 | ", stringify!($SelfT), "::from_le_bytes(int_bytes.try_into().unwrap()) | |
2112 | } | |
2113 | ```"), | |
2114 | #[stable(feature = "int_to_from_bytes", since = "1.32.0")] | |
2115 | #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")] | |
2116 | #[inline] | |
2117 | pub const fn from_le_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { | |
2118 | Self::from_le(Self::from_ne_bytes(bytes)) | |
2119 | } | |
2120 | } | |
2121 | ||
2122 | doc_comment! { | |
2123 | concat!("Create an integer value from its memory representation as a byte | |
2124 | array in native endianness. | |
2125 | ||
2126 | As the target platform's native endianness is used, portable code | |
2127 | likely wants to use [`from_be_bytes`] or [`from_le_bytes`], as | |
2128 | appropriate instead. | |
2129 | ||
2130 | [`from_be_bytes`]: #method.from_be_bytes | |
2131 | [`from_le_bytes`]: #method.from_le_bytes | |
2132 | ", | |
2133 | $from_xe_bytes_doc, | |
2134 | " | |
2135 | # Examples | |
2136 | ||
2137 | ``` | |
2138 | let value = ", stringify!($SelfT), "::from_ne_bytes(if cfg!(target_endian = \"big\") { | |
2139 | ", $be_bytes, " | |
2140 | } else { | |
2141 | ", $le_bytes, " | |
2142 | }); | |
2143 | assert_eq!(value, ", $swap_op, "); | |
2144 | ``` | |
2145 | ||
2146 | When starting from a slice rather than an array, fallible conversion APIs can be used: | |
2147 | ||
2148 | ``` | |
2149 | use std::convert::TryInto; | |
2150 | ||
2151 | fn read_ne_", stringify!($SelfT), "(input: &mut &[u8]) -> ", stringify!($SelfT), " { | |
2152 | let (int_bytes, rest) = input.split_at(std::mem::size_of::<", stringify!($SelfT), ">()); | |
2153 | *input = rest; | |
2154 | ", stringify!($SelfT), "::from_ne_bytes(int_bytes.try_into().unwrap()) | |
2155 | } | |
2156 | ```"), | |
2157 | #[stable(feature = "int_to_from_bytes", since = "1.32.0")] | |
2158 | #[rustc_const_stable(feature = "const_int_conversion", since = "1.44.0")] | |
2159 | // SAFETY: const sound because integers are plain old datatypes so we can always | |
2160 | // transmute to them | |
2161 | #[allow_internal_unstable(const_fn_transmute)] | |
2162 | #[inline] | |
2163 | pub const fn from_ne_bytes(bytes: [u8; mem::size_of::<Self>()]) -> Self { | |
2164 | // SAFETY: integers are plain old datatypes so we can always transmute to them | |
2165 | unsafe { mem::transmute(bytes) } | |
2166 | } | |
2167 | } | |
2168 | ||
2169 | doc_comment! { | |
2170 | concat!("**This method is soft-deprecated.** | |
2171 | ||
2172 | Although using it won’t cause a compilation warning, | |
2173 | new code should use [`", stringify!($SelfT), "::MIN", "`](#associatedconstant.MIN) instead. | |
2174 | ||
2175 | Returns the smallest value that can be represented by this integer type."), | |
2176 | #[stable(feature = "rust1", since = "1.0.0")] | |
2177 | #[inline(always)] | |
2178 | #[rustc_promotable] | |
2179 | #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")] | |
2180 | pub const fn min_value() -> Self { | |
2181 | Self::MIN | |
2182 | } | |
2183 | } | |
2184 | ||
2185 | doc_comment! { | |
2186 | concat!("**This method is soft-deprecated.** | |
2187 | ||
2188 | Although using it won’t cause a compilation warning, | |
2189 | new code should use [`", stringify!($SelfT), "::MAX", "`](#associatedconstant.MAX) instead. | |
2190 | ||
2191 | Returns the largest value that can be represented by this integer type."), | |
2192 | #[stable(feature = "rust1", since = "1.0.0")] | |
2193 | #[inline(always)] | |
2194 | #[rustc_promotable] | |
2195 | #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")] | |
2196 | pub const fn max_value() -> Self { | |
2197 | Self::MAX | |
2198 | } | |
74b04a01 | 2199 | } |
1b1a35ee | 2200 | } |
32a655c1 | 2201 | } |