tlid 0.2.2

Thread Local ID generator by predefined range without atomics/locks/random/time
Documentation
use crate::{pool::Pool, IdAble};
use core::marker::PhantomData;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub trait Behavior: Sized {
    type Id: IdAble;
    type Return;
    fn next(pool: &mut Pool<Self>) -> Self::Return;
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
pub struct Checked<Id: IdAble> {
    _p: PhantomData<Id>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
pub struct UnChecked<Id: IdAble> {
    _p: PhantomData<Id>,
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
pub struct Wrapping<Id: IdAble> {
    _p: PhantomData<Id>,
}

impl<Id: IdAble> Behavior for Checked<Id> {
    type Id = Id;
    type Return = Result<Id, CheckedError>;

    /// checked `::next()` method, id is always unique
    fn next(pool: &mut Pool<Self>) -> Self::Return {
        if pool.buffer.start < pool.buffer.end {
            let result = pool.buffer.start;
            pool.buffer.start += pool.one;
            Ok(result)
        } else {
            Err(CheckedError::PoolEmpty)
        }
    }
}

impl<Id: IdAble> Behavior for UnChecked<Id> {
    type Id = Id;
    type Return = Id;

    /// unchecked `::next()` method, no checks and gurantees given
    fn next(pool: &mut Pool<Self>) -> Self::Return {
        let result = pool.buffer.start;
        pool.buffer.start += pool.one;
        result
    }
}

impl<Id: IdAble> Behavior for Wrapping<Id> {
    type Id = Id;
    type Return = Id;

    /// wrapping `::next()` method, when out of range, wrapover to first element
    /// WARNING: due to optimizations this does not behave correctly on Pools
    /// which are created with an empty range
    fn next(pool: &mut Pool<Self>) -> Self::Return {
        if pool.buffer.start < pool.buffer.end {
            let result = pool.buffer.start;
            pool.buffer.start += pool.one;
            result
        } else {
            pool.buffer.start = pool.original_range.start + pool.one;
            pool.original_range.start
        }
    }
}

// ERROR TYPES //

///Error Type returned by Checked
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq)]
pub enum CheckedError {
    PoolEmpty,
}

impl core::fmt::Display for CheckedError {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        match self {
            CheckedError::PoolEmpty => write!(f, "pool is empty, all Id's are used up"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for CheckedError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        // Generic error, underlying cause isn't tracked.
        None
    }
}