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
use num::arithmetic::traits::{SaturatingMulAssign, SaturatingSquare, SaturatingSquareAssign};

macro_rules! impl_saturating_square {
    ($t:ident) => {
        impl SaturatingSquare for $t {
            type Output = $t;

            /// Squares a number, saturating at the numeric bounds instead of overflowing.
            ///
            /// $$
            /// f(x) = \\begin{cases}
            ///     x^2 & \text{if} \\quad x^2 \leq M, \\\\
            ///     M & \text{if} \\quad x^2 > M,
            /// \\end{cases}
            /// $$
            /// where $M$ is `Self::MAX`.
            ///
            /// # Worst-case complexity
            /// Constant time and additional memory.
            ///
            /// # Examples
            /// See [here](super::saturating_square#saturating_square).
            #[inline]
            fn saturating_square(self) -> $t {
                self.saturating_mul(self)
            }
        }

        impl SaturatingSquareAssign for $t {
            /// Squares a number in place, saturating at the numeric bounds instead of overflowing.
            ///
            /// $$
            /// x \gets \\begin{cases}
            ///     x^2 & \text{if} \\quad x^2 \leq M, \\\\
            ///     M & \text{if} \\quad x^2 > M,
            /// \\end{cases}
            /// $$
            /// where $M$ is `Self::MAX`.
            ///
            /// # Worst-case complexity
            /// Constant time and additional memory.
            ///
            /// # Examples
            /// See [here](super::saturating_square#saturating_square_assign).
            #[inline]
            fn saturating_square_assign(&mut self) {
                self.saturating_mul_assign(*self);
            }
        }
    };
}
apply_to_primitive_ints!(impl_saturating_square);