]> git.proxmox.com Git - cargo.git/blobdiff - vendor/typenum/src/uint.rs
New upstream version 0.52.0
[cargo.git] / vendor / typenum / src / uint.rs
index 12825627c0b98c4c456766d9b757c0a62daff107..aed884fe885e84c2413a5734a8f7f00033cc28e8 100644 (file)
@@ -3,7 +3,7 @@
 //!
 //! **Type operators** implemented:
 //!
-//! From `core::ops`: `BitAnd`, `BitOr`, `BitXor`, `Shl`, `Shr`, `Add`, `Sub`,
+//! From `::core::ops`: `BitAnd`, `BitOr`, `BitXor`, `Shl`, `Shr`, `Add`, `Sub`,
 //!                 `Mul`, `Div`, and `Rem`.
 //! From `typenum`: `Same`, `Cmp`, and `Pow`.
 //!
@@ -12,7 +12,7 @@
 //!
 //! # Example
 //! ```rust
-//! use std::ops::{BitAnd, BitOr, BitXor, Shl, Shr, Add, Sub, Mul, Div, Rem};
+//! use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Rem, Shl, Shr, Sub};
 //! use typenum::{Unsigned, U1, U2, U3, U4};
 //!
 //! assert_eq!(<U3 as BitAnd<U2>>::Output::to_u32(), 2);
 //! assert_eq!(<U3 as Div<U2>>::Output::to_u32(), 1);
 //! assert_eq!(<U3 as Rem<U2>>::Output::to_u32(), 1);
 //! ```
-//!
-
-use core::ops::{Add, BitAnd, BitOr, BitXor, Mul, Shl, Shr, Sub};
-use {
-    Cmp, Equal, Gcd, Greater, IsGreaterOrEqual, Len, Less, Logarithm2, Maximum, Minimum, NonZero,
-    Ord, Pow, SquareRoot,
-};
-
-use bit::{Bit, B0, B1};
 
