Insert a resource at the next available index.
(&self, a: Arc<T>)
| 33 | |
| 34 | /// Insert a resource at the next available index. |
| 35 | pub fn push<T: Any + Send + Sync>(&self, a: Arc<T>) -> Result<u32, Error> { |
| 36 | let mut inner = self.0.write().unwrap(); |
| 37 | // NOTE: The performance of this new key calculation could be very bad once keys wrap |
| 38 | // around. |
| 39 | if inner.map.len() == u32::MAX as usize { |
| 40 | return Err(Error::trap(EnvError::msg("table has no free keys"))); |
| 41 | } |
| 42 | loop { |
| 43 | let key = inner.next_key; |
| 44 | inner.next_key += 1; |
| 45 | if inner.map.contains_key(&key) { |
| 46 | continue; |
| 47 | } |
| 48 | inner.map.insert(key, a); |
| 49 | return Ok(key); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// Check if the table has a resource at the given index. |
| 54 | pub fn contains_key(&self, key: u32) -> bool { |