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
#![no_std]
#![forbid(missing_docs, clippy::missing_safety_doc)]
extern crate alloc as std;
use core::ops::{Deref, DerefMut, Index, IndexMut};
use std::vec::Vec;
#[cfg(feature = "pui-core")]
use pui_core::OneShotIdentifier;
mod pui_vec_index;
pub use pui_vec_index::{BuildPuiVecIndex, PuiVecAccess, PuiVecIndex};
#[cfg(feature = "pui-core")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Id<T> {
index: usize,
token: T,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct PuiVec<T, I> {
ident: I,
vec: Vec<T>,
}
impl<T, I> From<PuiVec<T, I>> for Vec<T> {
fn from(pui_vec: PuiVec<T, I>) -> Self { pui_vec.vec }
}
#[cfg(feature = "pui-core")]
impl<T> Id<T> {
pub const unsafe fn new_unchecked(index: usize, token: T) -> Self { Self { index, token } }
pub fn into_raw_parts(self) -> (usize, T) { (self.index, self.token) }
pub const fn get(&self) -> usize { self.index }
pub const fn token(&self) -> &T { &self.token }
}
impl<T, I> PuiVec<T, I> {
pub const fn new(ident: I) -> Self { Self::from_raw_parts(Vec::new(), ident) }
pub const fn from_raw_parts(vec: Vec<T>, ident: I) -> Self { Self { vec, ident } }
pub const fn ident(&self) -> &I { &self.ident }
pub fn is_empty(&self) -> bool { self.vec.is_empty() }
pub fn len(&self) -> usize { self.vec.len() }
pub fn capacity(&self) -> usize { self.vec.capacity() }
pub fn reserve(&mut self, additional: usize) { self.vec.reserve(additional) }
pub fn get<A: PuiVecAccess<T, I>>(&self, index: A) -> Option<&A::Output> { index.get(self) }
pub fn get_mut<A: PuiVecAccess<T, I>>(&mut self, index: A) -> Option<&mut A::Output> { index.get_mut(self) }
pub fn as_mut_parts(&mut self) -> (&I, &mut [T]) { (&self.ident, &mut self.vec) }
pub unsafe fn into_raw_parts(self) -> (I, Vec<T>) { (self.ident, self.vec) }
}
impl<T> PuiVec<T, ()> {
pub fn vec_mut(&mut self) -> &mut Vec<T> { &mut self.vec }
}
impl<T, I> PuiVec<T, I> {
pub fn push<Id: BuildPuiVecIndex<I, SliceIndex = usize>>(&mut self, value: T) -> Id {
let index = self.vec.len();
self.vec.push(value);
unsafe { Id::new_unchecked(index, &self.ident) }
}
pub fn append(&mut self, vec: &mut Vec<T>) { self.vec.append(vec); }
pub fn extend_from_slice(&mut self, slice: &[T])
where
T: Clone,
{
self.vec.extend_from_slice(slice);
}
}
#[cfg(feature = "pui-core")]
impl<T, I: OneShotIdentifier> PuiVec<T, I> {
pub fn ids(&self) -> impl ExactSizeIterator<Item = Id<I::Token>> + Clone {
let token = self.ident.token();
(0..self.len()).map(move |index| Id {
index,
token: token.clone(),
})
}
pub fn parse_id(&self, index: usize) -> Option<Id<I::Token>> {
if index < self.len() {
Some(Id {
index,
token: self.ident.token(),
})
} else {
None
}
}
pub fn swap(&mut self, a: Id<I::Token>, b: Id<I::Token>) {
assert!(self.ident.owns_token(&a.token) && self.ident.owns_token(&b.token));
let ptr = self.vec.as_mut_ptr();
unsafe { ptr.add(a.index).swap(ptr.add(b.index)) }
}
pub fn split_at(&self, mid: Id<I::Token>) -> (&[T], &[T]) {
assert!(self.ident.owns_token(&mid.token));
let len = self.len();
let ptr = self.vec.as_ptr();
unsafe {
(
core::slice::from_raw_parts(ptr, mid.index),
core::slice::from_raw_parts(ptr.add(mid.index), len - mid.index),
)
}
}
pub fn split_at_mut(&mut self, id: Id<I::Token>) -> (&mut [T], &mut [T]) {
assert!(self.ident.owns_token(&id.token));
let len = self.len();
let ptr = self.vec.as_mut_ptr();
unsafe {
(
core::slice::from_raw_parts_mut(ptr, id.index),
core::slice::from_raw_parts_mut(ptr.add(id.index), len - id.index),
)
}
}
}
impl<T, I> IntoIterator for PuiVec<T, I> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter { self.vec.into_iter() }
}
impl<A, T, I> Extend<A> for PuiVec<T, I>
where
Vec<T>: Extend<A>,
{
fn extend<Iter: IntoIterator<Item = A>>(&mut self, iter: Iter) { self.vec.extend(iter) }
}
impl<T, I, A> Index<A> for PuiVec<T, I>
where
A: PuiVecAccess<T, I>,
{
type Output = A::Output;
fn index(&self, index: A) -> &Self::Output { index.index(self) }
}
impl<T, I, A> IndexMut<A> for PuiVec<T, I>
where
A: PuiVecAccess<T, I>,
{
fn index_mut(&mut self, index: A) -> &mut Self::Output { index.index_mut(self) }
}
impl<T, I> Deref for PuiVec<T, I> {
type Target = [T];
fn deref(&self) -> &Self::Target { &self.vec }
}
impl<T, I> DerefMut for PuiVec<T, I> {
fn deref_mut(&mut self) -> &mut Self::Target { &mut self.vec }
}