async_ecs/storage/
drain.rs

1use hibitset::BitSet;
2
3use crate::{component::Component, entity::Index, join::Join};
4
5use super::MaskedStorage;
6
7/// A draining storage wrapper which has a `Join` implementation
8/// that removes the components.
9pub struct Drain<'a, T: Component> {
10    /// The masked storage
11    pub data: &'a mut MaskedStorage<T>,
12}
13
14impl<'a, T> Join for Drain<'a, T>
15where
16    T: Component,
17{
18    type Mask = BitSet;
19    type Type = T;
20    type Value = &'a mut MaskedStorage<T>;
21
22    unsafe fn open(self) -> (Self::Mask, Self::Value) {
23        let mask = self.data.mask().clone();
24
25        (mask, self.data)
26    }
27
28    unsafe fn get(value: &mut Self::Value, id: Index) -> T {
29        value.remove(id).expect("Tried to access same index twice")
30    }
31}