use serde::Deserialize;
use serde::Serialize;
use std::cmp::Ordering;
use std::fmt::Debug;
use std::fmt::Display;
type GenerationIdxType = u32;
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
#[repr(transparent)]
#[derive(::rkyv::Archive, ::rkyv::Serialize, ::rkyv::Deserialize)]
#[archive(check_bytes)]
pub struct GenerationIdx(GenerationIdxType);
impl GenerationIdx {
pub fn checked_add(self, other: Self) -> Option<Self> {
self.0.checked_add(other.0).map(Self)
}
pub fn checked_sub(self, other: Self) -> Option<Self> {
self.0.checked_sub(other.0).map(Self)
}
pub fn next(self) -> Self {
Self::from(self.0 as usize + 1)
}
pub fn prev(self) -> Self {
Self::from(self.0 as usize - 1)
}
pub fn stub() -> Self {
const GENERATION_STUB: GenerationIdxType = 0xCAFEBABE;
Self(GENERATION_STUB)
}
}
impl PartialOrd<usize> for GenerationIdx {
fn partial_cmp(&self, other: &usize) -> Option<Ordering> {
let self_as_usize: usize = (*self).into();
self_as_usize.partial_cmp(other)
}
}
impl PartialEq<usize> for GenerationIdx {
fn eq(&self, other: &usize) -> bool {
let self_as_usize: usize = (*self).into();
self_as_usize == *other
}
}
impl From<usize> for GenerationIdx {
fn from(value: usize) -> Self {
GenerationIdx(value as u32)
}
}
impl From<GenerationIdx> for usize {
fn from(value: GenerationIdx) -> Self {
value.0 as usize
}
}
impl Debug for GenerationIdx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl Display for GenerationIdx {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}