unsized_stack/
lib.rs

1/*
2 * Created on Sun Jul 02 2023
3 *
4 * Copyright (c) storycraft. Licensed under the MIT Licence.
5 */
6
7#![doc = include_str!("../readme.md")]
8
9mod fat_ptr;
10pub mod iter;
11pub mod raw;
12
13use iter::{Iter, IterMut};
14use raw::RawUnsizedStack;
15use std::{
16    alloc::Layout,
17    fmt::Debug,
18    ops::{Index, IndexMut},
19    ptr::NonNull,
20};
21
22pub struct UnsizedStack<T: ?Sized> {
23    raw: RawUnsizedStack<T>,
24}
25
26impl<T: ?Sized> UnsizedStack<T> {
27    pub const fn new() -> Self {
28        Self {
29            raw: RawUnsizedStack::new(),
30        }
31    }
32
33    pub const fn bytes_occupied(&self) -> usize {
34        self.raw.bytes_occupied()
35    }
36
37    pub const fn buf_layout(&self) -> Layout {
38        self.raw.buf_layout()
39    }
40
41    pub const fn buf_ptr(&self) -> NonNull<u8> {
42        self.raw.buf_ptr()
43    }
44
45    pub fn last(&self) -> Option<&T> {
46        self.raw.ref_from_table(|table| table.last())
47    }
48
49    pub fn last_mut(&mut self) -> Option<&mut T> {
50        self.raw.mut_from_table(|table| table.last())
51    }
52
53    pub fn get(&self, index: usize) -> Option<&T> {
54        self.raw.ref_from_table(|table| table.get(index))
55    }
56
57    pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
58        self.raw.mut_from_table(|table| table.get(index))
59    }
60
61    pub fn push<I>(&mut self, item: I, coercion: fn(&I) -> &T) {
62        self.raw.push(item, coercion)
63    }
64
65    pub fn len(&self) -> usize {
66        self.raw.table().len()
67    }
68
69    pub fn is_empty(&self) -> bool {
70        self.raw.table().is_empty()
71    }
72
73    pub fn pop(&mut self) -> Option<()> {
74        self.raw.pop()
75    }
76
77    pub fn iter(&self) -> Iter<'_, T> {
78        Iter::new(&self.raw)
79    }
80
81    pub fn iter_mut(&mut self) -> IterMut<'_, T> {
82        IterMut::new(&mut self.raw)
83    }
84
85    pub fn clear(&mut self) {
86        self.raw.clear();
87    }
88}
89
90impl<T: ?Sized> Default for UnsizedStack<T> {
91    fn default() -> Self {
92        Self::new()
93    }
94}
95
96impl<'a, T: ?Sized> IntoIterator for &'a UnsizedStack<T> {
97    type Item = &'a T;
98
99    type IntoIter = Iter<'a, T>;
100
101    fn into_iter(self) -> Self::IntoIter {
102        self.iter()
103    }
104}
105
106impl<'a, T: ?Sized> IntoIterator for &'a mut UnsizedStack<T> {
107    type Item = &'a mut T;
108
109    type IntoIter = IterMut<'a, T>;
110
111    fn into_iter(self) -> Self::IntoIter {
112        self.iter_mut()
113    }
114}
115
116impl<T: ?Sized> Index<usize> for UnsizedStack<T> {
117    type Output = T;
118
119    fn index(&self, index: usize) -> &Self::Output {
120        self.get(index).unwrap()
121    }
122}
123
124impl<T: ?Sized> IndexMut<usize> for UnsizedStack<T> {
125    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
126        self.get_mut(index).unwrap()
127    }
128}
129
130impl<T: ?Sized + Debug> Debug for UnsizedStack<T> {
131    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
132        f.debug_list().entries(self.iter()).finish()
133    }
134}