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
use std::collections::VecDeque;
use std::sync::Mutex;
use crate::error::CoordError;
#[derive(Debug)]
pub struct IdAllocator(Mutex<IdAllocatorInner>);
#[derive(Debug)]
struct IdAllocatorInner {
next: u32,
max: u32,
free: VecDeque<u32>,
}
impl IdAllocator {
pub fn new(min: u32, max: u32) -> IdAllocator {
IdAllocator(Mutex::new(IdAllocatorInner {
next: min,
max,
free: VecDeque::new(),
}))
}
pub fn alloc(&self) -> Result<u32, CoordError> {
let mut inner = self.0.lock().expect("lock poisoned");
if let Some(id) = inner.free.pop_front() {
Ok(id)
} else {
let id = inner.next;
if id > inner.max {
Err(CoordError::IdExhaustionError)
} else {
inner.next += 1;
Ok(id)
}
}
}
pub fn free(&self, id: u32) {
let mut inner = self.0.lock().expect("lock poisoned");
inner.free.push_back(id);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_id_alloc() {
let ida = IdAllocator::new(3, 5);
assert_eq!(ida.alloc().unwrap(), 3);
assert_eq!(ida.alloc().unwrap(), 4);
assert_eq!(ida.alloc().unwrap(), 5);
ida.free(4);
assert_eq!(ida.alloc().unwrap(), 4);
ida.free(5);
ida.free(3);
assert_eq!(ida.alloc().unwrap(), 5);
assert_eq!(ida.alloc().unwrap(), 3);
match ida.alloc() {
Ok(id) => panic!(
"id allocator returned {}, not expected id exhaution error",
id
),
Err(CoordError::IdExhaustionError) => (),
Err(e) => panic!("unexpected error {:?}", e),
}
}
}