1#![cfg_attr(not(feature = "std"), no_std)]
9#![deny(unsafe_code)] #[cfg(feature = "alloc")]
12extern crate alloc;
13
14use core::fmt::{Debug, Display, Formatter};
15use core::marker::PhantomData;
16use intid::IntegerId;
17
18#[cfg(feature = "alloc")]
19mod reusing;
20mod unique;
21
22#[cfg(feature = "alloc")]
23pub use self::reusing::IdAllocator;
24#[cfg(feature = "atomic")]
25pub use self::unique::atomic::UniqueIdAllocatorAtomic;
26pub use self::unique::UniqueIdAllocator;
27
28#[derive(Clone)]
31pub struct IdExhaustedError<T: IntegerId> {
32 marker: PhantomData<T>,
33}
34impl<T: IntegerId> IdExhaustedError<T> {
35 #[inline]
38 #[cold]
39 #[allow(clippy::new_without_default)] pub fn new() -> Self {
41 IdExhaustedError {
42 marker: PhantomData,
43 }
44 }
45
46 #[track_caller]
50 #[cold]
51 pub fn panic(self) -> ! {
52 panic!("{self}")
53 }
54}
55impl<T: IntegerId> Display for IdExhaustedError<T> {
56 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
57 write!(f, "Ran out of ids for {}", core::any::type_name::<T>())
58 }
59}
60impl<T: IntegerId> Debug for IdExhaustedError<T> {
61 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
62 f.debug_struct("IdExhaustedError")
63 .field("type_name", &core::any::type_name::<T>())
64 .finish_non_exhaustive()
65 }
66}
67
68#[rustversion::since(1.81)]
69impl<T: IntegerId> core::error::Error for IdExhaustedError<T> {}
70
71#[rustversion::before(1.81)]
72#[cfg(feature = "std")]
73impl<T: IntegerId> std::error::Error for IdExhaustedError<T> {}