#![cfg_attr(not(feature = "std"), no_std)]
#![deny(unsafe_code)]
#[cfg(feature = "alloc")]
extern crate alloc;
use core::fmt::{Debug, Display, Formatter};
use core::marker::PhantomData;
use intid::IntegerId;
#[cfg(feature = "alloc")]
mod reusing;
mod unique;
#[cfg(feature = "alloc")]
pub use self::reusing::IdAllocator;
#[cfg(feature = "atomic")]
pub use self::unique::atomic::UniqueIdAllocatorAtomic;
pub use self::unique::UniqueIdAllocator;
#[derive(Clone)]
pub struct IdExhaustedError<T: IntegerId> {
marker: PhantomData<T>,
}
impl<T: IntegerId> IdExhaustedError<T> {
#[inline]
#[cold]
#[allow(clippy::new_without_default)] #[must_use]
pub fn new() -> Self {
IdExhaustedError {
marker: PhantomData,
}
}
#[track_caller]
#[cold]
pub fn panic(self) -> ! {
panic!("{self}")
}
}
impl<T: IntegerId> Display for IdExhaustedError<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "Ran out of ids for {}", core::any::type_name::<T>())
}
}
impl<T: IntegerId> Debug for IdExhaustedError<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("IdExhaustedError")
.field("type_name", &core::any::type_name::<T>())
.finish_non_exhaustive()
}
}
#[rustversion::since(1.81)]
impl<T: IntegerId> core::error::Error for IdExhaustedError<T> {}
#[rustversion::before(1.81)]
#[cfg(feature = "std")]
impl<T: IntegerId> std::error::Error for IdExhaustedError<T> {}