arbitrary_int/
v1_number_compat.rs

1use crate::traits::{Integer, UnsignedInteger};
2use crate::TryNewError;
3use core::fmt::Debug;
4
5/// Compatibility with arbitrary-int 1.x, which didn't support signed integers.
6///
7/// Going forward, use [`UnsignedInteger`] (to allow only unsigned integers) or [`Integer`] (to
8/// support either signed or unsigned).
9///
10/// It is suggested to import via `use arbitrary_int::prelude::*` as `use arbitrary_int::*` will
11/// pull in this trait as well, which causes clashes with `Integer`.
12#[deprecated(
13    since = "2.0.0",
14    note = "Use [`UnsignedInteger`] or [`Integer`] instead. Suggested to import via `use arbitrary_int::prelude::*`."
15)]
16pub trait Number: UnsignedInteger<UnderlyingType = <Self as Number>::UnderlyingType> {
17    type UnderlyingType: Integer
18        + Debug
19        + From<u8>
20        + TryFrom<u16>
21        + TryFrom<u32>
22        + TryFrom<u64>
23        + TryFrom<u128>;
24
25    /// Number of bits that can fit in this type
26    const BITS: usize = <Self as Integer>::BITS;
27
28    /// Minimum value that can be represented by this type
29    const MIN: Self = <Self as Integer>::MIN;
30
31    /// Maximum value that can be represented by this type
32    const MAX: Self = <Self as Integer>::MAX;
33
34    /// Creates a number from the given value, throwing an error if the value is too large.
35    /// This constructor is useful when creating a value from a literal.
36    #[inline]
37    fn new(value: <Self as Number>::UnderlyingType) -> Self {
38        Integer::new(value)
39    }
40
41    /// Creates a number from the given value, return None if the value is too large
42    #[inline]
43    fn try_new(value: <Self as Number>::UnderlyingType) -> Result<Self, TryNewError> {
44        Integer::try_new(value)
45    }
46
47    #[inline]
48    fn value(self) -> <Self as Number>::UnderlyingType {
49        Integer::value(self)
50    }
51
52    /// Creates a number from the given value, throwing an error if the value is too large.
53    /// This constructor is useful when the value is convertible to T. Use [`Self::new`] for literals.
54    #[cfg(not(feature = "const_convert_and_const_trait_impl"))]
55    #[inline]
56    fn from_<T: Number>(value: T) -> Self {
57        Integer::from_(value)
58    }
59
60    /// Creates an instance from the given `value`. Unlike the various `new...` functions, this
61    /// will never fail as the value is masked to the result size.
62    #[cfg(not(feature = "const_convert_and_const_trait_impl"))]
63    #[inline]
64    fn masked_new<T: Number>(value: T) -> Self {
65        Integer::masked_new(value)
66    }
67
68    #[inline]
69    fn as_u8(&self) -> u8 {
70        Integer::as_u8(*self)
71    }
72
73    #[inline]
74    fn as_u16(&self) -> u16 {
75        Integer::as_u16(*self)
76    }
77
78    #[inline]
79    fn as_u32(&self) -> u32 {
80        Integer::as_u32(*self)
81    }
82
83    #[inline]
84    fn as_u64(&self) -> u64 {
85        Integer::as_u64(*self)
86    }
87
88    #[inline]
89    fn as_u128(&self) -> u128 {
90        Integer::as_u128(*self)
91    }
92
93    #[inline]
94    fn as_usize(&self) -> usize {
95        Integer::as_usize(*self)
96    }
97
98    #[cfg(not(feature = "const_convert_and_const_trait_impl"))]
99    #[inline]
100    fn as_<T: Number>(self) -> T {
101        Integer::as_(self)
102    }
103}