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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//! A fixed-size free-list with optional key lifetime safety and macroless unique typing.
#![doc(html_root_url = "https://docs.rs/fixed_free_list")]
#![crate_name = "fixed_free_list"]
#![warn(
    missing_debug_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unused_lifetimes,
    unused_import_braces,
    clippy::shadow_unrelated
)]
#![deny(missing_docs, unaligned_references, unsafe_op_in_unsafe_fn)]
#![cfg_attr(all(nightly, feature = "unstable"), feature(maybe_uninit_uninit_array))]

use std::{
    fmt::{Debug, Formatter, Result},
    marker::PhantomData,
    mem::{self, ManuallyDrop, MaybeUninit},
};

union Block<T> {
    value: ManuallyDrop<T>,
    next: usize,
}

/// A fixed-size free-list.
///
/// # Time Complexity
///
/// All operations are worst case O(1) unless noted otherwise
///
/// # Examples
///
/// ```
/// # use fixed_free_list::*;
/// let mut list: FixedFreeList<i32, 16> = FixedFreeList::new();
/// let key1 = list.alloc(8).unwrap();
/// let key2 = list.alloc(5).unwrap();
/// assert_eq!(unsafe { *list.get_unchecked(key1) }, 8);
/// assert_eq!(unsafe { *list.get_unchecked(key2) }, 5);
/// let value = unsafe { list.get_mut_unchecked(key1) };
/// *value = 2;
/// assert_eq!(unsafe { list.free_unchecked(key1) }, 2);
/// assert!(list.is_free(key1));
/// assert_eq!(list.size_hint(), 2);
/// let key3 = list.alloc(7).unwrap();
/// assert_eq!(list.size_hint(), 2);
/// list.clear();
/// assert!(list.is_free(key3));
/// ```
pub struct FixedFreeList<T, const N: usize> {
    next: usize,
    high: usize,
    data: [MaybeUninit<Block<T>>; N],
}

impl<T, const N: usize> FixedFreeList<T, N> {
    /// Creates a new empty `FixedFreeList`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list: FixedFreeList<i32, 16> = FixedFreeList::new();
    /// ```
    #[inline(always)]
    pub fn new() -> Self {
        Self {
            next: N,
            high: 0,
            #[cfg(all(nightly, feature = "unstable"))]
            data: MaybeUninit::uninit_array(),
            #[cfg(not(all(nightly, feature = "unstable")))]
            data: unsafe { MaybeUninit::uninit().assume_init() },
        }
    }

    /// If there is space, adds `value` to the free list and returns its key.
    /// If there is no space, Drops `value` and returns `None`.
    ///
    /// # Returns
    ///
    /// `None` if the list was already full.
    /// Note: `value` is dropped in this case. Check [`is_full`] beforehand to avoid this if desired.
    ///
    /// `Some(key)` if there was spare capacity to accommodate `value`.
    /// `key` can now be used to access `value` via [`get_unchecked`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list: FixedFreeList<i32, 16> = FixedFreeList::new();
    /// list.alloc(1);
    /// ```
    pub fn alloc(&mut self, value: T) -> Option<usize> {
        let key;
        if self.next < N {
            // Use a previously used but now free space
            key = self.next;
            // Update `next` to point at the next free space now that the current one will be used
            // # Safety
            // This space is guaranteed to be free, because otherwise `next` wouldn't point at it.
            self.next = unsafe { self.data.get_unchecked(key).assume_init_ref().next };
        } else {
            if self.high >= N {
                // Drops `value`
                return None;
            }
            // Use a fresh uninitialized space
            key = self.high;
            // Bump high-water mark
            self.high += 1;
        };
        // Dropping is unneccessary here because `data[key]` is either `usize` or `MaybeUninit<T>::uninit()`
        unsafe {
            *self.data.get_unchecked_mut(key) = MaybeUninit::new(Block {
                value: ManuallyDrop::new(value),
            });
        }
        Some(key)
    }

    /// Solely intended for verification purposes.
    /// If your algorithm needs this you're probably doing something wrong.
    ///
    /// # Time Complexity
    ///
    /// Worst case O(N)
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list: FixedFreeList<i32, 16> = FixedFreeList::new();
    /// assert_eq!(list.is_free(0), true);
    /// let key = list.alloc(1).unwrap();
    /// assert_eq!(list.is_free(0), false);
    /// unsafe { list.free_unchecked(key); }
    /// assert_eq!(list.is_free(0), true);
    /// ```
    pub fn is_free(&self, key: usize) -> bool {
        if key >= self.high {
            return true;
        }
        let mut next = self.next;
        while next < N {
            if next == key {
                return true;
            }
            next = unsafe { self.data.get_unchecked(next).assume_init_ref().next };
        }
        false
    }

