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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Non-thread-safe container for keeping autoincrement counter
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct AutoIncrement<T>(T);

impl<T: Incremental> AutoIncrement<T> {
    #[allow(clippy::should_implement_trait)]
    pub fn pull(&mut self) -> T {
        let next = Incremental::get_next(&self.0);
        std::mem::replace(&mut self.0, next)
    }

    pub fn init_with(initial_value: T) -> Self {
        Self(initial_value)
    }
}

impl<T: Clone> AutoIncrement<T> {
    pub fn current(&self) -> T {
        self.0.clone()
    }
}

/// Trait for implementing over non-thread-safe incremental types
pub trait Incremental: Sized {
    fn initial() -> Self;

    fn get_next(current: &Self) -> Self;

    fn init() -> AutoIncrement<Self> {
        AutoIncrement(Self::initial())
    }

    fn init_with(value: Self) -> AutoIncrement<Self> {
        AutoIncrement(value)
    }

    fn init_from(self) -> AutoIncrement<Self> {
        Self::init_with(self)
    }
}

#[cfg(test)]
mod tests {
    use crate as autoincrement;
    #[cfg(feature = "derive")]
    use autoincrement::Incremental;
    #[cfg(not(feature = "derive"))]
    use autoincrement_derive::Incremental;

    #[test]
    #[cfg(feature = "sync")]
    fn test_sync_u8() {
        #[derive(Incremental, Debug, PartialEq, Eq, Clone)]
        struct MyID(u8);

        let mut counter = MyID::init();

        assert_eq!(counter.current(), MyID(1));
        assert_eq!(counter.pull(), MyID(1));
        assert_eq!(counter.current(), MyID(2));
        assert_eq!(counter.pull(), MyID(2));
        assert_eq!(counter.current(), MyID(3));
        assert_eq!(counter.pull(), MyID(3));
    }

    #[test]
    #[cfg(feature = "sync")]
    fn test_sync_u16() {
        #[derive(Incremental, Debug, PartialEq, Eq, Clone)]
        struct MyID(u16);

        let mut counter = MyID::init();

        assert_eq!(counter.current(), MyID(1));
        assert_eq!(counter.pull(), MyID(1));
        assert_eq!(counter.current(), MyID(2));
        assert_eq!(counter.pull(), MyID(2));
        assert_eq!(counter.current(), MyID(3));
        assert_eq!(counter.pull(), MyID(3));
    }

    #[test]
    #[cfg(feature = "sync")]
    fn test_sync_u32() {
        #[derive(Incremental, Debug, PartialEq, Eq, Clone)]
        struct MyID(u32);

        let mut counter = MyID::init();

        assert_eq!(counter.current(), MyID(1));
        assert_eq!(counter.pull(), MyID(1));
        assert_eq!(counter.current(), MyID(2));
        assert_eq!(counter.pull(), MyID(2));
        assert_eq!(counter.current(), MyID(3));
        assert_eq!(counter.pull(), MyID(3));
    }

    #[test]
    #[cfg(feature = "sync")]
    fn test_sync_u64() {
        #[derive(Incremental, Debug, PartialEq, Eq, Clone)]
        struct MyID(u64);

        let mut counter = MyID::init();

        assert_eq!(counter.current(), MyID(1));
        assert_eq!(counter.pull(), MyID(1));
        assert_eq!(counter.current(), MyID(2));
        assert_eq!(counter.pull(), MyID(2));
        assert_eq!(counter.current(), MyID(3));
        assert_eq!(counter.pull(), MyID(3));
    }

    #[test]
    #[cfg(feature = "sync")]
    fn test_sync_usize() {
        #[derive(Incremental, Debug, PartialEq, Eq, Clone)]
        struct MyID(usize);

        let mut counter = MyID::init();

        assert_eq!(counter.current(), MyID(1));
        assert_eq!(counter.pull(), MyID(1));
        assert_eq!(counter.current(), MyID(2));
        assert_eq!(counter.pull(), MyID(2));
        assert_eq!(counter.current(), MyID(3));
        assert_eq!(counter.pull(), MyID(3));
    }
}