|
|
use std::{ |
|
|
any::type_name, |
|
|
marker::PhantomData, |
|
|
num::NonZeroU64, |
|
|
sync::atomic::{AtomicU64, Ordering}, |
|
|
}; |
|
|
|
|
|
use concurrent_queue::ConcurrentQueue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct IdFactory<T> { |
|
|
|
|
|
|
|
|
counter: AtomicU64, |
|
|
|
|
|
max_count: u64, |
|
|
id_offset: u64, |
|
|
_phantom_data: PhantomData<T>, |
|
|
} |
|
|
|
|
|
impl<T> IdFactory<T> { |
|
|
|
|
|
pub fn new(start: T, max: T) -> Self |
|
|
where |
|
|
T: Into<NonZeroU64> + Ord, |
|
|
{ |
|
|
Self::new_const(start.into(), max.into()) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub const fn new_const(start: NonZeroU64, max: NonZeroU64) -> Self { |
|
|
assert!(start.get() < max.get()); |
|
|
Self { |
|
|
|
|
|
|
|
|
counter: AtomicU64::new(0), |
|
|
max_count: max.get() - start.get(), |
|
|
id_offset: start.get(), |
|
|
_phantom_data: PhantomData, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> IdFactory<T> |
|
|
where |
|
|
T: TryFrom<NonZeroU64>, |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
pub fn get(&self) -> T { |
|
|
let count = self.counter.fetch_add(1, Ordering::Relaxed); |
|
|
|
|
|
#[cfg(debug_assertions)] |
|
|
{ |
|
|
if count == u64::MAX { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::process::abort() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if count > self.max_count { |
|
|
panic!( |
|
|
"Max id limit (overflow) hit while attempting to generate a unique {}", |
|
|
type_name::<T>(), |
|
|
) |
|
|
} |
|
|
|
|
|
let new_id_u64 = count + self.id_offset; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let new_id = unsafe { NonZeroU64::new_unchecked(new_id_u64) }; |
|
|
|
|
|
match new_id.try_into() { |
|
|
Ok(id) => id, |
|
|
|
|
|
|
|
|
Err(_) => panic!( |
|
|
"Failed to convert NonZeroU64 value of {} into {}", |
|
|
new_id, |
|
|
type_name::<T>() |
|
|
), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn wrapping_get(&self) -> T { |
|
|
let count = self.counter.fetch_add(1, Ordering::Relaxed); |
|
|
|
|
|
let new_id_u64 = (count % self.max_count) + self.id_offset; |
|
|
|
|
|
|
|
|
|
|
|
let new_id = unsafe { NonZeroU64::new_unchecked(new_id_u64) }; |
|
|
|
|
|
match new_id.try_into() { |
|
|
Ok(id) => id, |
|
|
Err(_) => panic!( |
|
|
"Failed to convert NonZeroU64 value of {} into {}", |
|
|
new_id, |
|
|
type_name::<T>() |
|
|
), |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct IdFactoryWithReuse<T> { |
|
|
factory: IdFactory<T>, |
|
|
free_ids: ConcurrentQueue<T>, |
|
|
} |
|
|
|
|
|
impl<T> IdFactoryWithReuse<T> |
|
|
where |
|
|
T: Into<NonZeroU64> + Ord, |
|
|
{ |
|
|
|
|
|
pub fn new(start: T, max: T) -> Self { |
|
|
Self { |
|
|
factory: IdFactory::new(start, max), |
|
|
free_ids: ConcurrentQueue::unbounded(), |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub const fn new_const(start: NonZeroU64, max: NonZeroU64) -> Self { |
|
|
Self { |
|
|
factory: IdFactory::new_const(start, max), |
|
|
free_ids: ConcurrentQueue::unbounded(), |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl<T> IdFactoryWithReuse<T> |
|
|
where |
|
|
T: TryFrom<NonZeroU64>, |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
pub fn get(&self) -> T { |
|
|
self.free_ids.pop().unwrap_or_else(|_| self.factory.get()) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub unsafe fn reuse(&self, id: T) { |
|
|
let _ = self.free_ids.push(id); |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(test)] |
|
|
mod tests { |
|
|
use std::num::NonZeroU8; |
|
|
|
|
|
use super::*; |
|
|
|
|
|
#[test] |
|
|
#[should_panic(expected = "Max id limit (overflow)")] |
|
|
fn test_overflow_detection() { |
|
|
let factory = IdFactory::new(NonZeroU8::MIN, NonZeroU8::MAX); |
|
|
assert_eq!(factory.get(), NonZeroU8::new(1).unwrap()); |
|
|
assert_eq!(factory.get(), NonZeroU8::new(2).unwrap()); |
|
|
for _ in 2..256 { |
|
|
factory.get(); |
|
|
} |
|
|
} |
|
|
|
|
|
#[test] |
|
|
#[should_panic(expected = "Max id limit (overflow)")] |
|
|
fn test_overflow_detection_near_u64_max() { |
|
|
let factory = IdFactory::new(NonZeroU64::try_from(u64::MAX - 5).unwrap(), NonZeroU64::MAX); |
|
|
for _ in 0..=6 { |
|
|
factory.get(); |
|
|
} |
|
|
} |
|
|
} |
|
|
|