    /// Frees the space occupied by the value at `key` and returns the value.
    ///
    /// # Returns
    ///
    /// The value at `key`.
    ///
    /// # Safety
    ///
    /// `key` must have originated from calling [`alloc`] on the same instance
    /// and the space must not already been freed since the [`alloc`] call.
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list: FixedFreeList<i32, 16> = FixedFreeList::new();
    /// let key = list.alloc(1).unwrap();
    /// let value = unsafe { list.free_unchecked(key) };
    /// assert_eq!(value, 1);
    /// ```
    #[inline(always)]
    pub unsafe fn free_unchecked(&mut self, key: usize) -> T {
        #[cfg(all(feature = "strict"))]
        assert!(!self.is_free(key));

        let value = mem::replace(
            unsafe { self.data.get_unchecked_mut(key) },
            MaybeUninit::new(Block { next: self.next }),
        );

        self.next = key;

        // # Safety
        // Function invariants imply the space is occupied by an initialized value
        ManuallyDrop::into_inner(unsafe { value.assume_init().value })
    }

    /// Provides immutable access to the value at `key`.
    ///
    /// # Returns
    ///
    /// An immutable borrow of the value at `key`.
    ///
    /// # Safety
    ///
    /// `key` must have originated from calling [`alloc`] on the same instance
    /// and the space must not already been freed since the [`alloc`] call.
    ///
    /// There must be no existing mutable borrow of the value at `key` via [`get_mut_unchecked`].
    /// Multiple immutable borrows are permitted.
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list: FixedFreeList<i32, 16> = FixedFreeList::new();
    /// let key = list.alloc(1).unwrap();
    /// let value = unsafe { list.get_unchecked(key) };
    /// assert_eq!(*value, 1);
    /// ```
    #[inline(always)]
    pub unsafe fn get_unchecked(&self, key: usize) -> &T {
        #[cfg(all(feature = "strict"))]
        assert!(!self.is_free(key));

        // # Safety
        // Function invariants imply the space is occupied by an initialized value
        unsafe { &self.data.get_unchecked(key).assume_init_ref().value }
    }

    /// Provides mutable access to the value at `key`.
    ///
    /// # Returns
    ///
    /// An immutable borrow of the value at `key`.
    ///
    /// # Safety
    ///
    /// `key` must have originated from calling [`alloc`] on the same instance
    /// and the space must not already been freed since the [`alloc`] call.
    ///
    /// There must be no existing borrows of the value at `key` via [`get_unchecked`] or [`get_mut_unchecked`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list: FixedFreeList<i32, 16> = FixedFreeList::new();
    /// let key = list.alloc(8).unwrap();
    /// let value = unsafe { list.get_mut_unchecked(key) };
    /// *value = 2;
    /// assert_eq!(unsafe { list.free_unchecked(key) }, 2);
    /// ```
    #[inline(always)]
    pub unsafe fn get_mut_unchecked(&mut self, key: usize) -> &mut T {
        #[cfg(all(feature = "strict"))]
        assert!(!self.is_free(key));

        // # Safety
        // Function invariants imply the space is occupied by an initialized value
        unsafe { &mut self.data.get_unchecked_mut(key).assume_init_mut().value }
    }

    /// Returns an upper bound on the number of elements contained.
    /// The actual number of elements is guaranteed to be less than or equal to this.
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list: FixedFreeList<i32, 16> = FixedFreeList::new();
    /// list.alloc(5);
    /// assert_eq!(list.size_hint(), 1);
    /// ```
    #[inline(always)]
    pub fn size_hint(&self) -> usize {
        self.high
    }

    /// Returns `true` if there is no free space left.
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list: FixedFreeList<i32, 1> = FixedFreeList::new();
    /// assert!(!list.is_full());
    /// list.alloc(7);
    /// assert!(list.is_full());
    /// ```
    #[inline(always)]
    pub fn is_full(&self) -> bool {
        self.high == N && self.next == N
    }

