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
#![crate_name = "tlid"]
use core::{
    clone::Clone,
    fmt::Debug,
    ops::{Add, AddAssign, Sub},
};

mod allocator;
mod behavior;
mod pool;
pub use allocator::AllocError;
pub use behavior::{Behavior, Checked, CheckedError, UnChecked, Wrapping};
pub use pool::Pool;

#[cfg(feature = "num-traits")]
extern crate num_traits;

#[cfg(feature = "serde")] extern crate serde;

/// trait that is used to describe all types that can be used as an ID
/// types like usize, u64, i64, ... are supported by default
///
/// - Ord: is needed for keeping a BTreeMap over free ranges
/// - Copy: as IDs should be small enough to be copied everywhere
/// - Add/Sub/AddAssign: for logic to increase and check generated IDs
/// - Default: to count the number of ID's in a pool
/// - Debug: to be used in Error Types and printed
/// - From<u8> to convert 1 into the respective ID type
pub trait IdAble:
    Ord + Copy + Add<Output = Self> + Sub<Output = Self> + AddAssign + Default + Debug + From<u8>
{
    fn inc(&mut self);
    fn inc_by(&mut self, other: Self);
}

impl<X: Ord + Clone + Copy + Add<Output = X> + Sub<Output = Self> + AddAssign + Default + Debug + From<u8>> IdAble
    for X
{
    fn inc(&mut self) { *self += X::from(1); }

    fn inc_by(&mut self, other: Self) { *self += other; }
}