|
1 |
| -use super::Wrapping; |
| 1 | +//! Definitions of `Wrapping<T>`. |
| 2 | +
|
| 3 | +use crate::fmt; |
| 4 | +use crate::ops::{Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign}; |
| 5 | +use crate::ops::{BitXor, BitXorAssign, Div, DivAssign}; |
| 6 | +use crate::ops::{Mul, MulAssign, Neg, Not, Rem, RemAssign}; |
| 7 | +use crate::ops::{Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign}; |
| 8 | + |
| 9 | +/// Provides intentionally-wrapped arithmetic on `T`. |
| 10 | +/// |
| 11 | +/// Operations like `+` on `u32` values are intended to never overflow, |
| 12 | +/// and in some debug configurations overflow is detected and results |
| 13 | +/// in a panic. While most arithmetic falls into this category, some |
| 14 | +/// code explicitly expects and relies upon modular arithmetic (e.g., |
| 15 | +/// hashing). |
| 16 | +/// |
| 17 | +/// Wrapping arithmetic can be achieved either through methods like |
| 18 | +/// `wrapping_add`, or through the `Wrapping<T>` type, which says that |
| 19 | +/// all standard arithmetic operations on the underlying value are |
| 20 | +/// intended to have wrapping semantics. |
| 21 | +/// |
| 22 | +/// The underlying value can be retrieved through the `.0` index of the |
| 23 | +/// `Wrapping` tuple. |
| 24 | +/// |
| 25 | +/// # Examples |
| 26 | +/// |
| 27 | +/// ``` |
| 28 | +/// use std::num::Wrapping; |
| 29 | +/// |
| 30 | +/// let zero = Wrapping(0u32); |
| 31 | +/// let one = Wrapping(1u32); |
| 32 | +/// |
| 33 | +/// assert_eq!(u32::MAX, (zero - one).0); |
| 34 | +/// ``` |
| 35 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 36 | +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Default, Hash)] |
| 37 | +#[repr(transparent)] |
| 38 | +pub struct Wrapping<T>(#[stable(feature = "rust1", since = "1.0.0")] pub T); |
| 39 | + |
| 40 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 41 | +impl<T: fmt::Debug> fmt::Debug for Wrapping<T> { |
| 42 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 43 | + self.0.fmt(f) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[stable(feature = "wrapping_display", since = "1.10.0")] |
| 48 | +impl<T: fmt::Display> fmt::Display for Wrapping<T> { |
| 49 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 50 | + self.0.fmt(f) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +#[stable(feature = "wrapping_fmt", since = "1.11.0")] |
| 55 | +impl<T: fmt::Binary> fmt::Binary for Wrapping<T> { |
| 56 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 57 | + self.0.fmt(f) |
| 58 | + } |
| 59 | +} |
2 | 60 |
|
3 |
| -use crate::ops::*; |
| 61 | +#[stable(feature = "wrapping_fmt", since = "1.11.0")] |
| 62 | +impl<T: fmt::Octal> fmt::Octal for Wrapping<T> { |
| 63 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 64 | + self.0.fmt(f) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[stable(feature = "wrapping_fmt", since = "1.11.0")] |
| 69 | +impl<T: fmt::LowerHex> fmt::LowerHex for Wrapping<T> { |
| 70 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 71 | + self.0.fmt(f) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +#[stable(feature = "wrapping_fmt", since = "1.11.0")] |
| 76 | +impl<T: fmt::UpperHex> fmt::UpperHex for Wrapping<T> { |
| 77 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 78 | + self.0.fmt(f) |
| 79 | + } |
| 80 | +} |
4 | 81 |
|
5 | 82 | #[allow(unused_macros)]
|
6 | 83 | macro_rules! sh_impl_signed {
|
|
0 commit comments