    /// Removes and drops all contained values.
    ///
    /// # Time Complexity
    ///
    /// O(1) if `T: Copy`, otherwise O(N).
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list: FixedFreeList<i32, 1> = FixedFreeList::new();
    /// list.alloc(3);
    /// assert!(list.is_full());
    /// list.clear();
    /// assert!(!list.is_full());
    /// ```
    pub fn clear(&mut self) {
        if mem::needs_drop::<T>() {
            let mut free = [false; N];
            let mut next = self.next;
            while next < N {
                free[next] = true;
                next = unsafe { self.data.get_unchecked(next).assume_init_ref().next };
            }
            for (i, &is_free) in free.iter().enumerate().take(self.high) {
                if !is_free {
                    unsafe {
                        ManuallyDrop::drop(
                            &mut self.data.get_unchecked_mut(i).assume_init_mut().value,
                        );
                    }
                }
            }
        }

        self.high = 0;
        self.next = N;
    }
}

unsafe impl<T: Sync, const N: usize> Sync for FixedFreeList<T, N> {}
unsafe impl<T: Send, const N: usize> Send for FixedFreeList<T, N> {}

impl<T, const N: usize> Drop for FixedFreeList<T, N> {
    #[inline(always)]
    fn drop(&mut self) {
        if mem::needs_drop::<T>() {
            self.clear();
        }
    }
}

#[derive(Debug)]
enum Space<T: Sized> {
    Value(T),
    Free(usize),
    Uninit,
}

trait UninitProvider: Sized {
    const UNINIT: Space<Self>;
}

impl<T> UninitProvider for T {
    const UNINIT: Space<Self> = Space::Uninit;
}

impl<T, const N: usize> Debug for FixedFreeList<T, N>
where
    T: Debug,
{
    fn fmt(&self, f: &mut Formatter) -> Result {
        // Alternative array initializer for rustc <1.63
        // let mut spaces = [<&T>::UNINIT; N];
        let mut spaces = std::array::from_fn::<Space<&T>, N, _>(|_| Space::Uninit);
        let mut next = self.next;
        while next < N {
            let free = unsafe { self.data.get_unchecked(next).assume_init_ref().next };
            spaces[next] = Space::Free(free);
            next = free;
        }
        for (i, space) in spaces.iter_mut().enumerate().take(self.high) {
            if let Space::Uninit = space {
                *space = Space::Value(unsafe { &self.data[i].assume_init_ref().value });
            }
        }

        f.debug_struct("FixedFreeList")
            .field("next", &self.next)
            .field("high", &self.high)
            .field("data", &spaces)
            .finish()
    }
}

impl<T, const N: usize> Default for FixedFreeList<T, N> {
    fn default() -> Self {
        Self::new()
    }
}

/// A lifetimed key into a `SafeFixedFreeList`
#[derive(Debug)]
pub struct ArenaKey<'a, T> {
    index: usize,
    _marker: PhantomData<&'a T>,
}

/// A fixed-size free-list with key lifetime safety and macroless unique typing.
/// This is a somewhat experimental use of the borrowchecker,
/// and as such [`new`] is `unsafe`.
pub struct SafeFixedFreeList<'a, T, const N: usize, U> {
    _marker: PhantomData<&'a U>,
    inner: FixedFreeList<T, N>,
}

impl<'a, T, const N: usize, U: Fn()> SafeFixedFreeList<'a, T, N, U> {
    /// Creates a new empty [`SafeFixedFreeList`]
    ///
    /// # Safety
    /// You MUST provide a unique inline closure to ensure keys are not
    /// shared with another instance.
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list = unsafe { SafeFixedFreeList::<i32, 16, _>::new(||()) };
    /// ```
    #[inline(always)]
    pub unsafe fn new(_: U) -> Self {
        Self {
            _marker: PhantomData,
            inner: FixedFreeList::new(),
        }
    }

    /// If there is space, adds `value` to the free list and returns its key.
    /// If there is no space, Drops `value` and returns `None`.
    ///
    /// # Returns
    ///
    /// `None` if the list was already full.
    /// Note: `value` is dropped in this case. Check [`is_full`] beforehand to avoid this if desired.
    ///
    /// `Some(key)` if there was spare capacity to accommodate `value`.
    /// `key` can now be used to access `value` via [`get`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list = unsafe { SafeFixedFreeList::<i32, 16, _>::new(||()) };
    /// list.alloc(1);
    /// ```
    #[inline(always)]
    pub fn alloc<'k>(&mut self, value: T) -> Option<ArenaKey<'k, U>>
    where
        'a: 'k,
    {
        self.inner.alloc(value).map(|index| ArenaKey {
            index,
            _marker: PhantomData,
        })
    }