-use private::{
-    BitDiff, PrivateAnd, PrivateCmp, PrivateLogarithm2, PrivatePow, PrivateSquareRoot, PrivateSub,
-    PrivateXor, Trim,
+use crate::{
+    bit::{Bit, B0, B1},
+    consts::{U0, U1},
+    private::{
+        BitDiff, BitDiffOut, Internal, InternalMarker, PrivateAnd, PrivateAndOut, PrivateCmp,
+        PrivateCmpOut, PrivateLogarithm2, PrivatePow, PrivatePowOut, PrivateSquareRoot, PrivateSub,
+        PrivateSubOut, PrivateXor, PrivateXorOut, Trim, TrimOut,
+    },
+    Add1, Cmp, Double, Equal, Gcd, Gcf, GrEq, Greater, IsGreaterOrEqual, Len, Length, Less, Log2,
+    Logarithm2, Maximum, Minimum, NonZero, Or, Ord, Pow, Prod, Shleft, Shright, Sqrt, Square,
+    SquareRoot, Sub1, Sum, ToInt, Zero,
 };
+use core::ops::{Add, BitAnd, BitOr, BitXor, Mul, Shl, Shr, Sub};
 
-use private::{
-    BitDiffOut, PrivateAndOut, PrivateCmpOut, PrivatePowOut, PrivateSubOut, PrivateXorOut, TrimOut,
-};
-
-use private::{Internal, InternalMarker};
-
-use consts::{U0, U1};
-use {Add1, Double, Gcf, GrEq, Length, Log2, Or, Prod, Shleft, Shright, Sqrt, Square, Sub1, Sum};
-
-pub use marker_traits::{PowerOfTwo, Unsigned};
+pub use crate::marker_traits::{PowerOfTwo, Unsigned};
 
 /// The terminating type for `UInt`; it always comes after the most significant
 /// bit. `UTerm` by itself represents zero, which is aliased to `U0`.
@@ -146,7 +137,7 @@ impl Unsigned for UTerm {
 ///
 /// # Example
 /// ```rust
-/// use typenum::{B0, B1, UInt, UTerm};
+/// use typenum::{UInt, UTerm, B0, B1};
 ///
 /// # #[allow(dead_code)]
 /// type U6 = UInt<UInt<UInt<UTerm, B1>, B1>, B0>;
@@ -238,6 +229,7 @@ impl<U: Unsigned, B: Bit> Unsigned for UInt<U, B> {
 }
 
 impl<U: Unsigned, B: Bit> NonZero for UInt<U, B> {}
+impl Zero for UTerm {}
 
 impl PowerOfTwo for UInt<UTerm, B1> {}
 impl<U: Unsigned + PowerOfTwo> PowerOfTwo for UInt<U, B0> {}
@@ -1289,7 +1281,7 @@ where
 
 // ---------------------------------------------------------------------------------------
 // Shifting one number until it's the size of another
-use private::ShiftDiff;
+use crate::private::ShiftDiff;
 impl<Ul: Unsigned, Ur: Unsigned> ShiftDiff<Ur> for Ul
 where
     Ur: BitDiff<Ul>,
@@ -1439,7 +1431,7 @@ where
 #[cfg(test)]
 mod gcd_tests {
     use super::*;
-    use consts::*;
+    use crate::consts::*;
 
     macro_rules! gcd_test {
         (
@@ -1475,7 +1467,7 @@ pub trait GetBit<I> {
     type Output;
 
     #[doc(hidden)]
-    fn get_bit<IM: InternalMarker>(&self, &I) -> Self::Output;
+    fn get_bit<IM: InternalMarker>(&self, _: &I) -> Self::Output;
 }
 
 #[allow(missing_docs)]
@@ -1520,8 +1512,8 @@ impl<I> GetBit<I> for UTerm {
 
 #[test]
 fn test_get_bit() {
-    use consts::*;
-    use Same;
+    use crate::consts::*;
+    use crate::Same;
     type T1 = <GetBitOut<U2, U0> as Same<B0>>::Output;
     type T2 = <GetBitOut<U2, U1> as Same<B1>>::Output;
     type T3 = <GetBitOut<U2, U2> as Same<B0>>::Output;
@@ -1541,12 +1533,12 @@ pub trait SetBit<I, B> {
     type Output;
 
     #[doc(hidden)]
-    fn set_bit<IM: InternalMarker>(self, I, B) -> Self::Output;
+    fn set_bit<IM: InternalMarker>(self, _: I, _: B) -> Self::Output;
 }
 /// Alias for the result of calling `SetBit`: `SetBitOut<N, I, B> = <N as SetBit<I, B>>::Output`.
 pub type SetBitOut<N, I, B> = <N as SetBit<I, B>>::Output;
 
-use private::{PrivateSetBit, PrivateSetBitOut};
+use crate::private::{PrivateSetBit, PrivateSetBitOut};
 
 // Call private one then trim it
 impl<N, I, B> SetBit<I, B> for N
@@ -1617,8 +1609,8 @@ where
 
 #[test]
 fn test_set_bit() {
-    use consts::*;
-    use Same;
+    use crate::consts::*;
+    use crate::Same;
     type T1 = <SetBitOut<U2, U0, B0> as Same<U2>>::Output;
     type T2 = <SetBitOut<U2, U0, B1> as Same<U3>>::Output;
     type T3 = <SetBitOut<U2, U1, B0> as Same<U0>>::Output;
@@ -1669,8 +1661,8 @@ mod tests {
     }
     #[test]
     fn test_div() {
-        use consts::*;
-        use {Quot, Same};
+        use crate::consts::*;
+        use crate::{Quot, Same};
 
         test_div!(U0 / U1 = U0);
         test_div!(U1 / U1 = U1);
@@ -1747,9 +1739,9 @@ where
 
 // -----------------------------------------
 // PrivateDiv
-use private::{PrivateDiv, PrivateDivQuot, PrivateDivRem};
+use crate::private::{PrivateDiv, PrivateDivQuot, PrivateDivRem};
 
-use Compare;
+use crate::Compare;
 // R == 0: We set R = UInt<UTerm, N[i]>, then call out to PrivateDivIf for the if statement
 impl<N, D, Q, I> PrivateDiv<N, D, Q, U0, I> for ()
 where
@@ -1861,7 +1853,7 @@ where
 // -----------------------------------------
 // PrivateDivIf
 
-use private::{PrivateDivIf, PrivateDivIfQuot, PrivateDivIfRem};
+use crate::private::{PrivateDivIf, PrivateDivIfQuot, PrivateDivIfRem};
 
 // R < D, I > 0, we do nothing and recurse
 impl<N, D, Q, R, Ui, Bi> PrivateDivIf<N, D, Q, R, UInt<Ui, Bi>, Less> for ()
@@ -1940,7 +1932,7 @@ where {
     }
 }
 
-use Diff;
+use crate::Diff;
 // R > D, I > 0, we set R -= D, Q[I] = 1 and recurse
 impl<N, D, Q, R, Ui, Bi> PrivateDivIf<N, D, Q, R, UInt<Ui, Bi>, Greater> for ()
 where
@@ -2049,7 +2041,7 @@ where
 
 // -----------------------------------------
 // PartialDiv
-use {PartialDiv, Quot};
+use crate::{PartialDiv, Quot};
 impl<Ur: Unsigned, Br: Bit> PartialDiv<UInt<Ur, Br>> for UTerm {
     type Output = UTerm;
     #[inline]
@@ -2072,7 +2064,7 @@ where
 
 // -----------------------------------------
 // PrivateMin
-use private::{PrivateMin, PrivateMinOut};
+use crate::private::{PrivateMin, PrivateMinOut};
 
 impl<U, B, Ur> PrivateMin<Ur, Equal> for UInt<U, B>
 where
@@ -2115,7 +2107,7 @@ where
 
 // -----------------------------------------
 // Min
-use Min;
+use crate::Min;
 
 impl<U> Min<U> for UTerm
 where
@@ -2144,7 +2136,7 @@ where
 
 // -----------------------------------------
 // PrivateMax
-use private::{PrivateMax, PrivateMaxOut};
+use crate::private::{PrivateMax, PrivateMaxOut};
 
 impl<U, B, Ur> PrivateMax<Ur, Equal> for UInt<U, B>
 where
@@ -2187,7 +2179,7 @@ where
 
 // -----------------------------------------
 // Max
-use Max;
+use crate::Max;
 
 impl<U> Max<U> for UTerm
 where
@@ -2262,7 +2254,7 @@ where
 
 #[test]
 fn sqrt_test() {
-    use consts::*;
+    use crate::consts::*;
 
     assert_eq!(0, <Sqrt<U0>>::to_u32());
 
@@ -2324,47 +2316,283 @@ where
     type Output = Add1<Log2<U>>;
 }
 
-#[test]
-fn log2_test() {
-    use consts::*;
-
-    assert_eq!(0, <Log2<U1>>::to_u32());
-
-    assert_eq!(1, <Log2<U2>>::to_u32());
-    assert_eq!(1, <Log2<U3>>::to_u32());
-
-    assert_eq!(2, <Log2<U4>>::to_u32());
-    assert_eq!(2, <Log2<U5>>::to_u32());
-    assert_eq!(2, <Log2<U6>>::to_u32());
-    assert_eq!(2, <Log2<U7>>::to_u32());
-
-    assert_eq!(3, <Log2<U8>>::to_u32());
-    assert_eq!(3, <Log2<U9>>::to_u32());
-    assert_eq!(3, <Log2<U10>>::to_u32());
-    assert_eq!(3, <Log2<U11>>::to_u32());
-    assert_eq!(3, <Log2<U12>>::to_u32());
-    assert_eq!(3, <Log2<U13>>::to_u32());
-    assert_eq!(3, <Log2<U14>>::to_u32());
-    assert_eq!(3, <Log2<U15>>::to_u32());
-
-    assert_eq!(4, <Log2<U16>>::to_u32());
-    assert_eq!(4, <Log2<U17>>::to_u32());
-    assert_eq!(4, <Log2<U18>>::to_u32());
-    assert_eq!(4, <Log2<U19>>::to_u32());
-    assert_eq!(4, <Log2<U20>>::to_u32());
-    assert_eq!(4, <Log2<U21>>::to_u32());
-    assert_eq!(4, <Log2<U22>>::to_u32());
-    assert_eq!(4, <Log2<U23>>::to_u32());
-    assert_eq!(4, <Log2<U24>>::to_u32());
-    assert_eq!(4, <Log2<U25>>::to_u32());
-    assert_eq!(4, <Log2<U26>>::to_u32());
-    assert_eq!(4, <Log2<U27>>::to_u32());
-    assert_eq!(4, <Log2<U28>>::to_u32());
-    assert_eq!(4, <Log2<U29>>::to_u32());
-    assert_eq!(4, <Log2<U30>>::to_u32());
-    assert_eq!(4, <Log2<U31>>::to_u32());
-
-    assert_eq!(5, <Log2<U32>>::to_u32());
-    assert_eq!(5, <Log2<U33>>::to_u32());
-    // ...
+// -----------------------------------------
+// ToInt
+
+impl ToInt<i8> for UTerm {
+    #[inline]
+    fn to_int() -> i8 {
+        Self::I8
+    }
+}
+
+impl ToInt<i16> for UTerm {
+    #[inline]
+    fn to_int() -> i16 {
+        Self::I16
+    }
+}
+
+impl ToInt<i32> for UTerm {
+    #[inline]
+    fn to_int() -> i32 {
+        Self::I32
+    }
+}
+
+impl ToInt<i64> for UTerm {
+    #[inline]
+    fn to_int() -> i64 {
+        Self::I64
+    }
+}
+
+impl ToInt<u8> for UTerm {
+    #[inline]
+    fn to_int() -> u8 {
+        Self::U8
+    }
+}
+
+impl ToInt<u16> for UTerm {
+    #[inline]
+    fn to_int() -> u16 {
+        Self::U16
+    }
+}
+
+impl ToInt<u32> for UTerm {
+    #[inline]
+    fn to_int() -> u32 {
+        Self::U32
+    }
+}
+
+impl ToInt<u64> for UTerm {
+    #[inline]
+    fn to_int() -> u64 {
+        Self::U64
+    }
+}
+
+impl ToInt<usize> for UTerm {
+    #[inline]
+    fn to_int() -> usize {
+        Self::USIZE
+    }
+}
+
+impl<U, B> ToInt<i8> for UInt<U, B>
+where
+    U: Unsigned,
+    B: Bit,
+{
+    #[inline]
+    fn to_int() -> i8 {
+        Self::I8
+    }
+}
+
+impl<U, B> ToInt<i16> for UInt<U, B>
+where
+    U: Unsigned,
+    B: Bit,
+{
+    #[inline]
+    fn to_int() -> i16 {
+        Self::I16
+    }
+}
+
+impl<U, B> ToInt<i32> for UInt<U, B>
+where
+    U: Unsigned,
+    B: Bit,
+{
+    #[inline]
+    fn to_int() -> i32 {
+        Self::I32
+    }
+}
+
+impl<U, B> ToInt<i64> for UInt<U, B>
+where
+    U: Unsigned,
+    B: Bit,
+{
+    #[inline]
+    fn to_int() -> i64 {
+        Self::I64
+    }
+}
+
+impl<U, B> ToInt<u8> for UInt<U, B>
+where
+    U: Unsigned,
+    B: Bit,
+{
+    #[inline]
+    fn to_int() -> u8 {
+        Self::U8
+    }
+}
+
+impl<U, B> ToInt<u16> for UInt<U, B>
+where
+    U: Unsigned,
+    B: Bit,
+{
+    #[inline]
+    fn to_int() -> u16 {
+        Self::U16
+    }
+}
+
+impl<U, B> ToInt<u32> for UInt<U, B>
+where
+    U: Unsigned,
+    B: Bit,
+{
+    #[inline]
+    fn to_int() -> u32 {
+        Self::U32
+    }
+}
+
+impl<U, B> ToInt<u64> for UInt<U, B>
+where
+    U: Unsigned,
+    B: Bit,
+{
+    #[inline]
+    fn to_int() -> u64 {
+        Self::U64
+    }
+}
+
+impl<U, B> ToInt<usize> for UInt<U, B>
+where
+    U: Unsigned,
+    B: Bit,
+{
+    #[inline]
+    fn to_int() -> usize {
+        Self::USIZE
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use crate::consts::*;
+    use crate::{Log2, ToInt, Unsigned};
+
+    #[test]
+    fn log2_test() {
+        assert_eq!(0, <Log2<U1>>::to_u32());
+
+        assert_eq!(1, <Log2<U2>>::to_u32());
+        assert_eq!(1, <Log2<U3>>::to_u32());
+
+        assert_eq!(2, <Log2<U4>>::to_u32());
+        assert_eq!(2, <Log2<U5>>::to_u32());
+        assert_eq!(2, <Log2<U6>>::to_u32());
+        assert_eq!(2, <Log2<U7>>::to_u32());
+
+        assert_eq!(3, <Log2<U8>>::to_u32());
+        assert_eq!(3, <Log2<U9>>::to_u32());
+        assert_eq!(3, <Log2<U10>>::to_u32());
+        assert_eq!(3, <Log2<U11>>::to_u32());
+        assert_eq!(3, <Log2<U12>>::to_u32());
+        assert_eq!(3, <Log2<U13>>::to_u32());
+        assert_eq!(3, <Log2<U14>>::to_u32());
+        assert_eq!(3, <Log2<U15>>::to_u32());
+
+        assert_eq!(4, <Log2<U16>>::to_u32());
+        assert_eq!(4, <Log2<U17>>::to_u32());
+        assert_eq!(4, <Log2<U18>>::to_u32());
+        assert_eq!(4, <Log2<U19>>::to_u32());
+        assert_eq!(4, <Log2<U20>>::to_u32());
+        assert_eq!(4, <Log2<U21>>::to_u32());
+        assert_eq!(4, <Log2<U22>>::to_u32());
+        assert_eq!(4, <Log2<U23>>::to_u32());
+        assert_eq!(4, <Log2<U24>>::to_u32());
+        assert_eq!(4, <Log2<U25>>::to_u32());
+        assert_eq!(4, <Log2<U26>>::to_u32());
+        assert_eq!(4, <Log2<U27>>::to_u32());
+        assert_eq!(4, <Log2<U28>>::to_u32());
+        assert_eq!(4, <Log2<U29>>::to_u32());
+        assert_eq!(4, <Log2<U30>>::to_u32());
+        assert_eq!(4, <Log2<U31>>::to_u32());
+
+        assert_eq!(5, <Log2<U32>>::to_u32());
+        assert_eq!(5, <Log2<U33>>::to_u32());
+
+        // ...
+    }
+
+    #[test]
+    fn uint_toint_test() {
+        // i8
+        assert_eq!(0_i8, U0::to_int());
+        assert_eq!(1_i8, U1::to_int());
+        assert_eq!(2_i8, U2::to_int());
+        assert_eq!(3_i8, U3::to_int());
+        assert_eq!(4_i8, U4::to_int());
+
+        // i16
+        assert_eq!(0_i16, U0::to_int());
+        assert_eq!(1_i16, U1::to_int());
+        assert_eq!(2_i16, U2::to_int());
+        assert_eq!(3_i16, U3::to_int());
+        assert_eq!(4_i16, U4::to_int());
+
+        // i32
+        assert_eq!(0_i32, U0::to_int());
+        assert_eq!(1_i32, U1::to_int());
+        assert_eq!(2_i32, U2::to_int());
+        assert_eq!(3_i32, U3::to_int());
+        assert_eq!(4_i32, U4::to_int());
+
+        // i64
+        assert_eq!(0_i64, U0::to_int());
+        assert_eq!(1_i64, U1::to_int());
+        assert_eq!(2_i64, U2::to_int());
+        assert_eq!(3_i64, U3::to_int());
+        assert_eq!(4_i64, U4::to_int());
+
+        // u8
+        assert_eq!(0_u8, U0::to_int());
+        assert_eq!(1_u8, U1::to_int());
+        assert_eq!(2_u8, U2::to_int());
+        assert_eq!(3_u8, U3::to_int());
+        assert_eq!(4_u8, U4::to_int());
+
+        // u16
+        assert_eq!(0_u16, U0::to_int());
+        assert_eq!(1_u16, U1::to_int());
+        assert_eq!(2_u16, U2::to_int());
+        assert_eq!(3_u16, U3::to_int());
+        assert_eq!(4_u16, U4::to_int());
+
+        // u32
+        assert_eq!(0_u32, U0::to_int());
+        assert_eq!(1_u32, U1::to_int());
+        assert_eq!(2_u32, U2::to_int());
+        assert_eq!(3_u32, U3::to_int());
+        assert_eq!(4_u32, U4::to_int());
+
+        // u64
+        assert_eq!(0_u64, U0::to_int());
+        assert_eq!(1_u64, U1::to_int());
+        assert_eq!(2_u64, U2::to_int());
+        assert_eq!(3_u64, U3::to_int());
+        assert_eq!(4_u64, U4::to_int());
+
+        // usize
+        assert_eq!(0_usize, U0::to_int());
+        assert_eq!(1_usize, U1::to_int());
+        assert_eq!(2_usize, U2::to_int());
+        assert_eq!(3_usize, U3::to_int());
+        assert_eq!(4_usize, U4::to_int());
+    }
 }