1use alloc::borrow::Cow;
2use alloc::boxed::Box;
3use alloc::collections::TryReserveError;
4use alloc::ffi::CString;
5use alloc::string::String;
6use alloc::vec::{self, Drain, Splice, Vec};
7use core::borrow::{Borrow, BorrowMut};
8use core::cmp::Ordering;
9use core::hash::{Hash, Hasher};
10use core::iter::FromIterator;
11use core::marker::PhantomData;
12use core::mem::MaybeUninit;
13use core::ops::{Deref, DerefMut, Index, IndexMut, RangeBounds};
14use core::{fmt, slice};
15#[cfg(feature = "std")]
16use std::io::{IoSlice, Result as IoResult, Write};
17
18#[cfg(all(feature = "alloc", feature = "serde"))]
19use serde::de::{Deserialize, Deserializer};
20#[cfg(feature = "serde")]
21use serde::ser::{Serialize, Serializer};
22
23use crate::{TiEnumerated, TiRangeBounds, TiSlice, TiSliceIndex};
24
25#[repr(transparent)]
91pub struct TiVec<K, V> {
92 pub raw: Vec<V>,
94
95 _marker: PhantomData<fn(K) -> K>,
112}
113
114impl<K, V> TiVec<K, V> {
115 #[inline]
121 #[must_use]
122 pub const fn new() -> Self {
123 Self {
124 raw: Vec::new(),
125 _marker: PhantomData,
126 }
127 }
128
129 #[inline]
135 #[must_use]
136 pub fn with_capacity(capacity: usize) -> Self {
137 Self {
138 raw: Vec::with_capacity(capacity),
139 _marker: PhantomData,
140 }
141 }
142
143 #[inline]
156 pub unsafe fn from_raw_parts(ptr: *mut V, length: usize, capacity: usize) -> Self {
157 Self {
158 raw: Vec::from_raw_parts(ptr, length, capacity),
159 _marker: PhantomData,
160 }
161 }
162
163 #[inline]
178 #[must_use]
179 pub const fn from_ref(raw: &Vec<V>) -> &Self {
180 unsafe { &*core::ptr::from_ref::<Vec<V>>(raw).cast::<Self>() }
182 }
183
184 #[inline]
196 pub fn from_mut(raw: &mut Vec<V>) -> &mut Self {
197 unsafe { &mut *core::ptr::from_mut::<Vec<V>>(raw).cast::<Self>() }
199 }
200
201 #[inline]
208 #[must_use]
209 pub fn capacity(&self) -> usize {
210 self.raw.capacity()
211 }
212
213 #[inline]
223 pub fn reserve(&mut self, additional: usize) {
224 self.raw.reserve(additional);
225 }
226
227 #[inline]
236 pub fn reserve_exact(&mut self, additional: usize) {
237 self.raw.reserve_exact(additional);
238 }
239
240 #[inline]
252 pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
253 self.raw.try_reserve(additional)
254 }
255
256 #[inline]
268 pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
269 self.raw.try_reserve_exact(additional)
270 }
271
272 #[inline]
278 pub fn shrink_to_fit(&mut self) {
279 self.raw.shrink_to_fit();
280 }
281
282 #[inline]
288 pub fn shrink_to(&mut self, min_capacity: usize) {
289 self.raw.shrink_to(min_capacity);
290 }
291 #[inline]
298 #[must_use]
299 pub fn into_boxed_slice(self) -> Box<TiSlice<K, V>> {
300 self.raw.into_boxed_slice().into()
301 }
302
303 #[inline]
310 pub fn truncate(&mut self, len: usize) {
311 self.raw.truncate(len);
312 }
313
314 #[inline]
320 #[must_use]
321 pub fn as_slice(&self) -> &TiSlice<K, V> {
322 self.raw.as_slice().as_ref()
323 }
324
325 #[inline]
331 pub fn as_mut_slice(&mut self) -> &mut TiSlice<K, V> {
332 self.raw.as_mut_slice().as_mut()
333 }
334
335 #[inline]
341 #[must_use]
342 pub fn as_ptr(&self) -> *const V {
343 self.raw.as_ptr()
344 }
345
346 #[inline]
352 pub fn as_mut_ptr(&mut self) -> *mut V {
353 self.raw.as_mut_ptr()
354 }
355
356 #[inline]
368 pub unsafe fn set_len(&mut self, new_len: usize) {
369 self.raw.set_len(new_len);
370 }
371
372 #[inline]
380 pub fn swap_remove(&mut self, index: K) -> V
381 where
382 usize: From<K>,
383 {
384 self.raw.swap_remove(index.into())
385 }
386
387 #[inline]
394 pub fn insert(&mut self, index: K, element: V)
395 where
396 usize: From<K>,
397 {
398 self.raw.insert(index.into(), element);
399 }
400
401 #[inline]
408 pub fn remove(&mut self, index: K) -> V
409 where
410 usize: From<K>,
411 {
412 self.raw.remove(index.into())
413 }
414
415 #[inline]
421 pub fn retain<F>(&mut self, f: F)
422 where
423 F: FnMut(&V) -> bool,
424 {
425 self.raw.retain(f);
426 }
427
428 #[inline]
435 pub fn retain_mut<F>(&mut self, f: F)
436 where
437 F: FnMut(&mut V) -> bool,
438 {
439 self.raw.retain_mut(f);
440 }
441
442 #[inline]
449 pub fn dedup_by_key<F, K2>(&mut self, key: F)
450 where
451 F: FnMut(&mut V) -> K2,
452 K2: PartialEq,
453 {
454 self.raw.dedup_by_key(key);
455 }
456
457 #[inline]
464 pub fn dedup_by<F>(&mut self, same_bucket: F)
465 where
466 F: FnMut(&mut V, &mut V) -> bool,
467 {
468 self.raw.dedup_by(same_bucket);
469 }
470
471 #[inline]
477 pub fn push(&mut self, value: V) {
478 self.raw.push(value);
479 }
480
481 #[inline]
504 pub fn push_and_get_key(&mut self, value: V) -> K
505 where
506 K: From<usize>,
507 {
508 let key = self.next_key();
509 self.raw.push(value);
510 key
511 }
512
513 #[inline]
520 pub fn pop(&mut self) -> Option<V> {
521 self.raw.pop()
522 }
523
524 #[inline]
547 pub fn pop_key_value(&mut self) -> Option<(K, V)>
548 where
549 K: From<usize>,
550 {
551 self.raw.pop().map(|value| (self.raw.len().into(), value))
552 }
553
554 #[inline]
560 pub fn append(&mut self, other: &mut Self) {
561 self.raw.append(&mut other.raw);
562 }
563
564 #[inline]
571 pub fn drain<R>(&mut self, range: R) -> Drain<'_, V>
572 where
573 R: TiRangeBounds<K>,
574 {
575 self.raw.drain(range.into_range())
576 }
577
578 #[inline]
608 pub fn drain_enumerated<R>(&mut self, range: R) -> TiEnumerated<Drain<'_, V>, K, V>
609 where
610 K: From<usize>,
611 R: TiRangeBounds<K>,
612 {
613 self.raw
614 .drain(range.into_range())
615 .enumerate()
616 .map(|(key, value)| (key.into(), value))
617 }
618
619 #[inline]
625 pub fn clear(&mut self) {
626 self.raw.clear();
627 }
628
629 #[inline]
636 #[must_use]
637 pub fn len(&self) -> usize {
638 self.raw.len()
639 }
640
641 #[inline]
647 #[must_use]
648 pub fn is_empty(&self) -> bool {
649 self.raw.is_empty()
650 }
651
652 #[inline]
658 #[must_use = "use `.truncate()` if you don't need the other half"]
659 pub fn split_off(&mut self, at: K) -> Self
660 where
661 usize: From<K>,
662 {
663 self.raw.split_off(at.into()).into()
664 }
665
666 #[inline]
672 pub fn resize_with<F>(&mut self, new_len: usize, f: F)
673 where
674 F: FnMut() -> V,
675 {
676 self.raw.resize_with(new_len, f);
677 }
678
679 #[inline]
685 pub fn resize(&mut self, new_len: usize, value: V)
686 where
687 V: Clone,
688 {
689 self.raw.resize(new_len, value);
690 }
691
692 #[expect(clippy::must_use_candidate, reason = "not used in `Vec::leak`")]
701 #[inline]
702 pub fn leak<'a>(self) -> &'a mut TiSlice<K, V> {
703 self.raw.leak().as_mut()
704 }
705
706 #[inline]
713 pub fn spare_capacity_mut(&mut self) -> &mut TiSlice<K, MaybeUninit<V>> {
714 self.raw.spare_capacity_mut().as_mut()
715 }
716
717 #[inline]
723 pub fn extend_from_slice(&mut self, other: &TiSlice<K, V>)
724 where
725 V: Clone,
726 {
727 self.raw.extend_from_slice(&other.raw);
728 }
729
730 #[inline]
741 pub fn extend_from_within<R>(&mut self, src: R)
742 where
743 V: Clone,
744 R: RangeBounds<usize>,
745 {
746 self.raw.extend_from_within(src);
747 }
748
749 #[inline]
757 pub fn dedup(&mut self)
758 where
759 V: PartialEq,
760 {
761 self.raw.dedup();
762 }
763
764 #[inline]
773 pub fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter>
774 where
775 R: TiRangeBounds<K>,
776 I: IntoIterator<Item = V>,
777 {
778 self.raw.splice(range.into_range(), replace_with)
779 }
780
781 #[inline]
802 pub fn into_iter_enumerated(self) -> TiEnumerated<vec::IntoIter<V>, K, V>
803 where
804 K: From<usize>,
805 {
806 self.raw
807 .into_iter()
808 .enumerate()
809 .map(|(key, value)| (key.into(), value))
810 }
811}
812
813impl<K, V> fmt::Debug for TiVec<K, V>
814where
815 K: fmt::Debug + From<usize>,
816 V: fmt::Debug,
817{
818 #[allow(clippy::allow_attributes, reason = "rust-lang/rust#130021")]
819 #[allow(
820 clippy::missing_inline_in_public_items,
821 reason = "use default inlining behavior"
822 )]
823 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
824 f.debug_map().entries(self.iter_enumerated()).finish()
825 }
826}
827
828impl<K, V> AsRef<Self> for TiVec<K, V> {
829 #[inline]
830 fn as_ref(&self) -> &Self {
831 self
832 }
833}
834
835impl<K, V> AsMut<Self> for TiVec<K, V> {
836 #[inline]
837 fn as_mut(&mut self) -> &mut Self {
838 self
839 }
840}
841
842impl<K, V> AsRef<TiSlice<K, V>> for TiVec<K, V> {
843 #[inline]
844 fn as_ref(&self) -> &TiSlice<K, V> {
845 self
846 }
847}
848
849impl<K, V> AsMut<TiSlice<K, V>> for TiVec<K, V> {
850 #[inline]
851 fn as_mut(&mut self) -> &mut TiSlice<K, V> {
852 self
853 }
854}
855
856impl<K, V> AsRef<Vec<V>> for TiVec<K, V> {
857 #[inline]
858 fn as_ref(&self) -> &Vec<V> {
859 &self.raw
860 }
861}
862
863impl<K, V> AsMut<Vec<V>> for TiVec<K, V> {
864 #[inline]
865 fn as_mut(&mut self) -> &mut Vec<V> {
866 &mut self.raw
867 }
868}
869
870impl<K, V> AsRef<[V]> for TiVec<K, V> {
871 #[inline]
872 fn as_ref(&self) -> &[V] {
873 &self.raw
874 }
875}
876
877impl<K, V> AsMut<[V]> for TiVec<K, V> {
878 #[inline]
879 fn as_mut(&mut self) -> &mut [V] {
880 &mut self.raw
881 }
882}
883
884impl<K, V> AsRef<TiVec<K, V>> for Vec<V> {
885 #[inline]
886 fn as_ref(&self) -> &TiVec<K, V> {
887 TiVec::from_ref(self)
888 }
889}
890
891impl<K, V> AsMut<TiVec<K, V>> for Vec<V> {
892 #[inline]
893 fn as_mut(&mut self) -> &mut TiVec<K, V> {
894 TiVec::from_mut(self)
895 }
896}
897
898impl<K, V> Borrow<TiSlice<K, V>> for TiVec<K, V> {
899 #[inline]
900 fn borrow(&self) -> &TiSlice<K, V> {
901 self.as_slice()
902 }
903}
904
905impl<K, V> BorrowMut<TiSlice<K, V>> for TiVec<K, V> {
906 #[inline]
907 fn borrow_mut(&mut self) -> &mut TiSlice<K, V> {
908 self.as_mut_slice()
909 }
910}
911
912impl<K, V> Deref for TiVec<K, V> {
913 type Target = TiSlice<K, V>;
914
915 #[inline]
916 fn deref(&self) -> &TiSlice<K, V> {
917 Self::Target::from_ref(&self.raw)
918 }
919}
920
921impl<K, V> DerefMut for TiVec<K, V> {
922 #[inline]
923 fn deref_mut(&mut self) -> &mut TiSlice<K, V> {
924 Self::Target::from_mut(&mut self.raw)
925 }
926}
927
928impl<K, V> From<Vec<V>> for TiVec<K, V> {
929 #[inline]
930 fn from(vec: Vec<V>) -> Self {
931 Self {
932 raw: vec,
933 _marker: PhantomData,
934 }
935 }
936}
937
938impl<K, V> From<TiVec<K, V>> for Vec<V> {
939 #[inline]
940 fn from(vec: TiVec<K, V>) -> Self {
941 vec.raw
942 }
943}
944
945impl<K, V> From<&TiSlice<K, V>> for TiVec<K, V>
946where
947 V: Clone,
948{
949 #[inline]
950 fn from(slice: &TiSlice<K, V>) -> Self {
951 slice.to_vec()
952 }
953}
954
955impl<K, V> From<&mut TiSlice<K, V>> for TiVec<K, V>
956where
957 V: Clone,
958{
959 #[inline]
960 fn from(slice: &mut TiSlice<K, V>) -> Self {
961 slice.to_vec()
962 }
963}
964
965impl<K, V> From<Cow<'_, TiSlice<K, V>>> for TiVec<K, V>
966where
967 V: Clone,
968{
969 #[inline]
970 fn from(slice: Cow<'_, TiSlice<K, V>>) -> Self {
971 slice.into_owned()
972 }
973}
974
975impl<K, V> From<TiVec<K, V>> for Cow<'_, TiSlice<K, V>>
976where
977 V: Clone,
978{
979 #[inline]
980 fn from(vec: TiVec<K, V>) -> Self {
981 Cow::Owned(vec)
982 }
983}
984
985impl<K> From<&str> for TiVec<K, u8> {
986 #[inline]
987 fn from(s: &str) -> Self {
988 s.as_bytes().to_vec().into()
989 }
990}
991
992impl<K> From<String> for TiVec<K, u8> {
993 #[inline]
994 fn from(s: String) -> Self {
995 s.into_bytes().into()
996 }
997}
998
999#[cfg(feature = "alloc")]
1000#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
1001impl<K> From<CString> for TiVec<K, u8> {
1002 #[inline]
1003 fn from(s: CString) -> Self {
1004 s.into_bytes().into()
1005 }
1006}
1007
1008impl<K, V> Clone for TiVec<K, V>
1009where
1010 V: Clone,
1011{
1012 #[inline]
1013 fn clone(&self) -> Self {
1014 self.raw.clone().into()
1015 }
1016}
1017
1018impl<K, V> Eq for TiVec<K, V> where V: Eq {}
1019
1020impl<K, A, B> PartialEq<TiVec<K, B>> for TiVec<K, A>
1021where
1022 A: PartialEq<B>,
1023{
1024 #[inline]
1025 fn eq(&self, other: &TiVec<K, B>) -> bool {
1026 self.raw == other.raw
1027 }
1028}
1029
1030impl<K, A, B> PartialEq<TiSlice<K, B>> for TiVec<K, A>
1031where
1032 A: PartialEq<B>,
1033{
1034 #[inline]
1035 fn eq(&self, other: &TiSlice<K, B>) -> bool {
1036 *self.raw == other.raw
1037 }
1038}
1039
1040impl<K, A, B> PartialEq<TiVec<K, B>> for TiSlice<K, A>
1041where
1042 A: PartialEq<B>,
1043{
1044 #[inline]
1045 fn eq(&self, other: &TiVec<K, B>) -> bool {
1046 self.raw == *other.raw
1047 }
1048}
1049
1050impl<'a, K, A, B> PartialEq<&'a TiSlice<K, B>> for TiVec<K, A>
1051where
1052 A: PartialEq<B>,
1053{
1054 #[inline]
1055 fn eq(&self, other: &&'a TiSlice<K, B>) -> bool {
1056 *self.raw == other.raw
1057 }
1058}
1059
1060impl<K, A, B> PartialEq<TiVec<K, B>> for &TiSlice<K, A>
1061where
1062 A: PartialEq<B>,
1063{
1064 #[inline]
1065 fn eq(&self, other: &TiVec<K, B>) -> bool {
1066 self.raw == *other.raw
1067 }
1068}
1069
1070impl<'a, K, A, B> PartialEq<&'a mut TiSlice<K, B>> for TiVec<K, A>
1071where
1072 A: PartialEq<B>,
1073{
1074 #[inline]
1075 fn eq(&self, other: &&'a mut TiSlice<K, B>) -> bool {
1076 *self.raw == other.raw
1077 }
1078}
1079
1080impl<K, A, B> PartialEq<TiVec<K, B>> for &mut TiSlice<K, A>
1081where
1082 A: PartialEq<B>,
1083{
1084 #[inline]
1085 fn eq(&self, other: &TiVec<K, B>) -> bool {
1086 self.raw == *other.raw
1087 }
1088}
1089
1090impl<K, V> Ord for TiVec<K, V>
1091where
1092 V: Ord,
1093{
1094 #[inline]
1095 fn cmp(&self, other: &Self) -> Ordering {
1096 self.raw.cmp(&other.raw)
1097 }
1098}
1099
1100impl<K, V> PartialOrd<Self> for TiVec<K, V>
1101where
1102 V: PartialOrd<V>,
1103{
1104 #[inline]
1105 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1106 self.raw.partial_cmp(&other.raw)
1107 }
1108}
1109
1110impl<K, V> Hash for TiVec<K, V>
1111where
1112 V: Hash,
1113{
1114 #[inline]
1115 fn hash<H: Hasher>(&self, state: &mut H) {
1116 self.raw.hash(state);
1117 }
1118}
1119
1120impl<K, V> Default for TiVec<K, V> {
1121 #[inline]
1122 fn default() -> Self {
1123 Vec::default().into()
1124 }
1125}
1126
1127impl<I, K, V> Index<I> for TiVec<K, V>
1128where
1129 I: TiSliceIndex<K, V>,
1130{
1131 type Output = I::Output;
1132
1133 #[inline]
1134 fn index(&self, index: I) -> &Self::Output {
1135 index.index(self)
1136 }
1137}
1138
1139impl<I, K, V> IndexMut<I> for TiVec<K, V>
1140where
1141 I: TiSliceIndex<K, V>,
1142{
1143 #[inline]
1144 fn index_mut(&mut self, index: I) -> &mut Self::Output {
1145 index.index_mut(self)
1146 }
1147}
1148
1149impl<K, V> Extend<V> for TiVec<K, V> {
1150 #[inline]
1151 fn extend<I: IntoIterator<Item = V>>(&mut self, iter: I) {
1152 self.raw.extend(iter);
1153 }
1154}
1155
1156impl<'a, K, V: 'a + Copy> Extend<&'a V> for TiVec<K, V> {
1157 #[inline]
1158 fn extend<I: IntoIterator<Item = &'a V>>(&mut self, iter: I) {
1159 self.raw.extend(iter);
1160 }
1161}
1162
1163impl<K, V> FromIterator<V> for TiVec<K, V> {
1164 #[inline]
1165 fn from_iter<I: IntoIterator<Item = V>>(iter: I) -> Self {
1166 Self {
1167 raw: Vec::from_iter(iter),
1168 _marker: PhantomData,
1169 }
1170 }
1171}
1172
1173impl<K, V> IntoIterator for TiVec<K, V> {
1174 type Item = V;
1175 type IntoIter = vec::IntoIter<V>;
1176
1177 #[inline]
1178 fn into_iter(self) -> vec::IntoIter<V> {
1179 self.raw.into_iter()
1180 }
1181}
1182
1183impl<'a, K, V> IntoIterator for &'a TiVec<K, V> {
1184 type Item = &'a V;
1185 type IntoIter = slice::Iter<'a, V>;
1186
1187 #[inline]
1188 fn into_iter(self) -> slice::Iter<'a, V> {
1189 self.raw.iter()
1190 }
1191}
1192
1193impl<'a, K, V> IntoIterator for &'a mut TiVec<K, V> {
1194 type Item = &'a mut V;
1195 type IntoIter = slice::IterMut<'a, V>;
1196
1197 #[inline]
1198 fn into_iter(self) -> slice::IterMut<'a, V> {
1199 self.raw.iter_mut()
1200 }
1201}
1202
1203#[cfg(feature = "std")]
1206#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
1207impl<K> Write for TiVec<K, u8> {
1208 #[inline]
1209 fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
1210 self.raw.write(buf)
1211 }
1212
1213 #[inline]
1214 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> IoResult<usize> {
1215 self.raw.write_vectored(bufs)
1216 }
1217
1218 #[inline]
1219 fn write_all(&mut self, buf: &[u8]) -> IoResult<()> {
1220 self.raw.write_all(buf)
1221 }
1222
1223 #[inline]
1224 fn flush(&mut self) -> IoResult<()> {
1225 self.raw.flush()
1226 }
1227}
1228
1229#[cfg(feature = "serde")]
1230#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
1231impl<K, V: Serialize> Serialize for TiVec<K, V> {
1232 #[inline]
1233 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1234 self.raw.as_slice().serialize(serializer)
1235 }
1236}
1237
1238#[cfg(all(feature = "alloc", feature = "serde"))]
1239#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", feature = "serde"))))]
1240impl<'de, K, V: Deserialize<'de>> Deserialize<'de> for TiVec<K, V> {
1241 #[inline]
1242 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1243 Vec::deserialize(deserializer).map(Into::into)
1244 }
1245}
1246
1247#[expect(
1248 dead_code,
1249 unused_imports,
1250 unused_mut,
1251 clippy::into_iter_on_ref,
1252 clippy::op_ref,
1253 clippy::too_many_lines,
1254 clippy::undocumented_unsafe_blocks,
1255 clippy::unwrap_used,
1256 reason = "okay in tests"
1257)]
1258#[cfg(test)]
1259mod test {
1260 use alloc::borrow::{Cow, ToOwned};
1261 use alloc::boxed::Box;
1262 use alloc::ffi::CString;
1263 use alloc::string::ToString;
1264 use alloc::vec::Vec;
1265 use core::borrow::{Borrow, BorrowMut};
1266 use core::hash::{Hash, Hasher};
1267 use core::ops::Bound;
1268 #[cfg(feature = "std")]
1269 use std::hash::DefaultHasher;
1270 #[cfg(feature = "std")]
1271 use std::io::{IoSlice, Write};
1272
1273 use crate::test_util::{AsSliceAndCapacity, Id};
1274 use crate::{TiSlice, TiVec};
1275
1276 #[test]
1277 fn test_vec_read_api_compatibility() {
1278 assert_eq!(
1279 TiVec::<Id, u32>::new().as_slice_and_capacity(),
1280 Vec::<u32>::new().as_slice_and_capacity(),
1281 );
1282 for c in [0, 1, 2, 4] {
1283 assert_eq!(
1284 TiVec::<Id, u32>::with_capacity(c).as_slice_and_capacity(),
1285 Vec::<u32>::with_capacity(c).as_slice_and_capacity(),
1286 );
1287 }
1288
1289 for v in [
1290 &[0_u32; 0][..],
1291 &[1],
1292 &[1, 1234],
1293 &[1, 2, 4],
1294 &[1, 5, 3, 2],
1295 &[1, 1, 9, 2, 4, 1, 12345, 12],
1296 ] {
1297 let cv = (v.to_vec(), TiVec::<Id, _>::from(v.to_vec()));
1298 let mut cv = (&cv.0, &cv.1);
1299
1300 let mut mv = (v.to_vec(), TiVec::<Id, _>::from(v.to_vec()));
1301 let mut mv = (&mut mv.0, &mut mv.1);
1302
1303 assert_eq_api!(cv, v => AsRef::<[_]>::as_ref(v));
1304 assert_eq_api!(mv, v => AsMut::<[_]>::as_mut(v));
1305 assert_eq_api!(cv, v => AsRef::<Vec<_>>::as_ref(v));
1306 assert_eq_api!(mv, v => AsMut::<Vec<_>>::as_mut(v));
1307 assert_eq_api!(cv, v => AsRef::<TiVec<_, _>>::as_ref(v));
1308 assert_eq_api!(mv, v => AsMut::<TiVec<_, _>>::as_mut(v));
1309 assert_eq!(
1310 AsRef::<[_]>::as_ref(cv.0),
1311 AsRef::<[_]>::as_ref(AsRef::<TiSlice<_, _>>::as_ref(cv.1))
1312 );
1313 assert_eq!(
1314 AsMut::<[_]>::as_mut(mv.0),
1315 AsMut::<[_]>::as_mut(AsMut::<TiSlice<_, _>>::as_mut(mv.1))
1316 );
1317 assert_eq!(
1318 Borrow::<[_]>::borrow(cv.0),
1319 AsRef::<[_]>::as_ref(Borrow::<TiSlice<_, _>>::borrow(cv.1))
1320 );
1321 assert_eq!(
1322 BorrowMut::<[_]>::borrow_mut(mv.0),
1323 AsMut::<[_]>::as_mut(BorrowMut::<TiSlice<_, _>>::borrow_mut(mv.1))
1324 );
1325
1326 assert_eq_api!(cv, v => v.len());
1327 assert_eq_api!(cv, v => v.is_empty());
1328 assert_eq_api!(cv, v => v.capacity());
1329 assert_eq_api!(cv, v => v.as_slice().into_std());
1330 assert_eq_api!(mv, v => v.as_mut_slice().into_std());
1331 assert_eq_api!(cv, v => TheVec::from(v.as_slice()).into_std());
1332 assert_eq_api!(mv, v => TheVec::from(v.as_mut_slice()).into_std());
1333 assert_eq_api!(cv, v => TheVec::from(Cow::Borrowed(v.as_slice())).into_std());
1334 assert_eq_api!(mv, v => Cow::from(v.clone()).into_std());
1335
1336 if !v.is_empty() {
1337 assert_ne!(cv.0.as_ptr(), cv.1.as_ptr());
1338 assert_ne!(cv.0.as_ptr_range(), cv.1.as_ptr_range());
1339 assert_ne!(mv.0.as_mut_ptr(), mv.1.as_mut_ptr());
1340 assert_ne!(mv.0.as_mut_ptr_range(), mv.1.as_mut_ptr_range());
1341 }
1342
1343 assert_eq_api!(cv, v => *v == TheVec::<u32>::default());
1344 assert_eq_api!(cv, v => v == v.as_slice());
1345 assert_eq_api!(cv, v => v.as_slice() == v);
1346 assert_eq_api!(cv, v => v == &v.as_slice());
1347 assert_eq_api!(cv, v => &v.as_slice() == v);
1348 assert_eq_api!(mv, v => v == &(&mut [1_u32, 1234][..]).into_tic());
1349 assert_eq_api!(mv, v => &(&mut [1_u32, 1234][..]).into_tic() == v);
1350 assert_eq_api!(cv, v => v.cmp(&alloc::vec![1, 1234].into_tic()));
1351 assert_eq_api!(cv, v => v.partial_cmp(&alloc::vec![1, 1234].into_tic()));
1352
1353 for i in 0..v.len() {
1354 assert_eq_api!(cv, v => v[i.into_tic()]);
1355 assert_eq_api!(mv, v => v[i.into_tic()] = v[i.into_tic()]);
1356 }
1357
1358 unsafe {
1359 assert_eq_api!(cv, v => {
1360 let mut v = core::mem::ManuallyDrop::new(v.clone());
1361 TheVec::from_raw_parts(v.as_mut_ptr(), v.len(), v.capacity()).into_std()
1362 });
1363 }
1364 }
1365 }
1366
1367 #[test]
1368 fn test_vec_write_api_compatibility() {
1369 for v in [
1370 &[0_u32; 0][..],
1371 &[1],
1372 &[1, 1234],
1373 &[1, 2, 4],
1374 &[1, 5, 3, 2],
1375 &[1, 1, 9, 2, 4, 1, 12345, 12],
1376 ] {
1377 let mut mv = (v.to_vec(), TiVec::<Id, _>::from(v.to_vec()));
1378 let mut mv = (&mut mv.0, &mut mv.1);
1379
1380 let restore = |mv: &mut (&mut Vec<u32>, &mut TiVec<Id, u32>)| {
1381 *mv.0 = v.to_vec();
1382 *mv.1 = TiVec::from(v.to_vec());
1383 };
1384
1385 restore(&mut mv);
1386 assert_eq_api!(mv, v => v.try_reserve(usize::MAX));
1387 restore(&mut mv);
1388 assert_eq_api!(mv, v => v.try_reserve_exact(usize::MAX));
1389
1390 for i in 0..8 {
1391 restore(&mut mv);
1392 assert_eq_api!(mv, v => v.resize(i, 123));
1393 restore(&mut mv);
1394 assert_eq_api!(mv, v => { let mut a = 1; v.resize_with(i, || { a *= 2; a }) });
1395 restore(&mut mv);
1396 assert_eq_api!(mv, v => v.reserve(i));
1397 assert_eq_api!(mv, v => v.spare_capacity_mut().len());
1398 restore(&mut mv);
1399 assert_eq_api!(mv, v => v.try_reserve(i));
1400 restore(&mut mv);
1401 assert_eq_api!(mv, v => v.reserve_exact(i));
1402 restore(&mut mv);
1403 assert_eq_api!(mv, v => v.try_reserve_exact(i));
1404 restore(&mut mv);
1405 assert_eq_api!(mv, v => v.reserve_exact(i));
1406 assert_eq_api!(mv, v => v.shrink_to_fit());
1407 restore(&mut mv);
1408 assert_eq_api!(mv, v => v.reserve_exact(i * 2));
1409 assert_eq_api!(mv, v => v.shrink_to(i));
1410 restore(&mut mv);
1411 assert_eq_api!(mv, v => v.truncate(i));
1412 }
1413
1414 let l1: Vec<_> = mv.0.clone();
1415 let l1c = l1.capacity();
1416 let l1 = l1.leak();
1417 let l2: TiVec<_, _> = mv.1.clone();
1418 let l2c = l2.capacity();
1419 let l2 = l2.leak();
1420 assert_eq!(l1, &l2.raw);
1421 drop(unsafe { Vec::from_raw_parts(l1.as_mut_ptr(), l1.len(), l1c) });
1422 drop(unsafe { TiVec::<Id, _>::from_raw_parts(l2.as_mut_ptr(), l2.len(), l2c) });
1423
1424 restore(&mut mv);
1425 assert_eq_api!(mv, v => (&*v).into_iter().copied().collect::<Vec<_>>());
1426 assert_eq_api!(mv, v => v.iter_mut().collect::<Vec<_>>());
1427 assert_eq_api!(mv, v => v.clone().into_iter().collect::<Vec<_>>());
1428
1429 restore(&mut mv);
1430 assert_eq_api!(mv, v => v.pop());
1431 assert_eq_api!(mv, v => v.push(123));
1432 assert_eq_api!(mv, v => v.pop());
1433 assert_eq_api!(mv, v => v.append(&mut v.clone()));
1434 restore(&mut mv);
1435 assert_eq_api!(mv, v => v.extend(v.clone().as_slice()));
1436 restore(&mut mv);
1437 assert_eq_api!(mv, v => v.extend(v.clone().iter().copied()));
1438 restore(&mut mv);
1439 assert_eq_api!(mv, v => v.extend_from_slice(&v.clone()));
1440 restore(&mut mv);
1441 assert_eq_api!(mv, v => v.into_iter().collect::<TheVec<_>>().into_std());
1442
1443 restore(&mut mv);
1444 assert_eq_api!(mv, v => v.retain(|value| value % 3 == 0 || value % 4 == 0));
1445
1446 restore(&mut mv);
1447 assert_eq_api!(mv, v => v.retain_mut(|value| {
1448 *value += 1;
1449 *value % 3 == 0 || *value % 4 == 0
1450 }));
1451
1452 restore(&mut mv);
1453 assert_eq_api!(mv, v => v.dedup());
1454
1455 restore(&mut mv);
1456 assert_eq_api!(mv, v => v.dedup_by(|lhs, rhs| lhs < rhs));
1457
1458 restore(&mut mv);
1459 assert_eq_api!(mv, v => v.dedup_by_key(|value| *value % 3));
1460
1461 for i in 0..v.len() {
1462 restore(&mut mv);
1463 assert_eq_api!(mv, v => v.swap_remove(i.into_tic()));
1464 restore(&mut mv);
1465 assert_eq_api!(mv, v => v.insert(i.into_tic(), 123));
1466 restore(&mut mv);
1467 assert_eq_api!(mv, v => v.remove(i.into_tic()));
1468 restore(&mut mv);
1469 unsafe { assert_eq_api!(mv, v => v.set_len(i)) };
1470 restore(&mut mv);
1471 assert_eq_api!(mv, v => v.split_off(i.into_tic()).into_std());
1472 }
1473
1474 for a in 0..v.len() {
1475 for b in a..v.len() {
1476 restore(&mut mv);
1477 assert_eq_api!(mv, v => v.drain((a..b).into_tic()).collect::<Vec<_>>());
1478 restore(&mut mv);
1479 assert_eq_api!(mv, v => v.extend_from_within(a..b));
1480 restore(&mut mv);
1481 assert_eq_api!(
1482 mv, v => v.splice((a..b).into_tic(), [1, 2, 3]).collect::<Vec<_>>()
1483 );
1484 }
1485 }
1486 restore(&mut mv);
1487 assert_eq_api!(mv, v => v.splice(.., [1, 2, 3]).collect::<Vec<_>>());
1488
1489 restore(&mut mv);
1490 assert_eq_api!(mv, v => v.clear());
1491 }
1492 }
1493
1494 #[cfg(feature = "std")]
1495 #[test]
1496 fn test_vec_hash_compatibility() {
1497 for v in [
1498 &[0_u32; 0][..],
1499 &[1],
1500 &[1, 1234],
1501 &[1, 2, 4],
1502 &[1, 5, 3, 2],
1503 &[1, 1, 9, 2, 4, 1, 12345, 12],
1504 ] {
1505 let cv = (v.to_vec(), TiVec::<Id, _>::from(v.to_vec()));
1506 let mut cv = (&cv.0, &cv.1);
1507 assert_eq_api!(cv, v => {
1508 let mut hasher = DefaultHasher::new();
1509 v.hash(&mut hasher);
1510 hasher.finish()
1511 });
1512 }
1513 }
1514
1515 #[test]
1516 fn test_u8_vec_api_compatibility() {
1517 assert_eq!(
1518 Vec::from(TiVec::<Id, u8>::from("abc")),
1519 Vec::<u8>::from("abc"),
1520 );
1521 assert_eq!(
1522 Vec::from(TiVec::<Id, u8>::from("abc".to_owned())),
1523 Vec::<u8>::from("abc".to_owned()),
1524 );
1525 assert_eq!(
1526 Vec::from(TiVec::<Id, u8>::from(CString::new("abc").unwrap())),
1527 Vec::<u8>::from(CString::new("abc").unwrap()),
1528 );
1529
1530 for v in [&b"abc"[..], b"aBc", b"ABC", b"abd", b"a\x80\x81b"] {
1531 let cv = (v.to_vec(), TiVec::<Id, _>::from(v.to_vec()));
1532 let mut cv = (&cv.0, &cv.1);
1533
1534 assert_eq_api!(cv, v => TheVec::from(v.as_slice()).into_std());
1535 }
1536 }
1537
1538 #[test]
1539 fn test_vec_debug() {
1540 let s0: TiVec<Id, u32> = TiVec::from(alloc::vec![]);
1541 let s1: TiVec<Id, u32> = TiVec::from(alloc::vec![12]);
1542 let s2: TiVec<Id, u32> = TiVec::from(alloc::vec![23, 34]);
1543 assert_eq!(&alloc::format!("{s0:?}"), "{}");
1544 assert_eq!(&alloc::format!("{s1:?}"), "{Id(0): 12}");
1545 assert_eq!(&alloc::format!("{s2:?}"), "{Id(0): 23, Id(1): 34}");
1546 }
1547
1548 #[cfg(feature = "std")]
1549 #[test]
1550 fn test_vec_write() {
1551 let mut mv = (Vec::<u8>::new(), TiVec::<Id, u8>::new());
1552 let mut mv = (&mut mv.0, &mut mv.1);
1553
1554 assert_eq_api!(mv, v => v.write(&[1, 2, 3]).unwrap());
1555 assert_eq_api!(mv, v => v.write_vectored(
1556 &[IoSlice::new(&[1, 2, 3]), IoSlice::new(&[4, 5])]
1557 ).unwrap());
1558 assert_eq_api!(mv, v => v.write_all(&[1, 2, 3]).unwrap());
1559 assert_eq_api!(mv, v => v.flush().unwrap());
1560 }
1561
1562 #[cfg(feature = "serde")]
1563 #[test]
1564 fn test_vec_serialize() {
1565 let s0: TiVec<Id, u32> = TiVec::from(alloc::vec![]);
1566 let s1: TiVec<Id, u32> = TiVec::from(alloc::vec![12]);
1567 let s2: TiVec<Id, u32> = TiVec::from(alloc::vec![23, 34]);
1568 assert_eq!(&serde_json::to_string(&s0).unwrap(), "[]");
1569 assert_eq!(&serde_json::to_string(&s1).unwrap(), "[12]");
1570 assert_eq!(&serde_json::to_string(&s2).unwrap(), "[23,34]");
1571 }
1572
1573 #[cfg(feature = "serde")]
1574 #[test]
1575 fn test_vec_deserialize() {
1576 let s0: TiVec<Id, u32> = serde_json::from_str("[]").unwrap();
1577 let s1: TiVec<Id, u32> = serde_json::from_str("[12]").unwrap();
1578 let s2: TiVec<Id, u32> = serde_json::from_str("[23, 34]").unwrap();
1579 assert_eq!(s0.as_slice().raw, [0; 0][..]);
1580 assert_eq!(s1.as_slice().raw, [12][..]);
1581 assert_eq!(s2.as_slice().raw, [23, 34][..]);
1582 }
1583}