1#![allow(unused_variables)]
2#![allow(missing_docs)]
3
4use super::*;
5
6pub struct SliceVec<'s, T> {
17 data: &'s mut [T],
18 len: usize,
19}
20
21impl<'s, T> Default for SliceVec<'s, T> {
22 #[inline(always)]
23 #[must_use]
24 fn default() -> Self {
25 Self { data: &mut [], len: 0 }
26 }
27}
28
29impl<'s, T> Deref for SliceVec<'s, T> {
30 type Target = [T];
31 #[inline(always)]
32 #[must_use]
33 fn deref(&self) -> &Self::Target {
34 &self.data[..self.len]
35 }
36}
37
38impl<'s, T> DerefMut for SliceVec<'s, T> {
39 #[inline(always)]
40 #[must_use]
41 fn deref_mut(&mut self) -> &mut Self::Target {
42 &mut self.data[..self.len]
43 }
44}
45
46impl<'s, T, I> Index<I> for SliceVec<'s, T>
47where
48 I: SliceIndex<[T]>,
49{
50 type Output = <I as SliceIndex<[T]>>::Output;
51 #[inline(always)]
52 #[must_use]
53 fn index(&self, index: I) -> &Self::Output {
54 &self.deref()[index]
55 }
56}
57
58impl<'s, T, I> IndexMut<I> for SliceVec<'s, T>
59where
60 I: SliceIndex<[T]>,
61{
62 #[inline(always)]
63 #[must_use]
64 fn index_mut(&mut self, index: I) -> &mut Self::Output {
65 &mut self.deref_mut()[index]
66 }
67}
68
69impl<'s, T> SliceVec<'s, T> {
70 #[inline]
71 pub fn append(&mut self, other: &mut Self)
72 where
73 T: Default,
74 {
75 for item in other.drain(..) {
76 self.push(item)
77 }
78 }
79
80 #[inline(always)]
86 #[must_use]
87 pub fn as_mut_ptr(&mut self) -> *mut T {
88 self.data.as_mut_ptr()
89 }
90
91 #[inline(always)]
93 #[must_use]
94 pub fn as_mut_slice(&mut self) -> &mut [T] {
95 self.deref_mut()
96 }
97
98 #[inline(always)]
104 #[must_use]
105 pub fn as_ptr(&self) -> *const T {
106 self.data.as_ptr()
107 }
108
109 #[inline(always)]
111 #[must_use]
112 pub fn as_slice(&self) -> &[T] {
113 self.deref()
114 }
115
116 #[inline(always)]
120 #[must_use]
121 pub fn capacity(&self) -> usize {
122 self.data.len()
123 }
124
125 #[inline(always)]
127 pub fn clear(&mut self)
128 where
129 T: Default,
130 {
131 self.truncate(0)
132 }
133
134 #[inline]
154 pub fn drain<'p, R: RangeBounds<usize>>(
155 &'p mut self, range: R,
156 ) -> SliceVecDrain<'p, 's, T>
157 where
158 T: Default,
159 {
160 use core::ops::Bound;
161 let start = match range.start_bound() {
162 Bound::Included(x) => *x,
163 Bound::Excluded(x) => x.saturating_add(1),
164 Bound::Unbounded => 0,
165 };
166 let end = match range.end_bound() {
167 Bound::Included(x) => x.saturating_add(1),
168 Bound::Excluded(x) => *x,
169 Bound::Unbounded => self.len,
170 };
171 assert!(
172 start <= end,
173 "SliceVec::drain> Illegal range, {} to {}",
174 start,
175 end
176 );
177 assert!(
178 end <= self.len,
179 "SliceVec::drain> Range ends at {} but length is only {}!",
180 end,
181 self.len
182 );
183 SliceVecDrain {
184 parent: self,
185 target_start: start,
186 target_index: start,
187 target_end: end,
188 }
189 }
190
191 #[inline]
192 pub fn extend_from_slice(&mut self, sli: &[T])
193 where
194 T: Clone,
195 {
196 if sli.is_empty() {
197 return;
198 }
199
200 let new_len = self.len + sli.len();
201 if new_len > self.capacity() {
202 panic!(
203 "SliceVec::extend_from_slice> total length {} exceeds capacity {}",
204 new_len,
205 self.capacity()
206 )
207 }
208
209 let target = &mut self.data[self.len..new_len];
210 target.clone_from_slice(sli);
211 self.set_len(new_len);
212 }
213
214 #[inline]
241 pub fn fill<I: IntoIterator<Item = T>>(&mut self, iter: I) -> I::IntoIter {
242 let mut iter = iter.into_iter();
243 for element in iter.by_ref().take(self.capacity() - self.len()) {
244 self.push(element);
245 }
246 iter
247 }
248
249 #[inline]
258 #[must_use]
259 #[allow(clippy::match_wild_err_arm)]
260 pub fn from_slice_len(data: &'s mut [T], len: usize) -> Self {
261 assert!(len <= data.len());
262 Self { data, len }
263 }
264
265 #[inline]
283 pub fn insert(&mut self, index: usize, item: T) {
284 if index > self.len {
285 panic!("SliceVec::insert> index {} is out of bounds {}", index, self.len);
286 }
287
288 self.push(item);
290 self.as_mut_slice()[index..].rotate_right(1);
292 }
293
294 #[inline(always)]
296 #[must_use]
297 pub fn is_empty(&self) -> bool {
298 self.len == 0
299 }
300
301 #[inline(always)]
303 #[must_use]
304 pub fn len(&self) -> usize {
305 self.len
306 }
307
308 #[inline]
323 pub fn pop(&mut self) -> Option<T>
324 where
325 T: Default,
326 {
327 if self.len > 0 {
328 self.len -= 1;
329 let out = core::mem::take(&mut self.data[self.len]);
330 Some(out)
331 } else {
332 None
333 }
334 }
335
336 #[inline(always)]
354 pub fn push(&mut self, val: T) {
355 if self.len < self.capacity() {
356 self.data[self.len] = val;
357 self.len += 1;
358 } else {
359 panic!("SliceVec::push> capacity overflow")
360 }
361 }
362
363 #[inline]
381 pub fn remove(&mut self, index: usize) -> T
382 where
383 T: Default,
384 {
385 let targets: &mut [T] = &mut self.deref_mut()[index..];
386 let item = core::mem::take(&mut targets[0]);
387 targets.rotate_left(1);
388 self.len -= 1;
389 item
390 }
391
392 #[inline]
412 pub fn resize(&mut self, new_len: usize, new_val: T)
413 where
414 T: Clone,
415 {
416 self.resize_with(new_len, || new_val.clone())
417 }
418
419 #[inline]
446 pub fn resize_with<F: FnMut() -> T>(&mut self, new_len: usize, mut f: F) {
447 match new_len.checked_sub(self.len) {
448 None => {
449 if needs_drop::<T>() {
450 while self.len() > new_len {
451 self.len -= 1;
452 self.data[self.len] = f();
453 }
454 } else {
455 self.len = new_len;
456 }
457 }
458 Some(new_elements) => {
459 for _ in 0..new_elements {
460 self.push(f());
461 }
462 }
463 }
464 }
465
466 #[inline]
479 pub fn retain<F: FnMut(&T) -> bool>(&mut self, mut acceptable: F)
480 where
481 T: Default,
482 {
483 struct JoinOnDrop<'vec, Item> {
486 items: &'vec mut [Item],
487 done_end: usize,
488 tail_start: usize,
490 }
491
492 impl<Item> Drop for JoinOnDrop<'_, Item> {
493 fn drop(&mut self) {
494 self.items[self.done_end..].rotate_left(self.tail_start);
495 }
496 }
497
498 let mut rest = JoinOnDrop { items: self.data, done_end: 0, tail_start: 0 };
499
500 for idx in 0..self.len {
501 if !acceptable(&rest.items[idx]) {
503 let _ = core::mem::take(&mut rest.items[idx]);
504 self.len -= 1;
505 rest.tail_start += 1;
506 } else {
507 rest.items.swap(rest.done_end, idx);
508 rest.done_end += 1;
509 }
510 }
511 }
512
513 #[inline(always)]
524 pub fn set_len(&mut self, new_len: usize) {
525 if new_len > self.capacity() {
526 panic!(
531 "SliceVec::set_len> new length {} exceeds capacity {}",
532 new_len,
533 self.capacity()
534 )
535 } else {
536 self.len = new_len;
537 }
538 }
539
540 #[inline]
559 pub fn split_off<'a>(&'a mut self, at: usize) -> SliceVec<'s, T> {
560 let mut new = Self::default();
561 let backing: &'s mut [T] = core::mem::take(&mut self.data);
562 let (me, other) = backing.split_at_mut(at);
563 new.len = self.len - at;
564 new.data = other;
565 self.len = me.len();
566 self.data = me;
567 new
568 }
569
570 #[inline]
588 pub fn swap_remove(&mut self, index: usize) -> T
589 where
590 T: Default,
591 {
592 assert!(
593 index < self.len,
594 "SliceVec::swap_remove> index {} is out of bounds {}",
595 index,
596 self.len
597 );
598 if index == self.len - 1 {
599 self.pop().unwrap()
600 } else {
601 let i = self.pop().unwrap();
602 replace(&mut self[index], i)
603 }
604 }
605
606 #[inline]
610 pub fn truncate(&mut self, new_len: usize)
611 where
612 T: Default,
613 {
614 if needs_drop::<T>() {
615 while self.len > new_len {
616 self.pop();
617 }
618 } else {
619 self.len = self.len.min(new_len);
620 }
621 }
622
623 #[inline]
633 pub fn try_from_slice_len(data: &'s mut [T], len: usize) -> Option<Self> {
634 if len <= data.len() {
635 Some(Self { data, len })
636 } else {
637 None
638 }
639 }
640}
641
642#[cfg(feature = "grab_spare_slice")]
643impl<'s, T> SliceVec<'s, T> {
644 #[inline(always)]
659 pub fn grab_spare_slice(&self) -> &[T] {
660 &self.data[self.len..]
661 }
662
663 #[inline(always)]
676 pub fn grab_spare_slice_mut(&mut self) -> &mut [T] {
677 &mut self.data[self.len..]
678 }
679}
680
681impl<'s, T> From<&'s mut [T]> for SliceVec<'s, T> {
682 #[inline]
690 fn from(data: &'s mut [T]) -> Self {
691 let len = data.len();
692 Self { data, len }
693 }
694}
695
696impl<'s, T, A> From<&'s mut A> for SliceVec<'s, T>
697where
698 A: AsMut<[T]>,
699{
700 #[inline]
708 fn from(a: &'s mut A) -> Self {
709 let data = a.as_mut();
710 let len = data.len();
711 Self { data, len }
712 }
713}
714
715pub struct SliceVecDrain<'p, 's, T: Default> {
719 parent: &'p mut SliceVec<'s, T>,
720 target_start: usize,
721 target_index: usize,
722 target_end: usize,
723}
724impl<'p, 's, T: Default> Iterator for SliceVecDrain<'p, 's, T> {
725 type Item = T;
726 #[inline]
727 fn next(&mut self) -> Option<Self::Item> {
728 if self.target_index != self.target_end {
729 let out = core::mem::take(&mut self.parent[self.target_index]);
730 self.target_index += 1;
731 Some(out)
732 } else {
733 None
734 }
735 }
736}
737impl<'p, 's, T: Default> FusedIterator for SliceVecDrain<'p, 's, T> {}
738impl<'p, 's, T: Default> Drop for SliceVecDrain<'p, 's, T> {
739 #[inline]
740 fn drop(&mut self) {
741 self.for_each(drop);
744 let count = self.target_end - self.target_start;
746 let targets: &mut [T] = &mut self.parent.deref_mut()[self.target_start..];
747 targets.rotate_left(count);
748 self.parent.len -= count;
749 }
750}
751
752impl<'s, T> AsMut<[T]> for SliceVec<'s, T> {
753 #[inline(always)]
754 #[must_use]
755 fn as_mut(&mut self) -> &mut [T] {
756 &mut *self
757 }
758}
759
760impl<'s, T> AsRef<[T]> for SliceVec<'s, T> {
761 #[inline(always)]
762 #[must_use]
763 fn as_ref(&self) -> &[T] {
764 &*self
765 }
766}
767
768impl<'s, T> Borrow<[T]> for SliceVec<'s, T> {
769 #[inline(always)]
770 #[must_use]
771 fn borrow(&self) -> &[T] {
772 &*self
773 }
774}
775
776impl<'s, T> BorrowMut<[T]> for SliceVec<'s, T> {
777 #[inline(always)]
778 #[must_use]
779 fn borrow_mut(&mut self) -> &mut [T] {
780 &mut *self
781 }
782}
783
784impl<'s, T> Extend<T> for SliceVec<'s, T> {
785 #[inline]
786 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
787 for t in iter {
788 self.push(t)
789 }
790 }
791}
792
793impl<'s, T> IntoIterator for SliceVec<'s, T> {
794 type Item = &'s mut T;
795 type IntoIter = core::slice::IterMut<'s, T>;
796 #[inline(always)]
797 #[must_use]
798 fn into_iter(self) -> Self::IntoIter {
799 self.data.iter_mut()
800 }
801}
802
803impl<'s, T> PartialEq for SliceVec<'s, T>
804where
805 T: PartialEq,
806{
807 #[inline]
808 #[must_use]
809 fn eq(&self, other: &Self) -> bool {
810 self.as_slice().eq(other.as_slice())
811 }
812}
813impl<'s, T> Eq for SliceVec<'s, T> where T: Eq {}
814
815impl<'s, T> PartialOrd for SliceVec<'s, T>
816where
817 T: PartialOrd,
818{
819 #[inline]
820 #[must_use]
821 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
822 self.as_slice().partial_cmp(other.as_slice())
823 }
824}
825impl<'s, T> Ord for SliceVec<'s, T>
826where
827 T: Ord,
828{
829 #[inline]
830 #[must_use]
831 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
832 self.as_slice().cmp(other.as_slice())
833 }
834}
835
836impl<'s, T> PartialEq<&[T]> for SliceVec<'s, T>
837where
838 T: PartialEq,
839{
840 #[inline]
841 #[must_use]
842 fn eq(&self, other: &&[T]) -> bool {
843 self.as_slice().eq(*other)
844 }
845}
846
847impl<'s, T> Hash for SliceVec<'s, T>
848where
849 T: Hash,
850{
851 #[inline]
852 fn hash<H: Hasher>(&self, state: &mut H) {
853 self.as_slice().hash(state)
854 }
855}
856
857#[cfg(feature = "experimental_write_impl")]
858impl<'s> core::fmt::Write for SliceVec<'s, u8> {
859 fn write_str(&mut self, s: &str) -> core::fmt::Result {
860 let my_len = self.len();
861 let str_len = s.as_bytes().len();
862 if my_len + str_len <= self.capacity() {
863 let remainder = &mut self.data[my_len..];
864 let target = &mut remainder[..str_len];
865 target.copy_from_slice(s.as_bytes());
866 Ok(())
867 } else {
868 Err(core::fmt::Error)
869 }
870 }
871}
872
873impl<'s, T> Binary for SliceVec<'s, T>
878where
879 T: Binary,
880{
881 #[allow(clippy::missing_inline_in_public_items)]
882 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
883 write!(f, "[")?;
884 if f.alternate() {
885 write!(f, "\n ")?;
886 }
887 for (i, elem) in self.iter().enumerate() {
888 if i > 0 {
889 write!(f, ",{}", if f.alternate() { "\n " } else { " " })?;
890 }
891 Binary::fmt(elem, f)?;
892 }
893 if f.alternate() {
894 write!(f, ",\n")?;
895 }
896 write!(f, "]")
897 }
898}
899
900impl<'s, T> Debug for SliceVec<'s, T>
901where
902 T: Debug,
903{
904 #[allow(clippy::missing_inline_in_public_items)]
905 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
906 write!(f, "[")?;
907 if f.alternate() && !self.is_empty() {
908 write!(f, "\n ")?;
909 }
910 for (i, elem) in self.iter().enumerate() {
911 if i > 0 {
912 write!(f, ",{}", if f.alternate() { "\n " } else { " " })?;
913 }
914 Debug::fmt(elem, f)?;
915 }
916 if f.alternate() && !self.is_empty() {
917 write!(f, ",\n")?;
918 }
919 write!(f, "]")
920 }
921}
922
923impl<'s, T> Display for SliceVec<'s, T>
924where
925 T: Display,
926{
927 #[allow(clippy::missing_inline_in_public_items)]
928 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
929 write!(f, "[")?;
930 if f.alternate() {
931 write!(f, "\n ")?;
932 }
933 for (i, elem) in self.iter().enumerate() {
934 if i > 0 {
935 write!(f, ",{}", if f.alternate() { "\n " } else { " " })?;
936 }
937 Display::fmt(elem, f)?;
938 }
939 if f.alternate() {
940 write!(f, ",\n")?;
941 }
942 write!(f, "]")
943 }
944}
945
946impl<'s, T> LowerExp for SliceVec<'s, T>
947where
948 T: LowerExp,
949{
950 #[allow(clippy::missing_inline_in_public_items)]
951 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
952 write!(f, "[")?;
953 if f.alternate() {
954 write!(f, "\n ")?;
955 }
956 for (i, elem) in self.iter().enumerate() {
957 if i > 0 {
958 write!(f, ",{}", if f.alternate() { "\n " } else { " " })?;
959 }
960 LowerExp::fmt(elem, f)?;
961 }
962 if f.alternate() {
963 write!(f, ",\n")?;
964 }
965 write!(f, "]")
966 }
967}
968
969impl<'s, T> LowerHex for SliceVec<'s, T>
970where
971 T: LowerHex,
972{
973 #[allow(clippy::missing_inline_in_public_items)]
974 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
975 write!(f, "[")?;
976 if f.alternate() {
977 write!(f, "\n ")?;
978 }
979 for (i, elem) in self.iter().enumerate() {
980 if i > 0 {
981 write!(f, ",{}", if f.alternate() { "\n " } else { " " })?;
982 }
983 LowerHex::fmt(elem, f)?;
984 }
985 if f.alternate() {
986 write!(f, ",\n")?;
987 }
988 write!(f, "]")
989 }
990}
991
992impl<'s, T> Octal for SliceVec<'s, T>
993where
994 T: Octal,
995{
996 #[allow(clippy::missing_inline_in_public_items)]
997 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
998 write!(f, "[")?;
999 if f.alternate() {
1000 write!(f, "\n ")?;
1001 }
1002 for (i, elem) in self.iter().enumerate() {
1003 if i > 0 {
1004 write!(f, ",{}", if f.alternate() { "\n " } else { " " })?;
1005 }
1006 Octal::fmt(elem, f)?;
1007 }
1008 if f.alternate() {
1009 write!(f, ",\n")?;
1010 }
1011 write!(f, "]")
1012 }
1013}
1014
1015impl<'s, T> Pointer for SliceVec<'s, T>
1016where
1017 T: Pointer,
1018{
1019 #[allow(clippy::missing_inline_in_public_items)]
1020 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
1021 write!(f, "[")?;
1022 if f.alternate() {
1023 write!(f, "\n ")?;
1024 }
1025 for (i, elem) in self.iter().enumerate() {
1026 if i > 0 {
1027 write!(f, ",{}", if f.alternate() { "\n " } else { " " })?;
1028 }
1029 Pointer::fmt(elem, f)?;
1030 }
1031 if f.alternate() {
1032 write!(f, ",\n")?;
1033 }
1034 write!(f, "]")
1035 }
1036}
1037
1038impl<'s, T> UpperExp for SliceVec<'s, T>
1039where
1040 T: UpperExp,
1041{
1042 #[allow(clippy::missing_inline_in_public_items)]
1043 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
1044 write!(f, "[")?;
1045 if f.alternate() {
1046 write!(f, "\n ")?;
1047 }
1048 for (i, elem) in self.iter().enumerate() {
1049 if i > 0 {
1050 write!(f, ",{}", if f.alternate() { "\n " } else { " " })?;
1051 }
1052 UpperExp::fmt(elem, f)?;
1053 }
1054 if f.alternate() {
1055 write!(f, ",\n")?;
1056 }
1057 write!(f, "]")
1058 }
1059}
1060
1061impl<'s, T> UpperHex for SliceVec<'s, T>
1062where
1063 T: UpperHex,
1064{
1065 #[allow(clippy::missing_inline_in_public_items)]
1066 fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
1067 write!(f, "[")?;
1068 if f.alternate() {
1069 write!(f, "\n ")?;
1070 }
1071 for (i, elem) in self.iter().enumerate() {
1072 if i > 0 {
1073 write!(f, ",{}", if f.alternate() { "\n " } else { " " })?;
1074 }
1075 UpperHex::fmt(elem, f)?;
1076 }
1077 if f.alternate() {
1078 write!(f, ",\n")?;
1079 }
1080 write!(f, "]")
1081 }
1082}