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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use super::RawSlab;
use super::RawSlabKey;
use super::SlabIndexT;
use crossbeam_channel::{Receiver, Sender};
use std::sync::Arc;

pub struct DropSlabKeyInner<T> {
    raw_slab_key: RawSlabKey<T>,
    drop_tx: Sender<RawSlabKey<T>>,
}

impl<T: Sized> Drop for DropSlabKeyInner<T> {
    fn drop(&mut self) {
        // Not a problem if the rx closed, it would have destroyed the contained objects
        let _ = self.drop_tx.send(self.raw_slab_key);
    }
}

pub struct DropSlabKey<T> {
    inner: Arc<DropSlabKeyInner<T>>,
}

impl<T: Sized> Clone for DropSlabKey<T> {
    fn clone(&self) -> DropSlabKey<T> {
        DropSlabKey {
            inner: self.inner.clone(),
        }
    }
}

// Ideally we don't need this, would need to decide if drop_tx should be included
/*
impl<T: Sized> PartialEq for DropSlabKey<T> {
    fn eq(
        &self,
        other: &Self,
    ) -> bool {
        self.raw_slab_key.index() == other.raw_slab_key.index()
    }
}

impl<T: Sized> Eq for DropSlabKey<T> {}

impl<T: Sized> Hash for DropSlabKey<T> {
    fn hash<H: Hasher>(
        &self,
        state: &mut H,
    ) {
        self.raw_slab_key.hash(state);
    }
}
*/

impl<T: Sized> std::fmt::Debug for DropSlabKey<T> {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        f.debug_struct("RawSlabKey")
            .field("index", &self.inner.raw_slab_key.index())
            .finish()
    }
}

impl<T: Sized> DropSlabKey<T> {
    pub fn new(
        raw_slab_key: RawSlabKey<T>,
        drop_tx: Sender<RawSlabKey<T>>,
    ) -> Self {
        let inner = DropSlabKeyInner {
            raw_slab_key,
            drop_tx,
        };

        DropSlabKey {
            inner: Arc::new(inner),
        }
    }

    pub fn index(&self) -> SlabIndexT {
        self.inner.raw_slab_key.index()
    }
}

/// Wraps a RawSlab with reference counting handles. When the handle is dropped it sends a message
/// to the slab. We process these messages to remove old elements
pub struct DropSlab<T> {
    raw_slab: RawSlab<T>,
    drop_tx: Sender<RawSlabKey<T>>,
    drop_rx: Receiver<RawSlabKey<T>>,
}

impl<T> Default for DropSlab<T> {
    fn default() -> Self {
        Self::with_capacity(32)
    }
}

impl<T> DropSlab<T> {
    /// Create an empty RawSlab
    pub fn new() -> Self {
        Default::default()
    }

    /// Create an empty but presized RawSlab
    pub fn with_capacity(capacity: SlabIndexT) -> Self {
        let (drop_tx, drop_rx) = crossbeam_channel::unbounded();
        DropSlab {
            raw_slab: RawSlab::with_capacity(capacity),
            drop_tx,
            drop_rx,
        }
    }

    pub fn process_drops(&mut self) {
        for slab_key in self.drop_rx.try_iter() {
            self.raw_slab.free(slab_key);
        }
    }

    pub fn allocate(
        &mut self,
        value: T,
    ) -> DropSlabKey<T> {
        let slab_key = self.raw_slab.allocate(value);
        DropSlabKey::new(slab_key, self.drop_tx.clone())
    }

    pub fn get(
        &self,
        slab_key: &DropSlabKey<T>,
    ) -> Option<&T> {
        self.raw_slab.get(slab_key.inner.raw_slab_key)
    }

    pub fn get_raw(
        &self,
        raw_slab_key: RawSlabKey<T>,
    ) -> Option<&T> {
        self.raw_slab.get(raw_slab_key)
    }

    pub fn get_mut(
        &mut self,
        slab_key: &DropSlabKey<T>,
    ) -> Option<&mut T> {
        self.raw_slab.get_mut(slab_key.inner.raw_slab_key)
    }

    pub fn get_raw_mut(
        &mut self,
        raw_slab_key: RawSlabKey<T>,
    ) -> Option<&mut T> {
        self.raw_slab.get_mut(raw_slab_key)
    }

    pub fn iter_values(&self) -> impl Iterator<Item = &T> {
        self.raw_slab.iter().map(move |(_, value)| value)
    }

    pub fn iter_values_mut(&mut self) -> impl Iterator<Item = &mut T> {
        self.raw_slab.iter_mut().map(move |(_, value)| value)
    }

    // Have not needed these yet
    /*
    pub fn iter(&self) -> impl Iterator<Item = (DropSlabKey<T>, &T)> {
        let drop_tx = self.drop_tx.clone();
        self.raw_slab.iter().map(move |(key, value)| {
            (
                DropSlabKey::new(key, drop_tx.clone()),
                value
            )
        })
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = (DropSlabKey<T>, &mut T)> {
        let drop_tx = self.drop_tx.clone();
        self.raw_slab.iter_mut().map(move |(key, value)| {
            (
                DropSlabKey::new(key, drop_tx.clone()),
                value
            )
        })
    }

    pub fn iter_raw(&self) -> impl Iterator<Item = (RawSlabKey<T>, &T)> {
        self.raw_slab.iter()
    }

    pub fn iter_raw_mut(&mut self) -> impl Iterator<Item = (RawSlabKey<T>, &mut T)> {
        self.raw_slab.iter_mut()
    }
    */

    pub fn allocated_count(&self) -> usize {
        self.raw_slab.allocated_count()
    }

    pub fn storage_size(&self) -> usize {
        self.raw_slab.storage_size()
    }
}