1#![doc = include_str!("../README.md")]
2#![no_std]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4
5extern crate alloc;
6
7#[cfg(doc)]
8use alloc::{vec::Vec, string::String};
9
10use core::{
11 iter::once,
12 ops::{Bound, Deref, DerefMut, Index, IndexMut, Range, RangeBounds},
13 slice::SliceIndex,
14};
15
16mod util;
17mod slice;
18mod offset;
19mod vec_like;
20
21pub use slice::*;
22pub use offset::*;
23pub use vec_like::*;
24
25mod externs {
26 mod core_impls;
27}
28
29#[derive(Debug, Clone, Default)]
50pub struct OffsetVec<V: VecLike> {
51 vec: V,
52 offset: usize,
53}
54
55impl<V: VecLike> Deref for OffsetVec<V> {
56 type Target = V::Slice;
57
58 #[inline]
59 fn deref(&self) -> &Self::Target {
60 let offset = self.offset;
61 let slice = self.vec.as_slice();
62 &slice[offset..]
63 }
64}
65
66impl<V: VecLike> DerefMut for OffsetVec<V> {
67 #[inline]
68 fn deref_mut(&mut self) -> &mut Self::Target {
69 let offset = self.offset;
70 let slice_mut = self.vec.as_mut_slice();
71 &mut slice_mut[offset..]
72 }
73}
74
75impl<V, I> Index<I> for OffsetVec<V>
76where V: VecLike,
77 I: SliceIndex<V::Slice>,
78 V::Slice: Index<I>,
79{
80 type Output = <V::Slice as Index<I>>::Output;
81
82 #[track_caller]
83 fn index(&self, index: I) -> &Self::Output {
84 let slice = &**self;
85 &slice[index]
86 }
87}
88
89impl<V, I> IndexMut<I> for OffsetVec<V>
90where V: VecLike,
91 I: SliceIndex<V::Slice>,
92 V::Slice: IndexMut<I>,
93{
94 #[track_caller]
95 fn index_mut(&mut self, index: I) -> &mut Self::Output {
96 let slice = &mut **self;
97 &mut slice[index]
98 }
99}
100
101impl<V: VecLike> OffsetVec<V> {
102 #[inline]
103 pub fn origin_vec(&self) -> &V {
104 &self.vec
105 }
106
107 #[inline]
108 pub fn origin_vec_mut(&mut self) -> &mut V {
109 &mut self.vec
110 }
111
112 #[inline]
113 pub fn into_origin_vec(self) -> V {
114 self.vec
115 }
116
117 #[inline]
118 pub fn as_slice(&self) -> &V::Slice {
119 self
120 }
121
122 #[inline]
123 pub fn as_mut_slice(&mut self) -> &mut V::Slice {
124 self
125 }
126
127 pub fn iter<'a>(&'a self) -> <&'a V::Slice as IntoIterator>::IntoIter
128 where &'a V::Slice: IntoIterator
129 {
130 self.as_slice().into_iter()
131 }
132
133 pub fn iter_mut<'a>(&'a mut self) -> <&'a V::Slice as IntoIterator>::IntoIter
134 where &'a V::Slice: IntoIterator
135 {
136 self.as_mut_slice().into_iter()
137 }
138
139 #[inline]
140 pub fn is_empty(&self) -> bool {
141 self.len() == 0
142 }
143
144 #[inline]
145 pub fn len(&self) -> usize {
146 self.vec.len() - self.offset
147 }
148
149 #[inline]
150 pub fn capacity(&self) -> usize {
151 self.vec.capacity() - self.offset
152 }
153
154 pub fn reserve(&mut self, additional: usize) {
155 self.vec.reserve(additional);
156 }
157
158 pub fn reserve_exact(&mut self, additional: usize) {
159 self.vec.reserve_exact(additional);
160 }
161
162 pub fn shrink_to_fit(&mut self) {
163 self.vec.shrink_to_fit();
164 }
165
166 pub fn shrink_to(&mut self, min_capacity: usize) {
167 self.vec.shrink_to(min_capacity);
168 }
169
170 pub fn origin_offset(&self) -> usize {
171 self.offset
172 }
173
174 pub fn push(&mut self, value: V::Elem) {
175 self.vec.push(value);
176 }
177
178 pub fn pop(&mut self) -> Option<V::Elem> {
179 if self.is_empty() {
180 return None;
181 }
182
183 self.vec.pop()
184 }
185
186 #[track_caller]
187 pub fn remove(&mut self, index: usize) -> V::Elem {
188 let len = self.len();
189 if index > len {
190 index_out_of_range(index, self.offset, len)
191 }
192 self.vec.remove(index + self.offset)
193 }
194
195 #[track_caller]
196 pub fn insert(&mut self, index: usize, elem: V::Elem) {
197 let len = self.len();
198 if index > len {
199 index_out_of_range(index, self.offset, len)
200 }
201 self.vec.insert(index + self.offset, elem)
202 }
203
204 pub fn truncate(&mut self, len: usize) {
205 self.vec.truncate(len + self.offset);
206 }
207
208 pub fn append(&mut self, other: &mut V::Collection) {
209 self.vec.append(other);
210 }
211
212 pub fn clear(&mut self) {
213 self.truncate(self.offset);
214 }
215
216 #[track_caller]
217 fn map_range<R: RangeBounds<usize>>(&self, range: R) -> Range<usize> {
218 let start = match range.start_bound() {
219 Bound::Included(&n) => n,
220 Bound::Excluded(&n) => n.checked_add(1).expect("start range overflow"),
221 Bound::Unbounded => 0,
222 };
223 let end = match range.end_bound() {
224 Bound::Included(&n) => n.checked_add(1).expect("end range overflow"),
225 Bound::Excluded(&n) => n,
226 Bound::Unbounded => self.len(),
227 };
228 if start > end {
229 #[cold]
230 #[track_caller]
231 #[inline(never)]
232 fn fail(index: usize, end: usize) -> ! {
233 panic!("range index starts at {index} but ends at {end}");
234 }
235 fail(start, end)
236 }
237 if end > self.len() {
238 #[cold]
239 #[track_caller]
240 #[inline(never)]
241 fn fail(index: usize, len: usize) -> ! {
242 panic!("range end index {index} out of range for slice of length {len}");
243 }
244 fail(end, self.len())
245 }
246 let offset = self.offset;
247 Range { start: start+offset, end: end+offset }
248 }
249
250 #[track_caller]
251 pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> V::Drain<'_> {
252 self.vec.drain(self.map_range(range))
253 }
254
255 #[track_caller]
256 #[must_use = "use `.truncate()` if you don't need the other half"]
257 pub fn split_off(&mut self, at: usize) -> V::Collection {
258 let len = self.len();
259 if at > len {
260 index_out_of_range(at, self.offset, len)
261 }
262 self.vec.split_off(at + self.offset)
263 }
264
265 pub fn resize(&mut self, new_len: usize, value: V::Elem)
266 where V::Elem: Clone,
267 {
268 self.vec.resize(new_len+self.offset, value);
269 }
270
271 pub fn resize_with<F>(&mut self, new_len: usize, f: F)
272 where F: FnMut() -> V::Elem,
273 {
274 self.vec.resize_with(new_len+self.offset, f);
275 }
276
277 pub fn retain<F>(&mut self, mut f: F)
278 where F: FnMut(V::ElemRef<'_>) -> bool,
279 {
280 let i = self.vec.as_slice().transform_index(self.offset);
281
282 let mut i = 0..i;
283 self.vec.retain(|elem| {
284 i.next().is_some() || f(elem)
285 });
286 }
287}
288
289impl<V: VecLikeSolid> OffsetVec<V> {
290 pub fn retain_mut<F: FnMut(&mut V::Elem) -> bool>(&mut self, mut f: F) {
291 let mut i = 0..self.offset;
292 self.vec.retain_mut(|elem| {
293 i.next().is_some() || f(elem)
294 });
295 }
296
297 #[track_caller]
298 pub fn swap_remove(&mut self, index: usize) -> V::Elem {
299 let len = self.len();
300 if index > len {
301 index_out_of_range(index, self.offset, len)
302 }
303 self.vec.swap_remove(index + self.offset)
304 }
305
306 pub fn pop_if<F>(&mut self, predicate: F) -> Option<V::Elem>
307 where F: FnOnce(&mut V::Elem) -> bool,
308 {
309 if self.is_empty() {
310 return None;
311 }
312
313 self.vec.pop_if(predicate)
314 }
315}
316
317impl<V: VecLike<Slice = str>> OffsetVec<V> {
318 #[inline]
319 pub fn as_str(&self) -> &str {
320 self
321 }
322
323 #[inline]
324 pub fn as_mut_str(&mut self) -> &mut str {
325 self
326 }
327
328 pub fn push_str<'a>(&mut self, s: &'a str)
329 where V::Collection: Extend<&'a str>,
330 {
331 self.vec.as_mut_collection().extend(once(s));
332 }
333}
334
335#[cold]
336#[track_caller]
337#[inline(never)]
338fn index_out_of_range(index: usize, offset: usize, len: usize) -> ! {
339 panic!("offset index ({index} -> {}) out of length (is {len} -> {})",
340 index+offset,
341 len+offset);
342}