Skip to main content

intid_core/
uint.rs

1//! Re-exports [`primint::UnsignedPrimInt`] and various module level functions.
2//!
3//! Exists primarily for compatibility with older versions of this crate,
4//! before [`primint`] was its own crate.
5//!
6//! It will be removed in the next semver-breaking release.
7
8/// A trait alias for [`primint::UnsignedPrimInt`].
9///
10/// This is logically a re-export,
11/// but a wrapper function is used instead as re-exports can't currently be deprecated ([rust-lang/rust#30827])
12///
13/// Since trait aliases are not usable on stable rust,
14/// we use a blanket impl and trait bound instead.
15///
16/// This trait alias will be removed in the next semver-breaking release.
17///
18/// [rust-lang/rust#30827]: https://github.com/rust-lang/rust/issues/30827
19#[deprecated(note = "Use primint::UnsignedPrimInt directly")]
20pub trait UnsignedPrimInt: primint::UnsignedPrimInt {}
21#[allow(deprecated)]
22impl<T: primint::UnsignedPrimInt> UnsignedPrimInt for T {}
23
24/// A type alias for [`primint::fmt::DebugDesc`].
25///
26/// This is logically a re-export,
27/// but a type alias is used instead as re-exports can't currently be deprecated ([rust-lang/rust#30827])
28///
29/// This type alias will be removed in the next semver-breaking release.
30///
31/// [rust-lang/rust#30827]: https://github.com/rust-lang/rust/issues/30827
32#[deprecated(note = "Use primint::fmt::DebugDesc directly")]
33pub type DebugDesc<T> = primint::fmt::DebugDesc<T>;
34
35macro_rules! deprecated_shim_fn {
36    (@add_attrs $name:ident => $target:path {$decl:item}) => {
37        #[doc = concat!("An alias for [`", stringify!($target), "`].")]
38        ///
39        /// This is logically a re-export,
40        /// but a wrapper function is used instead as re-exports can't currently be deprecated ([rust-lang/rust#30827])
41        ///
42        /// This function will be removed in the next semver-breaking release.
43        ///
44        /// [rust-lang/rust#30827]: https://github.com/rust-lang/rust/issues/30827
45        #[deprecated = concat!("Use ", stringify!($target), " directly.")]
46        #[inline] // just a wrapper
47        #[allow(deprecated)]
48        $decl
49    };
50    (@determine_target $name:ident $target:path) => ($target);
51    (@determine_target $name:ident) => (primint::$name);
52    ($($({$x:ident})? fn $name:ident $(<$($param:ident: $bound:ident),+>)? ($($arg:ident: $argty:ty),*) $(-> $ret:ty)?;)+) => {
53        $(deprecated_shim_fn!(@add_attrs $name => primint::$name {
54            pub $($x)? fn $name$(<$($param: $bound),*>)?($($arg: $argty,)*) $(-> $ret)? {
55                primint::$name $(::<$($param, )*>)?($($arg),*)
56            }
57        });)*
58    };
59}
60deprecated_shim_fn! {
61    {const} fn bits<T: UnsignedPrimInt>() -> u32;
62    fn checked_add<T: UnsignedPrimInt>(left: T, right: T) -> Option<T>;
63    fn checked_cast<T: UnsignedPrimInt, U: UnsignedPrimInt>(value: T) -> Option<U>;
64    fn checked_sub<T: UnsignedPrimInt>(left: T, right: T) -> Option<T>;
65    fn count_ones<T: UnsignedPrimInt>(value: T) -> u32;
66    fn from_usize_checked<T: UnsignedPrimInt>(value: usize) -> Option<T>;
67    fn from_usize_wrapping<T: UnsignedPrimInt>(value: usize) -> T;
68    fn leading_zeros<T: UnsignedPrimInt>(value: T) -> u32;
69    {const} fn max_value<T: UnsignedPrimInt>() -> T;
70    {const} fn one<T: UnsignedPrimInt>() -> T;
71    fn to_usize_checked<T: UnsignedPrimInt>(value: T) -> Option<usize>;
72    fn to_usize_wrapping<T: UnsignedPrimInt>(value: T) -> usize;
73    fn trailing_zeros<T: UnsignedPrimInt>(value: T) -> u32;
74    {const} fn zero<T: UnsignedPrimInt>() -> T;
75}
76deprecated_shim_fn! {
77    @add_attrs debug_desc => primint::fmt::debug_desc {
78        pub fn debug_desc<T: UnsignedPrimInt>(value: T) -> DebugDesc<T> {
79            primint::fmt::debug_desc(value)
80        }
81    }
82}
83
84/// Panic with a message indicating that an ID is not valid.
85///
86/// Used to implement the panic in [`crate::IntegerId::from_int`].
87///
88/// This is the only functionality in this crate not re-exported from [`primint`].
89#[inline(never)]
90#[track_caller]
91#[cold]
92pub(crate) fn invalid_id<T: primint::UnsignedPrimInt>(id: T) -> ! {
93    panic!("Invalid id: {}", primint::fmt::debug_desc(id))
94}