sector 0.1.21

A stateful vector implementation that provides different memory management behaviors through Rust traits and state machines.
Documentation
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
//! # Fixed Sector State
//!
//! The `Fixed` state indicates that the sector has a fixed capacity which does not change once set.
//! This means that operations which would normally trigger a reallocation (growth or shrink) are disabled.
//!
//! In this state, the methods `push` and `insert` will only add an element if there is remaining capacity.
//! They return a boolean value indicating success (`true`) or failure (`false`), depending on whether
//! the operation could be performed without exceeding the fixed capacity.
//!
//! **Note:** There is a known conflict with zero-sized types (ZST). When using a ZST as the element type,
//! a sector with a fixed capacity (e.g., 5) might allow unlimited insertions because ZSTs treat capacity
//! as maximal. This behavior contradicts the intended fixed capacity semantics and is subject to further discussion.
use core::ptr::NonNull;

use crate::components::{Cap, Grow, Index, Insert, Len, Pop, Ptr, Push, Remove, Shrink};

use crate::Sector;

/// The `Fixed` state indicates that the sector has a fixed capacity.
///
/// In this state, operations that would normally trigger a growth or shrink are disabled.
/// Instead, insertions (via `push` or `insert`) only succeed if there is enough capacity already.
///
/// > **Note:** There is a known conflict with zero-sized types (ZST). If a user creates a sector
/// > with a fixed capacity (e.g., 5) and uses a ZST as the element type, it is possible to insert or push
/// > an unlimited number of elements because ZSTs set the capacity to its maximum value. This contradicts
/// > the intended behavior of a fixed-capacity sector. Further discussion or resolution for this issue
/// > is needed.
pub struct Fixed;

impl crate::components::DefaultIter for Fixed {}

impl crate::components::DefaultDrain for Fixed {}

impl<T> Sector<Fixed, T> {
    /// Attempts to push an element to the sector.
    ///
    /// # Behavior
    ///
    /// - If the sector has remaining capacity (i.e., the current length is less than capacity),
    ///   the element is pushed, and the function returns `Ok(())`.
    /// - If the sector is full (i.e., the current length equals capacity), the element is **not** pushed,
    ///   and the function returns `Err(elem)`, where `elem` is the element that could not be
    ///   pushed.
    ///
    /// # Returns
    ///
    /// - `Ok(())` if the element was successfully pushed.
    /// - `Err(T)` containing the element if there was insufficient capacity.
    pub fn push(&mut self, elem: T) -> Result<(), T> {
        if self.__cap() == self.__len() {
            Err(elem)
        } else {
            self.__push(elem);
            Ok(())
        }
    }

    /// Removes the last element from the sector and returns it.
    ///
    /// Returns `None` if the sector is empty.
    pub fn pop(&mut self) -> Option<T> {
        self.__pop()
    }

    /// Attempts to insert an element into the sector at the specified index.
    ///
    /// # Behavior
    ///
    /// - If there is enough capacity (i.e., the current length is less than the capacity),  
    ///   the element is inserted at the provided index, and the function returns `Ok(())`.  
    /// - If the sector is full (i.e., the current length equals the capacity),  
    ///   the element is **not** inserted, and the function returns `Err(elem)`,  
    ///   where `elem` is the element that could not be inserted.
    ///
    /// # Returns
    ///
    /// - `Ok(())` if the element was successfully inserted.  
    /// - `Err(T)` containing the element if there was insufficient capacity.
    pub fn insert(&mut self, index: usize, elem: T) -> Result<(), T> {
        if self.__cap() == self.__len() {
            Err(elem)
        } else {
            self.__insert(index, elem);
            Ok(())
        }
    }

    /// Removes the element at the specified index and returns it, shifting all elements after it to the left.
    ///
    /// # Panics
    ///
    /// Panics if the index is out of bounds.
    pub fn remove(&mut self, index: usize) -> T {
        self.__remove(index)
    }

    /// Returns a reference to the element at the given index if it exists.
    pub fn get(&self, index: usize) -> Option<&T> {
        self.__get(index)
    }

    /// Returns a mutable reference to the element at the given index if it exists.
    pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
        self.__get_mut(index)
    }
}

