1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
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
    }
}