    /// Frees the space occupied by the value at `key` and returns the value.
    ///
    /// # Returns
    ///
    /// The value at `key`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list = unsafe { SafeFixedFreeList::<i32, 16, _>::new(||()) };
    /// let key = list.alloc(1).unwrap();
    /// let value = list.free(key);
    /// assert_eq!(value, 1);
    /// ```
    #[inline(always)]
    pub fn free(&mut self, key: ArenaKey<U>) -> T {
        unsafe { self.inner.free_unchecked(key.index) }
    }

    /// Provides immutable access to the value at `key`.
    ///
    /// # Returns
    ///
    /// An immutable borrow of the value at `key`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list = unsafe { SafeFixedFreeList::<i32, 16, _>::new(||()) };
    /// let key = list.alloc(1).unwrap();
    /// let value = list.get(&key);
    /// assert_eq!(*value, 1);
    /// ```
    #[inline(always)]
    pub fn get<'k: 'v, 'v>(&self, key: &'k ArenaKey<U>) -> &'v T
    where
        'a: 'k,
    {
        unsafe { mem::transmute::<&T, &T>(self.inner.get_unchecked(key.index)) }
    }

    /// Provides mutable access to the value at `key`.
    ///
    /// # Returns
    ///
    /// An immutable borrow of the value at `key`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list = unsafe { SafeFixedFreeList::<i32, 16, _>::new(||()) };
    /// let mut key = list.alloc(8).unwrap();
    /// let value = list.get_mut(&mut key);
    /// *value = 2;
    /// assert_eq!(list.free(key), 2);
    /// ```
    #[inline(always)]
    pub fn get_mut<'k: 'v, 'v>(&mut self, key: &'k mut ArenaKey<U>) -> &'v mut T
    where
        'a: 'k,
    {
        unsafe { mem::transmute::<&mut T, &mut T>(self.inner.get_mut_unchecked(key.index)) }
    }

    /// Returns an upper bound on the number of elements contained.
    /// The actual number of elements is guaranteed to be less than or equal to this.
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list = unsafe { SafeFixedFreeList::<i32, 16, _>::new(||()) };
    /// list.alloc(5);
    /// assert_eq!(list.size_hint(), 1);
    /// ```
    #[inline(always)]
    pub fn size_hint(&self) -> usize {
        self.inner.size_hint()
    }

    /// Returns `true` if there is no free space left.
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list = unsafe { SafeFixedFreeList::<i32, 1, _>::new(||()) };
    /// assert!(!list.is_full());
    /// list.alloc(7);
    /// assert!(list.is_full());
    /// ```
    #[inline(always)]
    pub fn is_full(&self) -> bool {
        self.inner.is_full()
    }

    /// Removes and drops all contained values.
    ///
    /// # Time Complexity
    ///
    /// O(1) if `T: Copy`, otherwise O(N).
    ///
    /// # Examples
    ///
    /// ```
    /// # use fixed_free_list::*;
    /// let mut list = unsafe { SafeFixedFreeList::<i32, 1, _>::new(||()) };
    /// list.alloc(3);
    /// assert!(list.is_full());
    /// list.clear();
    /// assert!(!list.is_full());
    /// ```
    #[inline(always)]
    pub fn clear(&mut self) {
        self.inner.clear();
    }
}

impl<'a, T, const N: usize, U: Fn()> Debug for SafeFixedFreeList<'a, T, N, U>
where
    T: Debug,
{
    fn fmt(&self, f: &mut Formatter) -> Result {
        f.debug_struct("SafeFixedFreeList")
            .field("inner", &self.inner)
            .finish()
    }
}

#[cfg(test)]
mod test {
    use crate::*;
    use rand::Rng;
    use std::cell::RefCell;

    #[test]
    fn test_safe() {
        fn consume<T>(_: T) {}
        let mut list = unsafe { SafeFixedFreeList::<u32, 16, _>::new(|| ()) };
        let mut key1 = list.alloc(5).unwrap();
        let mut key2 = list.alloc(6).unwrap();
        let value1 = list.get_mut(&mut key1);
        let value2 = list.get_mut(&mut key2);
        // miri hates this, I think its a valid abuse of borrowck though
        *value1 = 2;
        consume(value1);
        consume(value2);
        list.free(key1);
        list.free(key2);
        consume(list);
    }

    #[test]
    fn test_remove_all() {
        let mut list = FixedFreeList::<u32, 16>::new();
        for i in 0..16 {
            assert_eq!(list.alloc(i), Some(i as usize));
        }
        for i in 0..16 {
            unsafe {
                assert_eq!(list.free_unchecked(i as usize), i);
            }
        }
        for i in 0..16 {
            assert!(list.is_free(i));
        }
        for i in 0..16 {
            list.alloc(i);
        }
        assert!(list.is_full());
    }