impl<T> Ptr<T> for Sector<Fixed, T> {
    /// Returns the raw pointer to the first element in the sector.
    ///
    /// # Safety
    ///
    /// The pointer is obtained using an unsafe method which assumes the sector’s storage is valid.
    fn __ptr(&self) -> NonNull<T> {
        unsafe { self.as_ptr() }
    }

    /// Sets the raw pointer of the sector to a new value.
    ///
    /// # Safety
    ///
    /// The caller must ensure that the new pointer is valid for the current sector.
    fn __ptr_set(&mut self, new_ptr: NonNull<T>) {
        unsafe { Sector::set_ptr(self, new_ptr) };
    }
}

impl<T> Len for Sector<Fixed, T> {
    /// Returns the current number of elements in the sector.
    fn __len(&self) -> usize {
        Sector::len(self)
    }

    /// Sets the current number of elements in the sector.
    ///
    /// # Safety
    ///
    /// This function is unsafe because the new length must not exceed the actual allocation.
    fn __len_set(&mut self, new_len: usize) {
        unsafe { Sector::set_len(self, new_len) };
    }
}

impl<T> Cap for Sector<Fixed, T> {
    /// Returns the current capacity of the sector.
    ///
    /// This value indicates how many elements the sector can hold without needing to grow.
    fn __cap(&self) -> usize {
        self.capacity()
    }

    /// Sets a new capacity for the sector.
    ///
    /// # Safety
    ///
    /// The new capacity must be a valid size for the sector's allocation.
    fn __cap_set(&mut self, new_cap: usize) {
        unsafe { self.set_capacity(new_cap) };
    }
}

/// For the `Fixed` state, the sector is not allowed to grow.
/// This implementation intentionally does nothing.
unsafe impl<T> Grow<T> for Sector<Fixed, T> {
    /// Does nothing as the fixed state does not support growth.
    unsafe fn __grow(&mut self, _: usize, _: usize) {}
}

/// For the `Fixed` state, the sector is not allowed to shrink.
/// This implementation intentionally does nothing.
unsafe impl<T> Shrink<T> for Sector<Fixed, T> {
    /// Does nothing as the fixed state does not support shrinking.
    unsafe fn __shrink(&mut self, _: usize, _: usize) {}
}

// The following trait provides additional functionallity based on the grow/shrink
// implementations
// It also serves to mark the available operations on the sector.
impl<T> Push<T> for Sector<Fixed, T> {}
impl<T> Pop<T> for Sector<Fixed, T> {}
impl<T> Insert<T> for Sector<Fixed, T> {}
impl<T> Index<T> for Sector<Fixed, T> {}
impl<T> Remove<T> for Sector<Fixed, T> {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::components::testing::*;

    #[test]
    fn test_push_and_get() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(3);

        let _ = sector.push(10);
        let _ = sector.push(20);
        let _ = sector.push(30);
        assert_eq!(sector.push(40), Err(40)); // Should return err because there is no capacity left

