rust/hedgewars-server/src/server/indexslab.rs
changeset 14693 6a2e13e36b7f
parent 14692 e5415faa117b
equal deleted inserted replaced
14692:e5415faa117b 14693:6a2e13e36b7f
     1 use std::{
     1 use std::{
     2     iter,
     2     iter,
       
     3     mem::replace,
     3     ops::{Index, IndexMut},
     4     ops::{Index, IndexMut},
     4 };
     5 };
     5 
     6 
     6 pub struct IndexSlab<T> {
     7 pub struct IndexSlab<T> {
     7     data: Vec<Option<T>>,
     8     data: Vec<Option<T>>,
    30 
    31 
    31     pub fn contains(&self, index: usize) -> bool {
    32     pub fn contains(&self, index: usize) -> bool {
    32         self.data.get(index).and_then(|x| x.as_ref()).is_some()
    33         self.data.get(index).and_then(|x| x.as_ref()).is_some()
    33     }
    34     }
    34 
    35 
    35     pub fn remove(&mut self, index: usize) {
    36     pub fn remove(&mut self, index: usize) -> Option<T> {
    36         if let Some(x) = self.data.get_mut(index) {
    37         if let Some(x) = self.data.get_mut(index) {
    37             *x = None
    38             replace(x, None)
       
    39         } else {
       
    40             None
    38         }
    41         }
    39     }
    42     }
    40 
    43 
    41     pub fn iter(&self) -> impl Iterator<Item = (usize, &T)> {
    44     pub fn iter(&self) -> impl Iterator<Item = (usize, &T)> {
    42         self.data
    45         self.data