diff -r 901751d3cd80 -r febccab419b1 rust/hedgewars-server/src/core/indexslab.rs --- a/rust/hedgewars-server/src/core/indexslab.rs Tue Jun 04 23:19:18 2019 +0300 +++ b/rust/hedgewars-server/src/core/indexslab.rs Tue Jun 04 22:34:42 2019 +0200 @@ -1,71 +1,71 @@ -use std::{ - iter, - mem::replace, - ops::{Index, IndexMut}, -}; - -pub struct IndexSlab { - data: Vec>, -} - -impl IndexSlab { - pub fn new() -> Self { - Self { data: Vec::new() } - } - - pub fn with_capacity(capacity: usize) -> Self { - Self { - data: Vec::with_capacity(capacity), - } - } - - pub fn insert(&mut self, index: usize, value: T) { - if index >= self.data.len() { - self.data.reserve(index - self.data.len() + 1); - self.data.extend((self.data.len()..index).map(|_| None)); - self.data.push(Some(value)) - } else { - self.data[index] = Some(value); - } - } - - pub fn contains(&self, index: usize) -> bool { - self.data.get(index).and_then(|x| x.as_ref()).is_some() - } - - pub fn remove(&mut self, index: usize) -> Option { - if let Some(x) = self.data.get_mut(index) { - replace(x, None) - } else { - None - } - } - - pub fn iter(&self) -> impl Iterator { - self.data - .iter() - .enumerate() - .filter_map(|(index, opt)| opt.as_ref().and_then(|x| Some((index, x)))) - } - - pub fn iter_mut(&mut self) -> impl Iterator { - self.data - .iter_mut() - .enumerate() - .filter_map(|(index, opt)| opt.as_mut().and_then(|x| Some((index, x)))) - } -} - -impl Index for IndexSlab { - type Output = T; - - fn index(&self, index: usize) -> &Self::Output { - self.data[index].as_ref().unwrap() - } -} - -impl IndexMut for IndexSlab { - fn index_mut(&mut self, index: usize) -> &mut Self::Output { - self.data[index].as_mut().unwrap() - } -} +use std::{ + iter, + mem::replace, + ops::{Index, IndexMut}, +}; + +pub struct IndexSlab { + data: Vec>, +} + +impl IndexSlab { + pub fn new() -> Self { + Self { data: Vec::new() } + } + + pub fn with_capacity(capacity: usize) -> Self { + Self { + data: Vec::with_capacity(capacity), + } + } + + pub fn insert(&mut self, index: usize, value: T) { + if index >= self.data.len() { + self.data.reserve(index - self.data.len() + 1); + self.data.extend((self.data.len()..index).map(|_| None)); + self.data.push(Some(value)) + } else { + self.data[index] = Some(value); + } + } + + pub fn contains(&self, index: usize) -> bool { + self.data.get(index).and_then(|x| x.as_ref()).is_some() + } + + pub fn remove(&mut self, index: usize) -> Option { + if let Some(x) = self.data.get_mut(index) { + replace(x, None) + } else { + None + } + } + + pub fn iter(&self) -> impl Iterator { + self.data + .iter() + .enumerate() + .filter_map(|(index, opt)| opt.as_ref().and_then(|x| Some((index, x)))) + } + + pub fn iter_mut(&mut self) -> impl Iterator { + self.data + .iter_mut() + .enumerate() + .filter_map(|(index, opt)| opt.as_mut().and_then(|x| Some((index, x)))) + } +} + +impl Index for IndexSlab { + type Output = T; + + fn index(&self, index: usize) -> &Self::Output { + self.data[index].as_ref().unwrap() + } +} + +impl IndexMut for IndexSlab { + fn index_mut(&mut self, index: usize) -> &mut Self::Output { + self.data[index].as_mut().unwrap() + } +}