        assert_eq!(sector.get(0), Some(&10));
        assert_eq!(sector.get(1), Some(&20));
        assert_eq!(sector.get(2), Some(&30));
        assert_eq!(sector.get(3), None);
        assert_eq!(sector.get(4), None);
    }

    #[test]
    fn test_push_and_get_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(2);

        repeat!(sector.push(ZeroSizedType), 2);

        // Does not work because the cap for ZSTs is a pretty large number
        //assert_eq!(sector.get(0), Some(&ZeroSizedType));
        assert_eq!(sector.get(0), Some(&ZeroSizedType));
        assert_eq!(sector.get(1), Some(&ZeroSizedType));
        assert_eq!(sector.get(2), None);
        assert_eq!(sector.get(3), None);
    }

    #[test]
    fn test_pop() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(3);

        let _ = sector.push(10);
        let _ = sector.push(20);
        let _ = sector.push(30);

        assert_eq!(sector.pop(), Some(30));
        assert_eq!(sector.pop(), Some(20));
        assert_eq!(sector.pop(), Some(10));
        assert_eq!(sector.pop(), None);
        assert_eq!(sector.pop(), None);
        assert_eq!(sector.pop(), None);
    }

    #[test]
    fn test_pop_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(3);

        repeat!(sector.push(ZeroSizedType), 3);

        assert_eq!(sector.pop(), Some(ZeroSizedType));
        assert_eq!(sector.pop(), Some(ZeroSizedType));
        assert_eq!(sector.pop(), Some(ZeroSizedType));
        assert_eq!(sector.pop(), None);
        assert_eq!(sector.pop(), None);
        assert_eq!(sector.pop(), None);
    }

    #[test]
    fn test_insert() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(3);

        let _ = sector.push(10);
        let _ = sector.push(30);
        let _ = sector.insert(1, 20);
        assert_eq!(sector.insert(1, 20), Err(20)); // Should return Err() because there is no capacity left
        assert_eq!(sector.get(0), Some(&10));
        assert_eq!(sector.get(1), Some(&20));
        assert_eq!(sector.get(2), Some(&30));
    }

    #[test]
    fn test_insert_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(3);

        repeat!(sector.push(ZeroSizedType), 2);
        let _ = sector.insert(1, ZeroSizedType);
        assert_eq!(sector.get(0), Some(&ZeroSizedType));
        assert_eq!(sector.get(1), Some(&ZeroSizedType));
        assert_eq!(sector.get(2), Some(&ZeroSizedType));
    }

    #[test]
    fn test_remove() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(3);

        let _ = sector.push(10);
        let _ = sector.push(20);
        let _ = sector.push(30);

        assert_eq!(sector.remove(1), 20);
        assert_eq!(sector.get(0), Some(&10));
        assert_eq!(sector.get(1), Some(&30));
        assert_eq!(sector.get(2), None);
        assert_eq!(sector.get(2), None);
    }

    #[test]
    fn test_remove_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(3);

        repeat!(sector.push(ZeroSizedType), 3);

        assert_eq!(sector.remove(1), ZeroSizedType);
        assert_eq!(sector.get(0), Some(&ZeroSizedType));
        assert_eq!(sector.get(1), Some(&ZeroSizedType));
        assert_eq!(sector.get(2), None);
        assert_eq!(sector.get(3), None);
    }

    #[test]
    fn test_remove_on_emtpy() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(3);

        let _ = sector.push(10);
        let _ = sector.push(20);
        let _ = sector.push(30);

        assert_eq!(sector.remove(1), 20);
        assert_eq!(sector.get(0), Some(&10));
        assert_eq!(sector.get(1), Some(&30));
        assert_eq!(sector.get(2), None);
    }

    #[test]
    fn test_remove_on_emtpy_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(3);

        repeat!(sector.push(ZeroSizedType), 3);

        assert_eq!(sector.remove(1), ZeroSizedType);
        assert_eq!(sector.get(0), Some(&ZeroSizedType));
        assert_eq!(sector.get(1), Some(&ZeroSizedType));
        assert_eq!(sector.get(2), None);
    }

    #[test]
    fn test_get_mut() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(3);

        let _ = sector.push(10);
        let _ = sector.push(20);
        let _ = sector.push(30);

        if let Some(value) = sector.get_mut(1) {
            *value = 25;
        }

        assert_eq!(sector.get(1), Some(&25));
    }

    #[test]
    fn test_grow_behavior() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(100);

        for i in 0..100 {
            assert_eq!(sector.push(i), Ok(()));
        }

        assert_eq!(sector.len(), 100);
        assert!(sector.capacity() == 100);
    }

    #[test]
    fn test_grow_behavior_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(100);

        for _ in 0..100 {
            assert_eq!(sector.push(ZeroSizedType), Ok(()));
        }

        assert_eq!(sector.len(), 100);
        assert!(sector.capacity() == !0);
    }

    #[test]
    fn test_empty_behavior() {
        let mut sector: Sector<Fixed, i32> = Sector::new();

        assert_eq!(sector.pop(), None);
        assert_eq!(sector.get(0), None);
    }

    #[test]
    fn test_empty_behavior_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::new();

        assert_eq!(sector.pop(), None);
        assert_eq!(sector.get(0), None);
    }

    #[test]
    fn test_out_of_bounds_access() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(1);

        let _ = sector.push(10);

        assert_eq!(sector.get(1), None);
        assert_eq!(sector.get_mut(1), None);
    }

    #[test]
    fn test_out_of_bounds_access_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(1);

        let _ = sector.push(ZeroSizedType);

        assert_eq!(sector.get(1), None);
        assert_eq!(sector.get_mut(1), None);
    }

    #[test]
    fn test_deref() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(5);
        let _ = sector.push(10);
        let _ = sector.push(20);
        let _ = sector.push(30);
        let _ = sector.push(40);
        let _ = sector.push(-10);

        let derefed_sec = &*sector;

        assert_eq!(derefed_sec.first(), Some(&10));
        assert_eq!(derefed_sec.get(1), Some(&20));
        assert_eq!(derefed_sec.get(2), Some(&30));
        assert_eq!(derefed_sec.get(4), Some(&-10));
        assert_eq!(derefed_sec.get(5), None);
    }

    #[test]
    fn test_deref_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(5);

        repeat!(sector.push(ZeroSizedType), 5);
        let derefed_sec = &*sector;

        assert_eq!(derefed_sec.first(), Some(&ZeroSizedType));
        assert_eq!(derefed_sec.get(1), Some(&ZeroSizedType));
        assert_eq!(derefed_sec.get(2), Some(&ZeroSizedType));
        assert_eq!(derefed_sec.get(4), Some(&ZeroSizedType));
        assert_eq!(derefed_sec.get(5), None);
    }

    #[test]
    fn test_deref_mut() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(5);
        let _ = sector.push(10);
        let _ = sector.push(20);
        let _ = sector.push(30);
        let _ = sector.push(40);
        let _ = sector.push(-10);

        let derefed_sec = &mut *sector;

        derefed_sec[0] = 100;
        derefed_sec[1] = 200;
        derefed_sec[4] = -100;

        assert_eq!(derefed_sec.first(), Some(&100));
        assert_eq!(derefed_sec.get(1), Some(&200));
        assert_eq!(derefed_sec.get(2), Some(&30));
        assert_eq!(derefed_sec.get(4), Some(&-100));
        assert_eq!(derefed_sec.get(5), None);

        assert_eq!(sector.get(0), Some(&100));
        assert_eq!(sector.get(1), Some(&200));
    }

    #[test]
    fn test_deref_mut_zero_sized() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(5);
        repeat!(sector.push(ZeroSizedType), 5);

        let derefed_sec = &mut *sector;

        // We can't really update ZSTs...
        assert_eq!(derefed_sec.first(), Some(&ZeroSizedType));
        assert_eq!(derefed_sec.get(1), Some(&ZeroSizedType));
        assert_eq!(derefed_sec.get(2), Some(&ZeroSizedType));
        assert_eq!(derefed_sec.get(4), Some(&ZeroSizedType));
        assert_eq!(derefed_sec.get(5), None);
    }

    #[test]
    fn test_into_iter_next() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(6);
        let _ = sector.push(1000);
        let _ = sector.push(20528);
        let _ = sector.push(3522);
        let _ = sector.push(529388);
        let _ = sector.push(-81893);
        let _ = sector.push(-238146);
        assert_eq!(sector.push(-35892281), Err(-35892281));

        let mut iter_sec = sector.into_iter();

        assert_eq!(iter_sec.next(), Some(1000));
        assert_eq!(iter_sec.next(), Some(20528));
        assert_eq!(iter_sec.next(), Some(3522));
        assert_eq!(iter_sec.next(), Some(529388));
        assert_eq!(iter_sec.next(), Some(-81893));
        assert_eq!(iter_sec.next(), Some(-238146));
        assert_eq!(iter_sec.next(), None);
        assert_eq!(iter_sec.next(), None);
        assert_eq!(iter_sec.next(), None);
    }

    #[test]
    fn test_into_iter_next_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(6);
        repeat!(sector.push(ZeroSizedType), 6);

        let mut iter_sec = sector.into_iter();

        assert_eq!(iter_sec.next(), Some(ZeroSizedType));
        assert_eq!(iter_sec.next(), Some(ZeroSizedType));
        assert_eq!(iter_sec.next(), Some(ZeroSizedType));
        assert_eq!(iter_sec.next(), Some(ZeroSizedType));
        assert_eq!(iter_sec.next(), Some(ZeroSizedType));
        assert_eq!(iter_sec.next(), Some(ZeroSizedType));
        assert_eq!(iter_sec.next(), None);
        assert_eq!(iter_sec.next(), None);
        assert_eq!(iter_sec.next(), None);
        assert_eq!(iter_sec.next(), None);
    }

    #[test]
    fn test_into_iter_back() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(6);
        let _ = sector.push(1000);
        let _ = sector.push(20528);
        let _ = sector.push(3522);
        let _ = sector.push(529388);
        let _ = sector.push(-81893);
        let _ = sector.push(-238146);

        let mut iter_sec = sector.into_iter();

        assert_eq!(iter_sec.next_back(), Some(-238146));
        assert_eq!(iter_sec.next_back(), Some(-81893));
        assert_eq!(iter_sec.next_back(), Some(529388));
        assert_eq!(iter_sec.next_back(), Some(3522));
        assert_eq!(iter_sec.next_back(), Some(20528));
        assert_eq!(iter_sec.next_back(), Some(1000));
        assert_eq!(iter_sec.next(), None);
        assert_eq!(iter_sec.next(), None);
        assert_eq!(iter_sec.next(), None);
        assert_eq!(iter_sec.next(), None);
    }

    #[test]
    fn test_into_iter_back_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(6);

        repeat!(sector.push(ZeroSizedType), 6);

        let mut iter_sec = sector.into_iter();

        assert_eq!(iter_sec.next_back(), Some(ZeroSizedType));
        assert_eq!(iter_sec.next_back(), Some(ZeroSizedType));
        assert_eq!(iter_sec.next_back(), Some(ZeroSizedType));
        assert_eq!(iter_sec.next_back(), Some(ZeroSizedType));
        assert_eq!(iter_sec.next_back(), Some(ZeroSizedType));
        assert_eq!(iter_sec.next_back(), Some(ZeroSizedType));
        assert_eq!(iter_sec.next_back(), None);
        assert_eq!(iter_sec.next_back(), None);
        assert_eq!(iter_sec.next_back(), None);
        assert_eq!(iter_sec.next_back(), None);
    }

    #[test]
    fn test_drain_next() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(3);

        let _ = sector.push(1);
        let _ = sector.push(2);
        let _ = sector.push(3);

        let mut drain_iter = sector.drain();

        assert_eq!(drain_iter.next(), Some(1));
        assert_eq!(drain_iter.next(), Some(2));
        assert_eq!(drain_iter.next(), Some(3));
        assert_eq!(drain_iter.next(), None);
    }

    #[test]
    fn test_drain_lifetime() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(3);

        let _ = sector.push(1);
        let _ = sector.push(2);
        let _ = sector.push(3);

        let mut drain_iter = sector.drain();

        assert_eq!(drain_iter.next(), Some(1));
        assert_eq!(drain_iter.next(), Some(2));
        assert_eq!(drain_iter.next(), Some(3));
        assert_eq!(drain_iter.next(), None);
    }

    #[test]
    fn test_drain_next_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(3);

        repeat!(sector.push(ZeroSizedType), 3);

        let mut drain_iter = sector.drain();

        assert_eq!(drain_iter.next(), Some(ZeroSizedType));
        assert_eq!(drain_iter.next(), Some(ZeroSizedType));
        assert_eq!(drain_iter.next(), Some(ZeroSizedType));
        assert_eq!(drain_iter.next(), None);
    }

    #[test]
    fn test_drain_next_back() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(3);

        let _ = sector.push(10);
        let _ = sector.push(20);
        let _ = sector.push(30);

        let mut drain_iter = sector.drain();

        assert_eq!(drain_iter.next_back(), Some(30));
        assert_eq!(drain_iter.next_back(), Some(20));
        assert_eq!(drain_iter.next_back(), Some(10));
        assert_eq!(drain_iter.next_back(), None);
    }

    #[test]
    fn test_drain_next_back_zst() {
        let mut sector: Sector<Fixed, ZeroSizedType> = Sector::with_capacity(3);

        repeat!(sector.push(ZeroSizedType), 3);

        let mut drain_iter = sector.drain();

        assert_eq!(drain_iter.next_back(), Some(ZeroSizedType));
        assert_eq!(drain_iter.next_back(), Some(ZeroSizedType));
        assert_eq!(drain_iter.next_back(), Some(ZeroSizedType));
        assert_eq!(drain_iter.next_back(), None);
    }

    #[test]
    fn test_drain_mixed() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(4);

        let _ = sector.push(100);
        let _ = sector.push(200);
        let _ = sector.push(300);
        let _ = sector.push(400);

        let mut drain_iter = sector.drain();

        assert_eq!(drain_iter.next(), Some(100));
        assert_eq!(drain_iter.next_back(), Some(400));
        assert_eq!(drain_iter.next(), Some(200));
        assert_eq!(drain_iter.next_back(), Some(300));
        assert_eq!(drain_iter.next(), None);
        assert_eq!(drain_iter.next_back(), None);
    }

    #[test]
    fn test_drain_size_hint() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(5);

        for i in 0..5 {
            let _ = sector.push(i);
        }

        let mut drain_iter = sector.drain();
        let (lower, upper) = drain_iter.size_hint();
        assert_eq!(lower, 5);
        assert_eq!(upper, Some(5));

        drain_iter.next();
        let (lower, upper) = drain_iter.size_hint();
        assert_eq!(lower, 4);
        assert_eq!(upper, Some(4));
    }

    #[test]
    fn test_drain_drop() {
        let counter = core::cell::Cell::new(0);
        {
            let mut sector: Sector<Fixed, DropCounter> = Sector::with_capacity(5);
            for _ in 0..5 {
                let _ = sector.push(DropCounter { counter: &counter });
            }
            {
                let mut drain_iter = sector.drain();
                assert!(drain_iter.next().is_some());
                assert!(drain_iter.next().is_some());
            }
        }
        assert_eq!(counter.get(), 5);
    }

    #[test]
    fn test_behaviour_grow() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(19);
        assert_eq!(sector.capacity(), 19);

        assert_eq!(sector.push(1), Ok(()));
        assert_eq!(sector.capacity(), 19);

        assert_eq!(sector.push(2), Ok(()));
        assert_eq!(sector.capacity(), 19);

        assert_eq!(sector.push(3), Ok(()));
        assert_eq!(sector.capacity(), 19);

        assert_eq!(sector.push(4), Ok(()));
        assert_eq!(sector.capacity(), 19);

        assert_eq!(sector.push(5), Ok(()));
        assert_eq!(sector.capacity(), 19);

        assert_eq!(sector.push(6), Ok(()));
        assert_eq!(sector.capacity(), 19);

        assert_eq!(sector.push(7), Ok(()));
        assert_eq!(sector.capacity(), 19);

        assert_eq!(sector.push(8), Ok(()));
        assert_eq!(sector.capacity(), 19);

        assert_eq!(sector.push(9), Ok(()));
        assert_eq!(sector.capacity(), 19);

        repeat!(assert_eq!(sector.push(10), Ok(())), 10);
        assert_eq!(sector.capacity(), 19);
    }

    #[test]
    fn test_behaviour_shrink() {
        let mut sector: Sector<Fixed, i32> = Sector::with_capacity(1000);
        assert_eq!(sector.capacity(), 1000);

        repeat!(assert_eq!(sector.push(1), Ok(())), 100);

        repeat!(assert_eq!(sector.push(2), Ok(())), 100);

        repeat!(assert_eq!(sector.push(3), Ok(())), 100);

        repeat!(assert_eq!(sector.push(4), Ok(())), 100);

        repeat!(assert_eq!(sector.push(5), Ok(())), 100);

        repeat!(assert_eq!(sector.push(6), Ok(())), 100);

        repeat!(assert_eq!(sector.push(7), Ok(())), 100);

        repeat!(assert_eq!(sector.push(8), Ok(())), 100);

        repeat!(assert_eq!(sector.push(9), Ok(())), 100);

        repeat!(assert_eq!(sector.push(10), Ok(())), 100);

        assert_eq!(sector.capacity(), 1000);

        sector.pop();
        sector.pop();
        sector.pop();
        sector.pop();
        sector.pop();

        repeat!(sector.pop(), 994);
        assert_eq!(sector.capacity(), 1000);

        sector.pop();
        assert_eq!(sector.capacity(), 1000);
        assert_eq!(sector.len(), 0)
    }
}