1#![warn(missing_docs)]
27#![forbid(unsafe_code)]
28
29#[cfg(feature = "serde")]
30use serde::de::{Deserialize, Deserializer, SeqAccess, Visitor};
31#[cfg(feature = "serde")]
32use serde::ser::{Serialize, SerializeSeq, Serializer};
33
34#[derive(Clone, Debug)]
36pub enum SingleVec<T> {
37 One(Option<T>),
39 Many(Vec<T>),
41}
42
43impl<T> SingleVec<T> {
44 #[inline]
46 pub fn new() -> Self {
47 Self::One(None)
48 }
49
50 #[inline]
62 pub fn push(&mut self, i: T) {
63 #[cold]
68 fn move_to_vec<T>(o: &mut Option<T>, i: T) -> Vec<T> {
69 let mut v = Vec::with_capacity(2);
70 v.extend(o.take());
71 v.push(i);
72 v
73 }
74
75 match self {
76 Self::One(o @ None) => {
77 *o = Some(i);
78 }
79 Self::One(o @ Some(_)) => {
80 *self = Self::Many(move_to_vec(o, i));
81 }
82 Self::Many(v) => {
83 v.push(i);
84 }
85 }
86 }
87
88 #[inline]
107 pub fn pop(&mut self) -> Option<T> {
108 match self {
109 Self::One(o) => o.take(),
110 Self::Many(v) => v.pop(),
111 }
112 }
113
114 pub fn insert(&mut self, index: usize, e: T) {
119 match self {
120 Self::One(None) if index == 0 => {
121 *self = Self::One(Some(e));
122 }
123 Self::One(None) => panic!("insertion index (is {index}) should be <= len (is 0)"),
124 Self::One(Some(_)) => match index {
125 0 => {
126 let Some(o) = self.pop() else {
127 unreachable!();
128 };
129 let mut v = Vec::new();
130 v.push(e);
131 v.push(o);
132 *self = Self::Many(v);
133 }
134 1 => {
135 let Some(o) = self.pop() else {
136 unreachable!();
137 };
138 let mut v = Vec::new();
139 v.push(o);
140 v.push(e);
141 *self = Self::Many(v);
142 }
143 _ => panic!("insertion index (is {index}) should be <= len (is 1)"),
144 },
145 Self::Many(v) => v.insert(index, e),
146 }
147 }
148
149 pub fn remove(&mut self, index: usize) -> T {
156 match self {
157 Self::One(None) => panic!("removal index (is {index}) should be < len (is 0)"),
158 Self::One(Some(_)) if index == 0 => {
159 let Some(o) = self.pop() else {
160 unreachable!();
161 };
162 *self = Self::One(None);
163 o
164 }
165 Self::One(Some(_)) => {
166 panic!("removal index (is {index}) should be < len (is 1)");
167 }
168 Self::Many(v) => v.remove(index),
169 }
170 }
171
172 #[inline]
174 pub fn as_slice(&self) -> &[T] {
175 match self {
176 Self::One(o) => o.as_slice(),
177 Self::Many(v) => v.as_slice(),
178 }
179 }
180
181 #[inline]
183 pub fn as_mut_slice(&mut self) -> &mut [T] {
184 match self {
185 Self::One(o) => o.as_mut_slice(),
186 Self::Many(v) => v.as_mut_slice(),
187 }
188 }
189
190 #[inline]
192 pub fn as_ref(&self) -> SingleVec<&T> {
193 match self {
194 Self::One(o) => SingleVec::One(o.as_ref()),
195 Self::Many(v) => SingleVec::Many(v.iter().collect()),
196 }
197 }
198
199 #[inline]
201 pub fn as_mut(&mut self) -> SingleVec<&mut T> {
202 match self {
203 Self::One(o) => SingleVec::One(o.as_mut()),
204 Self::Many(v) => SingleVec::Many(v.iter_mut().collect()),
205 }
206 }
207
208 #[inline]
218 pub fn clear(&mut self) {
219 *self = Self::default();
220 }
221
222 #[inline]
239 pub fn filter(self, f: impl FnMut(&T) -> bool) -> Self {
240 match self {
241 Self::One(o) => Self::One(o.filter(f)),
242 Self::Many(v) => v.into_iter().filter(f).collect(),
243 }
244 }
245
246 #[inline]
262 pub fn map<U>(self, f: impl FnMut(T) -> U) -> SingleVec<U> {
263 match self {
264 Self::One(o) => SingleVec::One(o.map(f)),
265 Self::Many(v) => SingleVec::Many(v.into_iter().map(f).collect()),
266 }
267 }
268
269 #[inline]
287 pub fn filter_map<U>(self, mut f: impl FnMut(T) -> Option<U>) -> SingleVec<U> {
288 match self {
289 Self::One(None) => SingleVec::One(None),
290 Self::One(Some(t)) => SingleVec::One(f(t)),
291 Self::Many(v) => v.into_iter().filter_map(f).collect(),
292 }
293 }
294
295 #[inline]
326 pub fn reduce(self, f: impl FnMut(T, T) -> T) -> Option<T> {
327 match self {
328 Self::One(o) => o,
329 Self::Many(v) => v.into_iter().reduce(f),
330 }
331 }
332
333 #[inline]
351 pub fn fold<B>(self, init: B, mut f: impl FnMut(B, T) -> B) -> B {
352 match self {
353 Self::One(None) => init,
354 Self::One(Some(o)) => f(init, o),
355 Self::Many(v) => v.into_iter().fold(init, f),
356 }
357 }
358
359 #[inline]
384 pub fn zip<U>(self, other: SingleVec<U>) -> SingleVec<(T, U)> {
385 match (self, other) {
386 (Self::One(x), SingleVec::One(y)) => SingleVec::One(x.zip(y)),
387 (i, j) => i.into_iter().zip(j).collect(),
388 }
389 }
390
391 #[inline]
413 pub fn retain(&mut self, mut f: impl FnMut(&T) -> bool) {
414 match self {
415 Self::One(o) => {
416 if o.as_ref().is_some_and(|x| !f(x)) {
417 *o = None;
418 }
419 }
420 Self::Many(v) => v.retain(f),
421 }
422 }
423
424 #[inline]
456 pub fn retain_mut(&mut self, mut f: impl FnMut(&mut T) -> bool) {
457 match self {
458 Self::One(o) => {
459 if o.as_mut().is_some_and(|x| !f(x)) {
460 *o = None;
461 }
462 }
463 Self::Many(v) => v.retain_mut(f),
464 }
465 }
466}
467
468impl<T> SingleVec<Option<T>> {
469 #[inline]
484 pub fn flatten(self) -> SingleVec<T> {
485 match self {
486 Self::One(None) => SingleVec::One(None),
487 Self::One(Some(inner)) => SingleVec::One(inner),
488 Self::Many(v) => v.into_iter().flatten().collect(),
489 }
490 }
491
492 #[inline]
510 pub fn transpose(self) -> Option<SingleVec<T>> {
511 match self {
512 Self::One(None) => None,
513 Self::One(Some(inner)) => Some(SingleVec::One(inner)),
514 Self::Many(v) => v.into_iter().collect::<Option<_>>().map(SingleVec::Many),
515 }
516 }
517}
518
519impl<T> SingleVec<SingleVec<T>> {
520 #[inline]
535 pub fn flatten(self) -> SingleVec<T> {
536 match self {
537 Self::One(None) => SingleVec::One(None),
538 Self::One(Some(inner)) => inner,
539 Self::Many(v) => v.into_iter().flatten().collect(),
540 }
541 }
542}
543
544impl<T> SingleVec<Vec<T>> {
545 #[inline]
566 pub fn flatten(self) -> SingleVec<T> {
567 match self {
568 Self::One(None) => SingleVec::One(None),
569 Self::One(Some(inner)) => SingleVec::Many(inner),
570 Self::Many(v) => v.into_iter().flatten().collect(),
571 }
572 }
573}
574
575impl<T, U> SingleVec<(T, U)> {
576 #[inline]
591 pub fn unzip(self) -> (SingleVec<T>, SingleVec<U>) {
592 match self {
593 Self::One(None) => (SingleVec::One(None), SingleVec::One(None)),
594 Self::One(Some((t, u))) => (SingleVec::One(Some(t)), SingleVec::One(Some(u))),
595 Self::Many(v) => v.into_iter().unzip(),
596 }
597 }
598}
599
600impl<T, E> SingleVec<Result<T, E>> {
601 #[inline]
627 pub fn transpose(self) -> Result<SingleVec<T>, E> {
628 match self {
629 Self::One(o) => o.transpose().map(SingleVec::One),
630 Self::Many(v) => v.into_iter().collect::<Result<_, _>>().map(SingleVec::Many),
631 }
632 }
633}
634
635impl<T> Default for SingleVec<T> {
636 #[inline]
637 fn default() -> Self {
638 Self::new()
639 }
640}
641
642impl<T: core::cmp::Eq> core::cmp::Eq for SingleVec<T> {}
648
649impl<T: core::cmp::PartialEq> core::cmp::PartialEq for SingleVec<T> {
650 #[inline]
651 fn eq(&self, other: &SingleVec<T>) -> bool {
652 self.as_slice().eq(other.as_slice())
653 }
654}
655
656impl<T: core::cmp::Ord> core::cmp::Ord for SingleVec<T> {
657 #[inline]
658 fn cmp(&self, other: &SingleVec<T>) -> core::cmp::Ordering {
659 self.as_slice().cmp(other.as_slice())
660 }
661}
662
663impl<T: core::cmp::PartialOrd> core::cmp::PartialOrd for SingleVec<T> {
664 #[inline]
665 fn partial_cmp(&self, other: &SingleVec<T>) -> Option<core::cmp::Ordering> {
666 self.as_slice().partial_cmp(other.as_slice())
667 }
668}
669
670impl<T: core::hash::Hash> core::hash::Hash for SingleVec<T> {
671 #[inline]
672 fn hash<H: core::hash::Hasher>(&self, h: &mut H) {
673 self.as_slice().hash(h)
674 }
675}
676
677impl<T> core::ops::Deref for SingleVec<T> {
678 type Target = [T];
679
680 #[inline]
681 fn deref(&self) -> &[T] {
682 self.as_slice()
683 }
684}
685
686impl<T> core::ops::DerefMut for SingleVec<T> {
687 #[inline]
688 fn deref_mut(&mut self) -> &mut [T] {
689 self.as_mut_slice()
690 }
691}
692
693impl<T> Extend<T> for SingleVec<T> {
694 #[inline]
695 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
696 for v in iter {
697 self.push(v);
698 }
699 }
700}
701
702impl<T> FromIterator<T> for SingleVec<T> {
703 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
704 let mut s = Self::default();
705 s.extend(iter);
706 s
707 }
708}
709
710impl<T> IntoIterator for SingleVec<T> {
711 type Item = T;
712
713 type IntoIter =
714 EitherIterator<<Option<T> as IntoIterator>::IntoIter, <Vec<T> as IntoIterator>::IntoIter>;
715
716 #[inline]
717 fn into_iter(self) -> Self::IntoIter {
718 match self {
719 Self::One(o) => EitherIterator::I(o.into_iter()),
720 Self::Many(v) => EitherIterator::J(v.into_iter()),
721 }
722 }
723}
724
725impl<'t, T> IntoIterator for &'t SingleVec<T> {
726 type Item = &'t T;
727
728 type IntoIter = <&'t [T] as IntoIterator>::IntoIter;
729
730 #[inline]
731 fn into_iter(self) -> Self::IntoIter {
732 self.as_slice().iter()
733 }
734}
735
736impl<T> From<Option<T>> for SingleVec<T> {
737 #[inline]
738 fn from(o: Option<T>) -> Self {
739 Self::One(o)
740 }
741}
742
743impl<T> From<Vec<T>> for SingleVec<T> {
744 #[inline]
745 fn from(mut v: Vec<T>) -> Self {
746 match v.len() {
747 0 | 1 => Self::One(v.pop()),
748 _ => Self::Many(v),
749 }
750 }
751}
752
753impl<T> From<SingleVec<T>> for Vec<T> {
754 #[inline]
755 fn from(v: SingleVec<T>) -> Self {
756 match v {
757 SingleVec::One(None) => Vec::new(),
758 SingleVec::One(Some(x)) => vec![x],
759 SingleVec::Many(v) => v,
760 }
761 }
762}
763
764impl<T> TryFrom<SingleVec<T>> for Option<T> {
765 type Error = Vec<T>;
766
767 #[inline]
768 fn try_from(v: SingleVec<T>) -> Result<Self, Self::Error> {
769 match v {
770 SingleVec::One(o) => Ok(o),
771 SingleVec::Many(mut v) if v.len() < 2 => Ok(v.pop()),
772 SingleVec::Many(v) => Err(v),
773 }
774 }
775}
776
777impl<T, const N: usize> From<[T; N]> for SingleVec<T> {
778 #[inline]
779 fn from(v: [T; N]) -> Self {
780 v.into_iter().collect()
781 }
782}
783
784#[cfg(feature = "serde")]
785impl<T: Serialize> Serialize for SingleVec<T> {
786 #[must_use]
787 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
788 where
789 S: Serializer,
790 {
791 let mut seq = serializer.serialize_seq(Some(self.len()))?;
792 for element in self.iter() {
793 seq.serialize_element(element)?;
794 }
795 seq.end()
796 }
797}
798
799#[cfg(feature = "serde")]
800impl<'de, T: Deserialize<'de>> Deserialize<'de> for SingleVec<T> {
801 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
802 where
803 D: Deserializer<'de>,
804 {
805 deserializer.deserialize_seq(SingleVecVisitor(core::marker::PhantomData))
806 }
807}
808
809#[cfg(feature = "serde")]
810struct SingleVecVisitor<T>(core::marker::PhantomData<T>);
811
812#[cfg(feature = "serde")]
813impl<'de, T: Deserialize<'de>> Visitor<'de> for SingleVecVisitor<T> {
814 type Value = SingleVec<T>;
815
816 fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
817 formatter.write_str("a sequence")
818 }
819
820 fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error>
821 where
822 S: SeqAccess<'de>,
823 {
824 let mut v = SingleVec::default();
825
826 while let Some(value) = seq.next_element()? {
827 v.push(value);
828 }
829
830 Ok(v)
831 }
832}
833
834#[cfg(test)]
835mod tests {
836 use super::SingleVec;
837
838 #[test]
839 fn iter_test() {
840 let v = SingleVec::from([1, 2, 3]);
841 assert_eq!(v.iter().copied().sum::<i32>(), 6);
842
843 let v = SingleVec::from([1, 2, 3]);
844 assert_eq!((&v).into_iter().copied().sum::<i32>(), 6);
845 }
846
847 #[test]
848 fn index_test() {
849 let mut v = SingleVec::from([1, 2, 3]);
850 assert_eq!(v[0], 1);
851 assert_eq!(v[1], 2);
852 assert_eq!(v[2], 3);
853
854 v[0] = 3;
855 v[1] = 2;
856 v[2] = 1;
857
858 assert_eq!(v[0], 3);
859 assert_eq!(v[1], 2);
860 assert_eq!(v[2], 1);
861 }
862
863 #[test]
864 fn eq_test() {
865 let v1: SingleVec<i32> = SingleVec::One(None);
866 let v2 = SingleVec::Many(vec![]);
867 assert_eq!(v1, v2);
868
869 let v1: SingleVec<i32> = SingleVec::One(Some(1));
870 let v2 = SingleVec::Many(vec![1]);
871 assert_eq!(v1, v2);
872 }
873
874 #[test]
875 fn ord_test() {
876 let v1: SingleVec<i32> = SingleVec::One(Some(1));
877 let v2 = SingleVec::Many(vec![2]);
878 assert!(v1 < v2);
879 }
880
881 #[test]
882 fn hash_test() {
883 let mut h = std::collections::HashSet::new();
884 h.insert(SingleVec::One(Some(1)));
885 assert!(h.contains(&SingleVec::Many(vec![1])));
886 }
887
888 #[test]
889 fn insert_test() {
890 let mut v = SingleVec::new();
891 v.insert(0, 2);
892 v.insert(0, 1);
893 v.insert(2, 3);
894
895 assert_eq!(v[0], 1);
896 assert_eq!(v[1], 2);
897 assert_eq!(v[2], 3);
898 }
899
900 #[test]
901 fn remove_test() {
902 let mut v = SingleVec::from([1, 2, 3]);
903 assert_eq!(v.remove(0), 1);
904 assert_eq!(v.remove(0), 2);
905 assert_eq!(v.remove(0), 3);
906 }
907}
908
909#[derive(Clone, Debug)]
916pub enum EitherIterator<I, J> {
917 I(I),
919 J(J),
921}
922
923impl<I: Iterator, J: Iterator<Item = I::Item>> Iterator for EitherIterator<I, J> {
924 type Item = I::Item;
925
926 #[inline]
927 fn next(&mut self) -> Option<Self::Item> {
928 match self {
929 Self::I(i) => i.next(),
930 Self::J(j) => j.next(),
931 }
932 }
933
934 #[inline]
935 fn size_hint(&self) -> (usize, Option<usize>) {
936 match self {
937 Self::I(i) => i.size_hint(),
938 Self::J(j) => j.size_hint(),
939 }
940 }
941
942 #[inline]
943 fn count(self) -> usize
944 where
945 Self: Sized,
946 {
947 match self {
948 Self::I(i) => i.count(),
949 Self::J(j) => j.count(),
950 }
951 }
952
953 #[inline]
954 fn last(self) -> Option<Self::Item>
955 where
956 Self: Sized,
957 {
958 match self {
959 Self::I(i) => i.last(),
960 Self::J(j) => j.last(),
961 }
962 }
963
964 #[inline]
965 fn for_each<F>(self, f: F)
966 where
967 Self: Sized,
968 F: FnMut(Self::Item),
969 {
970 match self {
971 Self::I(i) => i.for_each(f),
972 Self::J(j) => j.for_each(f),
973 }
974 }
975
976 #[inline]
977 fn collect<B>(self) -> B
978 where
979 B: FromIterator<Self::Item>,
980 Self: Sized,
981 {
982 match self {
983 Self::I(i) => i.collect(),
984 Self::J(j) => j.collect(),
985 }
986 }
987
988 #[inline]
989 fn partition<B, F>(self, f: F) -> (B, B)
990 where
991 Self: Sized,
992 B: Default + Extend<Self::Item>,
993 F: FnMut(&Self::Item) -> bool,
994 {
995 match self {
996 Self::I(i) => i.partition(f),
997 Self::J(j) => j.partition(f),
998 }
999 }
1000
1001 #[inline]
1002 fn fold<B, F>(self, init: B, f: F) -> B
1003 where
1004 Self: Sized,
1005 F: FnMut(B, Self::Item) -> B,
1006 {
1007 match self {
1008 Self::I(i) => i.fold(init, f),
1009 Self::J(j) => j.fold(init, f),
1010 }
1011 }
1012
1013 #[inline]
1014 fn reduce<F>(self, f: F) -> Option<Self::Item>
1015 where
1016 Self: Sized,
1017 F: FnMut(Self::Item, Self::Item) -> Self::Item,
1018 {
1019 match self {
1020 Self::I(i) => i.reduce(f),
1021 Self::J(j) => j.reduce(f),
1022 }
1023 }
1024
1025 #[inline]
1026 fn all<F>(&mut self, f: F) -> bool
1027 where
1028 Self: Sized,
1029 F: FnMut(Self::Item) -> bool,
1030 {
1031 match self {
1032 Self::I(i) => i.all(f),
1033 Self::J(j) => j.all(f),
1034 }
1035 }
1036
1037 #[inline]
1038 fn any<F>(&mut self, f: F) -> bool
1039 where
1040 Self: Sized,
1041 F: FnMut(Self::Item) -> bool,
1042 {
1043 match self {
1044 Self::I(i) => i.any(f),
1045 Self::J(j) => j.any(f),
1046 }
1047 }
1048
1049 #[inline]
1050 fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
1051 where
1052 Self: Sized,
1053 P: FnMut(&Self::Item) -> bool,
1054 {
1055 match self {
1056 Self::I(i) => i.find(predicate),
1057 Self::J(j) => j.find(predicate),
1058 }
1059 }
1060
1061 #[inline]
1062 fn find_map<B, F>(&mut self, f: F) -> Option<B>
1063 where
1064 Self: Sized,
1065 F: FnMut(Self::Item) -> Option<B>,
1066 {
1067 match self {
1068 Self::I(i) => i.find_map(f),
1069 Self::J(j) => j.find_map(f),
1070 }
1071 }
1072
1073 #[inline]
1074 fn position<P>(&mut self, predicate: P) -> Option<usize>
1075 where
1076 Self: Sized,
1077 P: FnMut(Self::Item) -> bool,
1078 {
1079 match self {
1080 Self::I(i) => i.position(predicate),
1081 Self::J(j) => j.position(predicate),
1082 }
1083 }
1084
1085 #[inline]
1086 fn max(self) -> Option<Self::Item>
1087 where
1088 Self: Sized,
1089 Self::Item: Ord,
1090 {
1091 match self {
1092 Self::I(i) => i.max(),
1093 Self::J(j) => j.max(),
1094 }
1095 }
1096
1097 #[inline]
1098 fn min(self) -> Option<Self::Item>
1099 where
1100 Self: Sized,
1101 Self::Item: Ord,
1102 {
1103 match self {
1104 Self::I(i) => i.min(),
1105 Self::J(j) => j.min(),
1106 }
1107 }
1108
1109 #[inline]
1110 fn max_by_key<B, F>(self, f: F) -> Option<Self::Item>
1111 where
1112 B: Ord,
1113 Self: Sized,
1114 F: FnMut(&Self::Item) -> B,
1115 {
1116 match self {
1117 Self::I(i) => i.max_by_key(f),
1118 Self::J(j) => j.max_by_key(f),
1119 }
1120 }
1121
1122 #[inline]
1123 fn max_by<F>(self, compare: F) -> Option<Self::Item>
1124 where
1125 Self: Sized,
1126 F: FnMut(&Self::Item, &Self::Item) -> core::cmp::Ordering,
1127 {
1128 match self {
1129 Self::I(i) => i.max_by(compare),
1130 Self::J(j) => j.max_by(compare),
1131 }
1132 }
1133
1134 #[inline]
1135 fn min_by_key<B, F>(self, f: F) -> Option<Self::Item>
1136 where
1137 B: Ord,
1138 Self: Sized,
1139 F: FnMut(&Self::Item) -> B,
1140 {
1141 match self {
1142 Self::I(i) => i.min_by_key(f),
1143 Self::J(j) => j.min_by_key(f),
1144 }
1145 }
1146
1147 #[inline]
1148 fn min_by<F>(self, compare: F) -> Option<Self::Item>
1149 where
1150 Self: Sized,
1151 F: FnMut(&Self::Item, &Self::Item) -> core::cmp::Ordering,
1152 {
1153 match self {
1154 Self::I(i) => i.min_by(compare),
1155 Self::J(j) => j.min_by(compare),
1156 }
1157 }
1158
1159 #[inline]
1160 fn sum<S>(self) -> S
1161 where
1162 Self: Sized,
1163 S: core::iter::Sum<Self::Item>,
1164 {
1165 match self {
1166 Self::I(i) => i.sum(),
1167 Self::J(j) => j.sum(),
1168 }
1169 }
1170
1171 #[inline]
1172 fn product<P>(self) -> P
1173 where
1174 Self: Sized,
1175 P: core::iter::Product<Self::Item>,
1176 {
1177 match self {
1178 Self::I(i) => i.product(),
1179 Self::J(j) => j.product(),
1180 }
1181 }
1182
1183 #[inline]
1184 fn cmp<K>(self, other: K) -> core::cmp::Ordering
1185 where
1186 K: IntoIterator<Item = Self::Item>,
1187 Self::Item: core::cmp::Ord,
1188 Self: Sized,
1189 {
1190 match self {
1191 Self::I(i) => i.cmp(other),
1192 Self::J(j) => j.cmp(other),
1193 }
1194 }
1195
1196 #[inline]
1197 fn partial_cmp<K>(self, other: K) -> Option<core::cmp::Ordering>
1198 where
1199 K: IntoIterator,
1200 Self::Item: PartialOrd<<K as IntoIterator>::Item>,
1201 Self: Sized,
1202 {
1203 match self {
1204 Self::I(i) => i.partial_cmp(other),
1205 Self::J(j) => j.partial_cmp(other),
1206 }
1207 }
1208
1209 #[inline]
1210 fn eq<K>(self, other: K) -> bool
1211 where
1212 K: IntoIterator,
1213 Self::Item: PartialEq<<K as IntoIterator>::Item>,
1214 Self: Sized,
1215 {
1216 match self {
1217 Self::I(i) => i.eq(other),
1218 Self::J(j) => j.eq(other),
1219 }
1220 }
1221
1222 #[inline]
1223 fn ne<K>(self, other: K) -> bool
1224 where
1225 K: IntoIterator,
1226 Self::Item: PartialEq<<K as IntoIterator>::Item>,
1227 Self: Sized,
1228 {
1229 match self {
1230 Self::I(i) => i.ne(other),
1231 Self::J(j) => j.ne(other),
1232 }
1233 }
1234
1235 #[inline]
1236 fn lt<K>(self, other: K) -> bool
1237 where
1238 K: IntoIterator,
1239 Self::Item: PartialOrd<<K as IntoIterator>::Item>,
1240 Self: Sized,
1241 {
1242 match self {
1243 Self::I(i) => i.lt(other),
1244 Self::J(j) => j.lt(other),
1245 }
1246 }
1247
1248 #[inline]
1249 fn le<K>(self, other: K) -> bool
1250 where
1251 K: IntoIterator,
1252 Self::Item: PartialOrd<<K as IntoIterator>::Item>,
1253 Self: Sized,
1254 {
1255 match self {
1256 Self::I(i) => i.le(other),
1257 Self::J(j) => j.le(other),
1258 }
1259 }
1260
1261 #[inline]
1262 fn gt<K>(self, other: K) -> bool
1263 where
1264 K: IntoIterator,
1265 Self::Item: PartialOrd<<K as IntoIterator>::Item>,
1266 Self: Sized,
1267 {
1268 match self {
1269 Self::I(i) => i.gt(other),
1270 Self::J(j) => j.gt(other),
1271 }
1272 }
1273
1274 #[inline]
1275 fn ge<K>(self, other: K) -> bool
1276 where
1277 K: IntoIterator,
1278 Self::Item: PartialOrd<<K as IntoIterator>::Item>,
1279 Self: Sized,
1280 {
1281 match self {
1282 Self::I(i) => i.ge(other),
1283 Self::J(j) => j.ge(other),
1284 }
1285 }
1286}
1287
1288impl<I: DoubleEndedIterator, J: DoubleEndedIterator<Item = I::Item>> DoubleEndedIterator
1289 for EitherIterator<I, J>
1290{
1291 #[inline]
1292 fn next_back(&mut self) -> Option<Self::Item> {
1293 match self {
1294 Self::I(i) => i.next_back(),
1295 Self::J(j) => j.next_back(),
1296 }
1297 }
1298}
1299
1300impl<I: ExactSizeIterator, J: ExactSizeIterator<Item = I::Item>> ExactSizeIterator
1301 for EitherIterator<I, J>
1302{
1303}
1304
1305impl<I: core::iter::FusedIterator, J: core::iter::FusedIterator<Item = I::Item>>
1306 core::iter::FusedIterator for EitherIterator<I, J>
1307{
1308}