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
use alloc::vec::Vec;

pub type Tid = usize;

pub struct TidAllocator {
    state: Tid,
    deallocated_tid: Vec<Tid>,
}

impl TidAllocator {
    pub fn new() -> Self {
        TidAllocator {
            state: 0,
            deallocated_tid: Vec::new(),
        }
    }

    pub fn allocate(&mut self) -> Tid {
        if let Some(tid) = self.deallocated_tid.pop() {
            tid
        } else {
            let tid = self.state;
            self.state += 1;
            tid
        }
    }

    pub fn deallocate(&mut self, tid: Tid) {
        self.deallocated_tid.push(tid);
    }
}