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
use std::marker::PhantomData;

use std::collections::VecDeque;

/// Simple implementation of a store that manages a recycling pool of u16 keys
pub struct KeyGenerator<K: From<u16> + Into<u16> + Copy> {
    recycled_local_keys: VecDeque<u16>,
    next_new_local_key: u16,
    phantom: PhantomData<K>,
}

impl<K: From<u16> + Into<u16> + Copy> KeyGenerator<K> {
    pub fn new() -> Self {
        Self {
            recycled_local_keys: VecDeque::new(),
            next_new_local_key: 0,
            phantom: PhantomData,
        }
    }
    /// Get a new, unused key
    pub fn generate(&mut self) -> K {
        if let Some(local_key) = self.recycled_local_keys.pop_front() {
            return K::from(local_key);
        }

        let output = self.next_new_local_key;
        self.next_new_local_key += 1;
        K::from(output)
    }

    /// Recycle a used key, freeing it up
    pub fn recycle_key(&mut self, local_key: &K) {
        let local_key_u16: u16 = Into::<u16>::into(*local_key);
        self.recycled_local_keys.push_back(local_key_u16);
    }
}