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
use crate::{Error, ErrorExt};
use std::any::Any;
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
pub struct Table(RwLock<Inner>);
struct Inner {
map: HashMap<u32, Arc<dyn Any + Send + Sync>>,
next_key: u32,
}
impl Table {
pub fn new() -> Self {
Table(RwLock::new(Inner {
map: HashMap::new(),
next_key: 3, }))
}
pub fn insert_at<T: Any + Send + Sync>(&self, key: u32, a: Arc<T>) {
self.0.write().unwrap().map.insert(key, a);
}
pub fn push<T: Any + Send + Sync>(&self, a: Arc<T>) -> Result<u32, Error> {
let mut inner = self.0.write().unwrap();
if inner.map.len() == u32::MAX as usize {
return Err(Error::trap(anyhow::Error::msg("table has no free keys")));
}
loop {
let key = inner.next_key;
inner.next_key += 1;
if inner.map.contains_key(&key) {
continue;
}
inner.map.insert(key, a);
return Ok(key);
}
}
pub fn contains_key(&self, key: u32) -> bool {
self.0.read().unwrap().map.contains_key(&key)
}
pub fn is<T: Any + Sized>(&self, key: u32) -> bool {
if let Some(r) = self.0.read().unwrap().map.get(&key) {
r.is::<T>()
} else {
false
}
}
pub fn get<T: Any + Send + Sync + Sized>(&self, key: u32) -> Result<Arc<T>, Error> {
if let Some(r) = self.0.read().unwrap().map.get(&key).cloned() {
r.downcast::<T>()
.map_err(|_| Error::badf().context("element is a different type"))
} else {
Err(Error::badf().context("key not in table"))
}
}
pub fn get_mut<T: Any>(&mut self, key: u32) -> Result<&mut T, Error> {
let entry = match self.0.get_mut().unwrap().map.get_mut(&key) {
Some(entry) => entry,
None => return Err(Error::badf().context("key not in table")),
};
let entry = match Arc::get_mut(entry) {
Some(entry) => entry,
None => return Err(Error::badf().context("cannot mutably borrow shared file")),
};
entry
.downcast_mut::<T>()
.ok_or_else(|| Error::badf().context("element is a different type"))
}
pub fn delete<T: Any + Send + Sync>(&self, key: u32) -> Option<Arc<T>> {
self.0
.write()
.unwrap()
.map
.remove(&key)
.map(|r| r.downcast::<T>().unwrap())
}
pub fn renumber(&self, from: u32, to: u32) -> Result<(), Error> {
let map = &mut self.0.write().unwrap().map;
let from_entry = map.remove(&from).ok_or(Error::badf())?;
map.insert(to, from_entry);
Ok(())
}
}