1#![allow(clippy::partialeq_ne_impl)]
2use crate::raw_cev::RawCev;
3use core::borrow::{Borrow, BorrowMut};
4use core::cmp::Ordering;
5use core::fmt;
6use core::mem::{self, ManuallyDrop, MaybeUninit};
7use core::ops::{self, Index, IndexMut};
8use core::ptr::{self, NonNull};
9use core::slice::{self, SliceIndex};
10
11pub struct Cev<T> {
27 buf: RawCev<T>,
28 len: usize,
29}
30
31impl<T> Cev<T> {
32 #[inline]
47 pub fn append(&mut self, other: &mut Self) {
48 unsafe {
49 self.append_elements(other.as_slice() as _);
50 other.set_len_ptr(0);
51 }
52 }
53
54 #[inline]
80 pub fn as_mut_ptr(&mut self) -> *mut T {
81 self.buf.ptr()
82 }
83
84 #[inline]
86 pub fn as_mut_slice(&mut self) -> &mut [T] {
87 self
88 }
89
90 #[inline]
113 pub fn as_ptr(&self) -> *const T {
114 self.buf.ptr()
115 }
116
117 #[inline]
119 pub fn as_slice(&self) -> &[T] {
120 self
121 }
122
123 #[inline]
140 pub fn capacity(&self) -> usize {
141 self.buf.capacity()
142 }
143
144 #[inline]
159 pub fn clear(&mut self) {
160 let elems: *mut [T] = self.as_mut_slice();
161
162 unsafe {
163 self.set_len_ptr(0);
164 ptr::drop_in_place(elems);
165 }
166 }
167
168 #[inline]
170 pub unsafe fn from_raw_parts(mov_ptr: *mut T, raw_ptr: *mut T, len: usize, cap: usize) -> Self {
171 unsafe {
172 Cev {
173 buf: RawCev::from_raw_parts_ptr(mov_ptr, raw_ptr, cap),
174 len,
175 }
176 }
177 }
178
179 #[inline]
194 pub fn from_vec(mut vec: Vec<T>) -> Self {
195 let (raw_ptr, len, capacity) = (vec.as_mut_ptr(), vec.len(), vec.capacity());
196 unsafe {
197 vec.set_len(0);
198 mem::forget(vec);
199
200 Cev::from_raw_parts(
201 if capacity == len {
202 raw_ptr
203 } else if len == 0 {
204 raw_ptr.add(capacity - 1)
205 } else {
206 let mov_ptr = raw_ptr.add(capacity - len);
207 ptr::copy(raw_ptr, mov_ptr, len);
208 mov_ptr
209 },
210 raw_ptr,
211 len,
212 capacity,
213 )
214 }
215 }
216
217 pub fn insert(&mut self, index: usize, element: T) {
236 #[cold]
237 #[inline(never)]
238 fn assert_failed(index: usize, len: usize) -> ! {
239 panic!("insertion index (is {index}) should be <= len (is {len})");
240 }
241
242 let len = self.len();
243
244 if len == self.buf.capacity() {
245 self.reserve(1);
246 }
247
248 unsafe {
249 if index == 0 {
250 if len != 0 {
251 self.buf.mov_ptr_sub(1)
252 }
253 } else if index <= len {
254 let p = self.as_mut_ptr();
255 ptr::copy(p, p.sub(1), index);
256 self.buf.mov_ptr_sub(1)
257 } else {
258 assert_failed(index, len);
259 }
260
261 ptr::write(self.as_mut_ptr().add(index), element);
262 self.set_len(len + 1);
263 }
264 }
265
266 #[inline]
281 pub fn into_vec(self) -> Vec<T> {
282 let (ptr, len, capacity) = (self.buf.raw_ptr(), self.len(), self.capacity());
283 unsafe {
284 if capacity != len {
285 ptr::copy(self.as_ptr(), ptr, len);
286 }
287
288 mem::forget(self);
289 Vec::from_raw_parts(ptr, len, capacity)
290 }
291 }
292
293 pub fn is_empty(&self) -> bool {
307 self.len() == 0
308 }
309
310 #[inline]
321 pub fn len(&self) -> usize {
322 self.len
323 }
324
325 #[inline]
335 pub const fn new() -> Self {
336 Cev {
337 buf: RawCev::NEW,
338 len: 0,
339 }
340 }
341
342 #[inline]
358 pub fn pop(&mut self) -> Option<T> {
359 if self.len == 0 {
360 None
361 } else {
362 unsafe {
363 self.len -= 1;
364 let ptr = self.as_ptr();
365 if self.len != 0 {
366 self.buf.mov_ptr_add(1);
367 }
368 Some(ptr::read(ptr))
369 }
370 }
371 }
372
373 #[inline]
391 pub fn push(&mut self, value: T) {
392 if self.len == self.capacity() {
393 self.buf.reserve_for_push(self.len);
394 }
395 unsafe {
396 if self.len != 0 {
397 self.buf.mov_ptr_sub(1);
398 }
399 self.as_mut_ptr().write(value);
400 self.len += 1;
401 };
402 }
403
404 #[inline]
407 pub fn raw_ptr(&self) -> *const T {
408 self.buf.raw_ptr()
409 }
410
411 pub fn reserve(&mut self, additional: usize) {
429 self.buf.reserve(self.len, additional);
430 }
431
432 #[inline]
452 pub unsafe fn set_len(&mut self, new_len: usize) {
453 debug_assert!(new_len <= self.capacity());
454
455 self.len = new_len;
456 }
457
458 #[inline]
476 pub unsafe fn set_len_ptr(&mut self, new_len: usize) {
477 debug_assert!(new_len <= self.capacity());
478
479 self.buf.mov_ptr(self.mov_ptr(new_len));
480 self.len = new_len;
481 }
482
483 #[inline]
484 unsafe fn mov_ptr(&self, new_len: usize) -> *mut T {
485 if self.capacity() == new_len {
486 self.buf.raw_ptr()
487 } else if new_len == 0 {
488 self.buf.raw_ptr().add(self.capacity() - 1)
489 } else {
490 self.buf.raw_ptr().add(self.capacity() - new_len)
491 }
492 }
493
494 #[inline]
497 pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
498 unsafe {
499 slice::from_raw_parts_mut(
500 self.buf.raw_ptr() as *mut MaybeUninit<T>,
501 self.buf.capacity() - self.len,
502 )
503 }
504 }
505
506 pub fn truncate(&mut self, len: usize) {
519 unsafe {
520 if len >= self.len {
521 return;
522 }
523
524 let remaining_len = self.len - len;
525 let s = ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), remaining_len);
526 if len != 0 {
527 self.buf.mov_ptr_add(remaining_len);
528 } else {
529 self.buf.mov_ptr_add(remaining_len - 1);
530 }
531
532 self.len = len;
533 ptr::drop_in_place(s);
534 }
535 }
536
537 #[inline]
565 pub fn with_capacity(capacity: usize) -> Self {
566 Cev {
567 buf: RawCev::with_capacity(capacity),
568 len: 0,
569 }
570 }
571
572 #[inline]
574 unsafe fn append_elements(&mut self, other: *const [T]) {
575 let count = (*other).len();
576 self.reserve(count);
577 self.set_len_ptr(self.len() + count);
578 ptr::copy_nonoverlapping(other as *const T, self.as_mut_ptr(), count);
579 }
580}
581
582impl<T> AsRef<Cev<T>> for Cev<T> {
583 fn as_ref(&self) -> &Cev<T> {
584 self
585 }
586}
587
588impl<T> AsMut<Cev<T>> for Cev<T> {
589 fn as_mut(&mut self) -> &mut Cev<T> {
590 self
591 }
592}
593
594impl<T> Borrow<[T]> for Cev<T> {
595 fn borrow(&self) -> &[T] {
596 &self[..]
597 }
598}
599
600impl<T> BorrowMut<[T]> for Cev<T> {
601 fn borrow_mut(&mut self) -> &mut [T] {
602 &mut self[..]
603 }
604}
605
606impl<T> AsRef<[T]> for Cev<T> {
607 fn as_ref(&self) -> &[T] {
608 self
609 }
610}
611
612impl<T> AsMut<[T]> for Cev<T> {
613 fn as_mut(&mut self) -> &mut [T] {
614 self
615 }
616}
617
618impl<T> ops::Deref for Cev<T> {
619 type Target = [T];
620
621 #[inline]
622 fn deref(&self) -> &[T] {
623 unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
624 }
625}
626
627impl<T> ops::DerefMut for Cev<T> {
628 #[inline]
629 fn deref_mut(&mut self) -> &mut [T] {
630 unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
631 }
632}
633
634impl<T> Drop for Cev<T> {
635 fn drop(&mut self) {
636 unsafe { ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len)) }
637 }
638}
639
640impl<T: fmt::Debug> fmt::Debug for Cev<T> {
641 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
642 fmt::Debug::fmt(&**self, f)
643 }
644}
645
646macro_rules! impl_slice_eq {
647 ([$($vars:tt)*] $lhs:ty, $rhs:ty $(where $ty:ty: $bound:ident)?) => {
648 impl<T, U, $($vars)*> PartialEq<$rhs> for $lhs
649 where
650 T: PartialEq<U>,
651 $($ty: $bound)?
652 {
653 #[inline]
654 fn eq(&self, other: &$rhs) -> bool { self[..] == other[..] }
655 #[inline]
656 fn ne(&self, other: &$rhs) -> bool { self[..] != other[..] }
657 }
658 }
659}
660
661impl_slice_eq! { [] Cev<T>, &[U]}
662impl_slice_eq! { [const N: usize] Cev<T>, [U; N] }
663impl_slice_eq! { [const N: usize] Cev<T>, &[U; N] }
664impl_slice_eq! { [] Cev<T>, &mut [U] }
665impl_slice_eq! { [] Cev<T>, [U] }
666impl_slice_eq! { [] [T], Cev<U> }
667impl_slice_eq! { [] &[T], Cev<U> }
668impl_slice_eq! { [] &mut [T], Cev<U> }
669impl_slice_eq! { [] Cev<T>, Cev<U> }
670impl_slice_eq! { [] Cev<T>, Vec<U> }
671impl_slice_eq! { [] Vec<T>, Cev<U> }
672
673impl<T: PartialOrd> PartialOrd for Cev<T> {
674 #[inline]
675 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
676 PartialOrd::partial_cmp(&**self, &**other)
677 }
678}
679
680impl<T: Eq> Eq for Cev<T> {}
681
682impl<T: Ord> Ord for Cev<T> {
683 #[inline]
684 fn cmp(&self, other: &Self) -> Ordering {
685 Ord::cmp(&**self, &**other)
686 }
687}
688
689impl<T> Default for Cev<T> {
690 fn default() -> Cev<T> {
691 Cev::new()
692 }
693}
694
695impl<T> Clone for Cev<T>
696where
697 T: Clone,
698{
699 fn clone(&self) -> Self {
700 to_cev(self)
701 }
702}
703
704pub fn to_cev<T: ConvertCev>(s: &[T]) -> Cev<T> {
705 T::to_cev(s)
706}
707
708impl<T, I: SliceIndex<[T]>> Index<I> for Cev<T> {
709 type Output = I::Output;
710
711 #[inline]
712 fn index(&self, index: I) -> &Self::Output {
713 Index::index(&**self, index)
714 }
715}
716
717impl<T, I: SliceIndex<[T]>> IndexMut<I> for Cev<T> {
718 #[inline]
719 fn index_mut(&mut self, index: I) -> &mut Self::Output {
720 IndexMut::index_mut(&mut **self, index)
721 }
722}
723
724impl<T, const N: usize> TryFrom<Cev<T>> for [T; N] {
725 type Error = Cev<T>;
726
727 fn try_from(mut cev: Cev<T>) -> Result<[T; N], Cev<T>> {
728 if cev.len() != N {
729 return Err(cev);
730 }
731
732 unsafe { cev.set_len(0) };
733 let array = unsafe { ptr::read(cev.as_ptr() as *const [T; N]) };
734 Ok(array)
735 }
736}
737
738impl<T> From<Cev<T>> for Vec<T> {
739 fn from(cev: Cev<T>) -> Vec<T> {
740 cev.into_vec()
741 }
742}
743
744impl<T> From<Vec<T>> for Cev<T> {
745 fn from(vec: Vec<T>) -> Cev<T> {
746 Cev::from_vec(vec)
747 }
748}
749
750impl<T, const N: usize> From<[T; N]> for Cev<T> {
751 fn from(slice: [T; N]) -> Cev<T> {
752 Cev::from(Vec::from(slice))
753 }
754}
755
756impl<T> From<&[T]> for Cev<T>
757where
758 T: Clone,
759{
760 fn from(slice: &[T]) -> Cev<T> {
761 to_cev(slice)
762 }
763}
764
765impl<T> From<&mut [T]> for Cev<T>
766where
767 T: Clone,
768{
769 fn from(slice: &mut [T]) -> Cev<T> {
770 to_cev(slice)
771 }
772}
773
774pub trait ConvertCev {
775 fn to_cev(s: &[Self]) -> Cev<Self>
776 where
777 Self: Sized;
778}
779
780impl<T> FromIterator<T> for Cev<T> {
781 #[inline]
782 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Cev<T> {
783 Self::from_vec(iter.into_iter().collect::<Vec<_>>())
784 }
785}
786
787impl<T> IntoIterator for Cev<T> {
788 type Item = T;
789 type IntoIter = IntoIter<T>;
790
791 #[inline]
792 fn into_iter(self) -> Self::IntoIter {
793 let cev = ManuallyDrop::new(self);
794 let ptr = cev.as_ptr();
795 let len = cev.len();
796
797 IntoIter {
798 buf: unsafe { NonNull::new_unchecked(cev.buf.raw_ptr()) },
799 cap: cev.capacity(),
800 len,
801 ptr,
802 end: unsafe { ptr.add(len) },
803 }
804 }
805}
806
807impl<'a, T> IntoIterator for &'a Cev<T> {
808 type Item = &'a T;
809 type IntoIter = slice::Iter<'a, T>;
810
811 fn into_iter(self) -> Self::IntoIter {
812 self.iter()
813 }
814}
815
816impl<'a, T> IntoIterator for &'a mut Cev<T> {
817 type Item = &'a mut T;
818 type IntoIter = slice::IterMut<'a, T>;
819
820 fn into_iter(self) -> Self::IntoIter {
821 self.iter_mut()
822 }
823}
824
825impl<T: Clone> ConvertCev for T {
826 fn to_cev(s: &[Self]) -> Cev<Self> {
827 struct DropGuard<'a, T> {
828 cev: &'a mut Cev<T>,
829 num_init: usize,
830 }
831
832 impl<'a, T> Drop for DropGuard<'a, T> {
833 fn drop(&mut self) {
834 unsafe {
835 self.cev.set_len(self.num_init);
836 }
837 }
838 }
839
840 let mut cev = Cev::with_capacity(s.len());
841 let mut guard = DropGuard {
842 cev: &mut cev,
843 num_init: 0,
844 };
845
846 let slots = guard.cev.spare_capacity_mut();
847 for (i, b) in s.iter().enumerate().take(slots.len()) {
848 guard.num_init = i;
849 slots[i].write(b.clone());
850 }
851
852 core::mem::forget(guard);
853 unsafe {
854 cev.set_len(s.len());
855 cev.buf.mov_ptr(cev.buf.raw_ptr());
856 }
857 cev
858 }
859}
860
861pub struct IntoIter<T> {
862 buf: NonNull<T>,
863 cap: usize,
864 len: usize,
865 ptr: *const T,
866 end: *const T,
867}
868
869impl<T> IntoIter<T> {
870 pub fn as_slice(&self) -> &[T] {
871 unsafe { slice::from_raw_parts(self.ptr, self.len()) }
872 }
873
874 pub fn as_mut_slice(&mut self) -> &mut [T] {
875 unsafe { &mut *self.as_raw_mut_slice() }
876 }
877
878 fn as_raw_mut_slice(&mut self) -> *mut [T] {
879 ptr::slice_from_raw_parts_mut(self.ptr as *mut T, self.len())
880 }
881}
882
883impl<T> AsRef<[T]> for IntoIter<T> {
884 fn as_ref(&self) -> &[T] {
885 self.as_slice()
886 }
887}
888
889impl<T> Default for IntoIter<T> {
890 fn default() -> Self {
891 Cev::new().into_iter()
892 }
893}
894
895impl<T> Iterator for IntoIter<T> {
896 type Item = T;
897
898 #[inline]
899 fn next(&mut self) -> Option<T> {
900 if self.len == 0 {
901 None
902 } else {
903 unsafe {
904 self.len -= 1;
905 let ptr = self.ptr;
906 self.ptr = self.ptr.add(1);
907 Some(ptr::read(ptr))
908 }
909 }
910 }
911
912 #[inline]
913 fn size_hint(&self) -> (usize, Option<usize>) {
914 (self.len, Some(self.len))
915 }
916}
917
918impl<T> Drop for IntoIter<T> {
919 fn drop(&mut self) {
920 if self.cap != 0 {
921 for _ in &mut *self {}
922 unsafe {
923 let _ = RawCev::from_raw_parts_ptr(self.buf.as_ptr(), self.buf.as_ptr(), self.cap);
924 }
925 }
926 }
927}
928
929impl<T> DoubleEndedIterator for IntoIter<T> {
930 #[inline]
931 fn next_back(&mut self) -> Option<T> {
932 if self.len == 0 {
933 None
934 } else {
935 unsafe {
936 self.len -= 1;
937 self.end = self.end.sub(1);
938 Some(ptr::read(self.end))
939 }
940 }
941 }
942}
943
944impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
945 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
946 f.debug_tuple("IntoIter").field(&self.as_slice()).finish()
947 }
948}
949
950impl<T> ExactSizeIterator for IntoIter<T> {}