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
#![doc = include_str!("../readme.md")]

mod fat_ptr;
pub mod iter;
pub mod raw;

use iter::{Iter, IterMut};
use raw::RawUnsizedStack;
use std::{
    alloc::Layout,
    fmt::Debug,
    marker::PhantomData,
    ops::{Index, IndexMut},
    ptr::NonNull,
};

pub struct UnsizedStack<T: ?Sized> {
    raw: RawUnsizedStack<T>,
}

impl<T: ?Sized> UnsizedStack<T> {
    pub const fn new() -> Self {
        Self {
            raw: RawUnsizedStack::new(),
        }
    }

    pub const fn bytes_occupied(&self) -> usize {
        self.raw.bytes_occupied()
    }

    pub const fn buf_layout(&self) -> Layout {
        self.raw.buf_layout()
    }

    pub const fn buf_ptr(&self) -> NonNull<u8> {
        self.raw.buf_ptr()
    }

    pub fn get(&self, index: usize) -> Option<&T> {
        self.raw.get(index)
    }

    pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
        self.raw.get_mut(index)
    }

    pub fn push<I>(&mut self, item: I, coercion: fn(&I) -> &T) {
        self.raw.push(item, coercion)
    }

    pub fn len(&self) -> usize {
        self.raw.table().len()
    }

    pub fn is_empty(&self) -> bool {
        self.raw.table().is_empty()
    }

    pub fn pop(&mut self) -> Option<()> {
        self.raw.pop()
    }

    pub fn iter(&self) -> Iter<'_, T> {
        Iter {
            base: self.raw.buf_ptr().as_ptr(),
            table_iter: self.raw.table().iter(),
            _phantom: PhantomData,
        }
    }

    pub fn iter_mut(&mut self) -> IterMut<'_, T> {
        IterMut {
            base: self.raw.buf_ptr().as_ptr(),
            table_iter: self.raw.table().iter(),
            _phantom: PhantomData,
        }
    }

    pub fn clear(&mut self) {
        self.raw.clear();
    }
}

impl<T: ?Sized> Default for UnsizedStack<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a, T: ?Sized> IntoIterator for &'a UnsizedStack<T> {
    type Item = &'a T;

    type IntoIter = Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<'a, T: ?Sized> IntoIterator for &'a mut UnsizedStack<T> {
    type Item = &'a mut T;

    type IntoIter = IterMut<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter_mut()
    }
}

impl<T: ?Sized> Index<usize> for UnsizedStack<T> {
    type Output = T;

    fn index(&self, index: usize) -> &Self::Output {
        self.get(index).unwrap()
    }
}

impl<T: ?Sized> IndexMut<usize> for UnsizedStack<T> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        self.get_mut(index).unwrap()
    }
}

impl<T: ?Sized + Debug> Debug for UnsizedStack<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.raw.fmt(f)
    }
}