    #[test]
    fn test_reuse_half() {
        let mut list = FixedFreeList::<u32, 16>::new();
        for i in 0..16 {
            assert_eq!(list.alloc(i), Some(i as usize));
        }
        for i in 0..8 {
            unsafe {
                list.free_unchecked(i * 2usize);
            }
        }
        for i in 0..8 {
            list.alloc(i);
        }
        assert!(list.is_full());
    }

    #[test]
    fn test_reuse_preferred() {
        let mut list = FixedFreeList::<u32, 16>::new();
        for i in 0..8 {
            assert_eq!(list.alloc(i), Some(i as usize));
        }
        for i in 0..4 {
            unsafe {
                list.free_unchecked(i * 2usize);
            }
        }
        for i in 0..4 {
            list.alloc(i);
        }
        assert_eq!(list.size_hint(), 8);
        for i in 8..16 {
            assert_eq!(list.alloc(i), Some(i as usize));
        }
        assert!(list.is_full());
    }

    #[test]
    fn test_debug() {
        let mut list = FixedFreeList::<u32, 8>::new();
        list.alloc(3);
        let key1 = list.alloc(5).unwrap();
        list.alloc(7);
        list.alloc(4);
        let key2 = list.alloc(2).unwrap();
        unsafe {
            list.free_unchecked(key1);
            list.free_unchecked(key2);
        }
        assert_eq!(format!("{:?}", list), "FixedFreeList { next: 4, high: 5, data: [Value(3), Free(8), Value(7), Value(4), Free(1), Uninit, Uninit, Uninit] }");
    }

    #[test]
    fn test_full() {
        let mut list = FixedFreeList::<u32, 4>::new();
        assert_eq!(list.alloc(3), Some(0));
        assert_eq!(list.alloc(5), Some(1));
        assert_eq!(list.alloc(7), Some(2));
        assert_eq!(list.alloc(4), Some(3));
        assert_eq!(list.alloc(2), None);
    }

    #[test]
    fn test_drop() {
        let drops = RefCell::new(0usize);
        {
            let mut list: FixedFreeList<DropCounted, 16> = FixedFreeList::new();
            for _ in 0..11 {
                list.alloc(DropCounted(&drops));
            }
            assert_eq!(*drops.borrow(), 0);

            // Drop a few
            for i in 0..4 {
                unsafe {
                    list.free_unchecked(i);
                }
            }
            assert_eq!(*drops.borrow(), 4);

            // Let the rest drop
        }
        assert_eq!(*drops.borrow(), 11);
        {
            let mut list: FixedFreeList<DropCounted, 1> = FixedFreeList::new();
            list.alloc(DropCounted(&drops));

            // Inserting into a full list should drop the value
            list.alloc(DropCounted(&drops));
            assert_eq!(*drops.borrow(), 12);

            // Let the rest drop
        }
        assert_eq!(*drops.borrow(), 13);
    }

    #[derive(Clone)]
    struct DropCounted<'a>(&'a RefCell<usize>);

    impl<'a> Drop for DropCounted<'a> {
        fn drop(&mut self) {
            *self.0.borrow_mut() += 1;
        }
    }

    #[test]
    fn test_fuzz() {
        fuzz::<0, 4>(10);
        fuzz::<1, 8>(10);
        fuzz::<2, 8>(10);
        fuzz::<3, 8>(10);
        fuzz::<5, 8>(10);
        fuzz::<7, 16>(10);
        fuzz::<16, 16>(10);
        fuzz::<16, 64>(10);
    }

    fn fuzz<const N: usize, const M: usize>(iters: usize) {
        for _ in 0..iters {
            let mut list: FixedFreeList<usize, N> = FixedFreeList::new();
            let mut keys = Vec::new();
            let mut count = 0;
            let mut high = 0;
            for i in 0..M {
                if rand::thread_rng().gen() {
                    if let Some(key) = list.alloc(i) {
                        keys.push(key);
                        count += 1;
                        if high < count {
                            high = count;
                        }
                    } else {
                        assert_eq!(count, N);
                    }
                } else if count > 0 {
                    let key = keys.swap_remove(rand::thread_rng().gen_range(0..keys.len()));
                    unsafe {
                        assert!(!list.is_free(key));
                        list.free_unchecked(key);
                        assert!(list.is_free(key));
                        count -= 1;
                    }
                }
                assert_eq!(list.size_hint(), high);
            }
        }
    }
}