1#[macro_use] mod macros;
5
6use super::{from_raw_parts, from_raw_parts_mut};
7use crate::hint::assert_unchecked;
8use crate::iter::{
9 FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce, UncheckedIterator,
10};
11use crate::marker::PhantomData;
12use crate::mem::{self, SizedTypeProperties};
13use crate::num::NonZero;
14use crate::ptr::{NonNull, without_provenance, without_provenance_mut};
15use crate::{cmp, fmt};
16
17#[stable(feature = "boxed_slice_into_iter", since = "1.80.0")]
18impl<T> !Iterator for [T] {}
19
20#[stable(feature = "rust1", since = "1.0.0")]
21impl<'a, T> IntoIterator for &'a [T] {
22 type Item = &'a T;
23 type IntoIter = Iter<'a, T>;
24
25 fn into_iter(self) -> Iter<'a, T> {
26 self.iter()
27 }
28}
29
30#[stable(feature = "rust1", since = "1.0.0")]
31impl<'a, T> IntoIterator for &'a mut [T] {
32 type Item = &'a mut T;
33 type IntoIter = IterMut<'a, T>;
34
35 fn into_iter(self) -> IterMut<'a, T> {
36 self.iter_mut()
37 }
38}
39
40#[stable(feature = "rust1", since = "1.0.0")]
67#[must_use = "iterators are lazy and do nothing unless consumed"]
68#[rustc_diagnostic_item = "SliceIter"]
69pub struct Iter<'a, T: 'a> {
70 ptr: NonNull<T>,
75 end_or_len: *const T,
79 _marker: PhantomData<&'a T>,
80}
81
82#[stable(feature = "core_impl_debug", since = "1.9.0")]
83impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 f.debug_tuple("Iter").field(&self.as_slice()).finish()
86 }
87}
88
89#[stable(feature = "rust1", since = "1.0.0")]
90unsafe impl<T: Sync> Sync for Iter<'_, T> {}
91#[stable(feature = "rust1", since = "1.0.0")]
92unsafe impl<T: Sync> Send for Iter<'_, T> {}
93
94impl<'a, T> Iter<'a, T> {
95 #[inline]
96 pub(super) const fn new(slice: &'a [T]) -> Self {
97 let len = slice.len();
98 let ptr: NonNull<T> = NonNull::from_ref(slice).cast();
99 unsafe {
101 let end_or_len =
102 if T::IS_ZST { without_provenance(len) } else { ptr.as_ptr().add(len) };
103
104 Self { ptr, end_or_len, _marker: PhantomData }
105 }
106 }
107
108 #[must_use]
135 #[stable(feature = "iter_to_slice", since = "1.4.0")]
136 #[inline]
137 pub fn as_slice(&self) -> &'a [T] {
138 self.make_slice()
139 }
140}
141
142iterator! {struct Iter -> *const T, &'a T, const, {}, as_ref, each_ref, {
143 fn is_sorted_by<F>(self, mut compare: F) -> bool
144 where
145 Self: Sized,
146 F: FnMut(&Self::Item, &Self::Item) -> bool,
147 {
148 self.as_slice().is_sorted_by(|a, b| compare(&a, &b))
149 }
150}}
151
152#[stable(feature = "rust1", since = "1.0.0")]
153impl<T> Clone for Iter<'_, T> {
154 #[inline]
155 fn clone(&self) -> Self {
156 Iter { ptr: self.ptr, end_or_len: self.end_or_len, _marker: self._marker }
157 }
158}
159
160#[stable(feature = "slice_iter_as_ref", since = "1.13.0")]
161impl<T> AsRef<[T]> for Iter<'_, T> {
162 #[inline]
163 fn as_ref(&self) -> &[T] {
164 self.as_slice()
165 }
166}
167
168#[stable(feature = "rust1", since = "1.0.0")]
193#[must_use = "iterators are lazy and do nothing unless consumed"]
194pub struct IterMut<'a, T: 'a> {
195 ptr: NonNull<T>,
200 end_or_len: *mut T,
204 _marker: PhantomData<&'a mut T>,
205}
206
207#[stable(feature = "core_impl_debug", since = "1.9.0")]
208impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
209 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
210 f.debug_tuple("IterMut").field(&self.make_slice()).finish()
211 }
212}
213
214#[stable(feature = "rust1", since = "1.0.0")]
215unsafe impl<T: Sync> Sync for IterMut<'_, T> {}
216#[stable(feature = "rust1", since = "1.0.0")]
217unsafe impl<T: Send> Send for IterMut<'_, T> {}
218
219impl<'a, T> IterMut<'a, T> {
220 #[inline]
221 pub(super) const fn new(slice: &'a mut [T]) -> Self {
222 let len = slice.len();
223 let ptr: NonNull<T> = NonNull::from_mut(slice).cast();
224 unsafe {
241 let end_or_len =
242 if T::IS_ZST { without_provenance_mut(len) } else { ptr.as_ptr().add(len) };
243
244 Self { ptr, end_or_len, _marker: PhantomData }
245 }
246 }
247
248 #[must_use = "`self` will be dropped if the result is not used"]
275 #[stable(feature = "iter_to_slice", since = "1.4.0")]
276 pub fn into_slice(self) -> &'a mut [T] {
277 unsafe { from_raw_parts_mut(self.ptr.as_ptr(), len!(self)) }
281 }
282
283 #[must_use]
311 #[stable(feature = "slice_iter_mut_as_slice", since = "1.53.0")]
312 #[inline]
313 pub fn as_slice(&self) -> &[T] {
314 self.make_slice()
315 }
316
317 #[must_use]
346 #[unstable(feature = "slice_iter_mut_as_mut_slice", issue = "93079")]
348 pub fn as_mut_slice(&mut self) -> &mut [T] {
349 unsafe { from_raw_parts_mut(self.ptr.as_ptr(), len!(self)) }
353 }
354}
355
356#[stable(feature = "slice_iter_mut_as_slice", since = "1.53.0")]
357impl<T> AsRef<[T]> for IterMut<'_, T> {
358 #[inline]
359 fn as_ref(&self) -> &[T] {
360 self.as_slice()
361 }
362}
363
364iterator! {struct IterMut -> *mut T, &'a mut T, mut, {mut}, as_mut, each_mut, {}}
372
373#[doc(hidden)]
376pub(super) trait SplitIter: DoubleEndedIterator {
377 fn finish(&mut self) -> Option<Self::Item>;
380}
381
382#[stable(feature = "rust1", since = "1.0.0")]
400#[must_use = "iterators are lazy and do nothing unless consumed"]
401pub struct Split<'a, T: 'a, P>
402where
403 P: FnMut(&T) -> bool,
404{
405 pub(crate) v: &'a [T],
407 pred: P,
408 pub(crate) finished: bool,
410}
411
412impl<'a, T: 'a, P: FnMut(&T) -> bool> Split<'a, T, P> {
413 #[inline]
414 pub(super) fn new(slice: &'a [T], pred: P) -> Self {
415 Self { v: slice, pred, finished: false }
416 }
417 #[unstable(feature = "split_as_slice", issue = "96137")]
428 pub fn as_slice(&self) -> &'a [T] {
429 if self.finished { &[] } else { &self.v }
430 }
431}
432
433#[stable(feature = "core_impl_debug", since = "1.9.0")]
434impl<T: fmt::Debug, P> fmt::Debug for Split<'_, T, P>
435where
436 P: FnMut(&T) -> bool,
437{
438 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
439 f.debug_struct("Split").field("v", &self.v).field("finished", &self.finished).finish()
440 }
441}
442
443#[stable(feature = "rust1", since = "1.0.0")]
445impl<T, P> Clone for Split<'_, T, P>
446where
447 P: Clone + FnMut(&T) -> bool,
448{
449 fn clone(&self) -> Self {
450 Split { v: self.v, pred: self.pred.clone(), finished: self.finished }
451 }
452}
453
454#[stable(feature = "rust1", since = "1.0.0")]
455impl<'a, T, P> Iterator for Split<'a, T, P>
456where
457 P: FnMut(&T) -> bool,
458{
459 type Item = &'a [T];
460
461 #[inline]
462 fn next(&mut self) -> Option<&'a [T]> {
463 if self.finished {
464 return None;
465 }
466
467 match self.v.iter().position(|x| (self.pred)(x)) {
468 None => self.finish(),
469 Some(idx) => {
470 let (left, right) =
471 unsafe { (self.v.get_unchecked(..idx), self.v.get_unchecked(idx + 1..)) };
474 let ret = Some(left);
475 self.v = right;
476 ret
477 }
478 }
479 }
480
481 #[inline]
482 fn size_hint(&self) -> (usize, Option<usize>) {
483 if self.finished {
484 (0, Some(0))
485 } else {
486 (1, Some(self.v.len() + 1))
489 }
490 }
491}
492
493#[stable(feature = "rust1", since = "1.0.0")]
494impl<'a, T, P> DoubleEndedIterator for Split<'a, T, P>
495where
496 P: FnMut(&T) -> bool,
497{
498 #[inline]
499 fn next_back(&mut self) -> Option<&'a [T]> {
500 if self.finished {
501 return None;
502 }
503
504 match self.v.iter().rposition(|x| (self.pred)(x)) {
505 None => self.finish(),
506 Some(idx) => {
507 let (left, right) =
508 unsafe { (self.v.get_unchecked(..idx), self.v.get_unchecked(idx + 1..)) };
511 let ret = Some(right);
512 self.v = left;
513 ret
514 }
515 }
516 }
517}
518
519impl<'a, T, P> SplitIter for Split<'a, T, P>
520where
521 P: FnMut(&T) -> bool,
522{
523 #[inline]
524 fn finish(&mut self) -> Option<&'a [T]> {
525 if self.finished {
526 None
527 } else {
528 self.finished = true;
529 Some(self.v)
530 }
531 }
532}
533
534#[stable(feature = "fused", since = "1.26.0")]
535impl<T, P> FusedIterator for Split<'_, T, P> where P: FnMut(&T) -> bool {}
536
537#[stable(feature = "split_inclusive", since = "1.51.0")]
556#[must_use = "iterators are lazy and do nothing unless consumed"]
557pub struct SplitInclusive<'a, T: 'a, P>
558where
559 P: FnMut(&T) -> bool,
560{
561 v: &'a [T],
562 pred: P,
563 finished: bool,
564}
565
566impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitInclusive<'a, T, P> {
567 #[inline]
568 pub(super) fn new(slice: &'a [T], pred: P) -> Self {
569 let finished = slice.is_empty();
570 Self { v: slice, pred, finished }
571 }
572}
573
574#[stable(feature = "split_inclusive", since = "1.51.0")]
575impl<T: fmt::Debug, P> fmt::Debug for SplitInclusive<'_, T, P>
576where
577 P: FnMut(&T) -> bool,
578{
579 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
580 f.debug_struct("SplitInclusive")
581 .field("v", &self.v)
582 .field("finished", &self.finished)
583 .finish()
584 }
585}
586
587#[stable(feature = "split_inclusive", since = "1.51.0")]
589impl<T, P> Clone for SplitInclusive<'_, T, P>
590where
591 P: Clone + FnMut(&T) -> bool,
592{
593 fn clone(&self) -> Self {
594 SplitInclusive { v: self.v, pred: self.pred.clone(), finished: self.finished }
595 }
596}
597
598#[stable(feature = "split_inclusive", since = "1.51.0")]
599impl<'a, T, P> Iterator for SplitInclusive<'a, T, P>
600where
601 P: FnMut(&T) -> bool,
602{
603 type Item = &'a [T];
604
605 #[inline]
606 fn next(&mut self) -> Option<&'a [T]> {
607 if self.finished {
608 return None;
609 }
610
611 let idx =
612 self.v.iter().position(|x| (self.pred)(x)).map(|idx| idx + 1).unwrap_or(self.v.len());
613 if idx == self.v.len() {
614 self.finished = true;
615 }
616 let ret = Some(&self.v[..idx]);
617 self.v = &self.v[idx..];
618 ret
619 }
620
621 #[inline]
622 fn size_hint(&self) -> (usize, Option<usize>) {
623 if self.finished {
624 (0, Some(0))
625 } else {
626 (1, Some(cmp::max(1, self.v.len())))
630 }
631 }
632}
633
634#[stable(feature = "split_inclusive", since = "1.51.0")]
635impl<'a, T, P> DoubleEndedIterator for SplitInclusive<'a, T, P>
636where
637 P: FnMut(&T) -> bool,
638{
639 #[inline]
640 fn next_back(&mut self) -> Option<&'a [T]> {
641 if self.finished {
642 return None;
643 }
644
645 let remainder = if self.v.is_empty() { &[] } else { &self.v[..(self.v.len() - 1)] };
649 let idx = remainder.iter().rposition(|x| (self.pred)(x)).map(|idx| idx + 1).unwrap_or(0);
650 if idx == 0 {
651 self.finished = true;
652 }
653 let ret = Some(&self.v[idx..]);
654 self.v = &self.v[..idx];
655 ret
656 }
657}
658
659#[stable(feature = "split_inclusive", since = "1.51.0")]
660impl<T, P> FusedIterator for SplitInclusive<'_, T, P> where P: FnMut(&T) -> bool {}
661
662#[stable(feature = "rust1", since = "1.0.0")]
677#[must_use = "iterators are lazy and do nothing unless consumed"]
678pub struct SplitMut<'a, T: 'a, P>
679where
680 P: FnMut(&T) -> bool,
681{
682 v: &'a mut [T],
683 pred: P,
684 finished: bool,
685}
686
687impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitMut<'a, T, P> {
688 #[inline]
689 pub(super) fn new(slice: &'a mut [T], pred: P) -> Self {
690 Self { v: slice, pred, finished: false }
691 }
692}
693
694#[stable(feature = "core_impl_debug", since = "1.9.0")]
695impl<T: fmt::Debug, P> fmt::Debug for SplitMut<'_, T, P>
696where
697 P: FnMut(&T) -> bool,
698{
699 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
700 f.debug_struct("SplitMut").field("v", &self.v).field("finished", &self.finished).finish()
701 }
702}
703
704impl<'a, T, P> SplitIter for SplitMut<'a, T, P>
705where
706 P: FnMut(&T) -> bool,
707{
708 #[inline]
709 fn finish(&mut self) -> Option<&'a mut [T]> {
710 if self.finished {
711 None
712 } else {
713 self.finished = true;
714 Some(mem::take(&mut self.v))
715 }
716 }
717}
718
719#[stable(feature = "rust1", since = "1.0.0")]
720impl<'a, T, P> Iterator for SplitMut<'a, T, P>
721where
722 P: FnMut(&T) -> bool,
723{
724 type Item = &'a mut [T];
725
726 #[inline]
727 fn next(&mut self) -> Option<&'a mut [T]> {
728 if self.finished {
729 return None;
730 }
731
732 match self.v.iter().position(|x| (self.pred)(x)) {
733 None => self.finish(),
734 Some(idx) => {
735 let tmp = mem::take(&mut self.v);
736 let (head, tail) = tmp.split_at_mut(idx + 1);
740 self.v = tail;
741 Some(&mut head[..idx])
743 }
744 }
745 }
746
747 #[inline]
748 fn size_hint(&self) -> (usize, Option<usize>) {
749 if self.finished {
750 (0, Some(0))
751 } else {
752 (1, Some(self.v.len() + 1))
755 }
756 }
757}
758
759#[stable(feature = "rust1", since = "1.0.0")]
760impl<'a, T, P> DoubleEndedIterator for SplitMut<'a, T, P>
761where
762 P: FnMut(&T) -> bool,
763{
764 #[inline]
765 fn next_back(&mut self) -> Option<&'a mut [T]> {
766 if self.finished {
767 return None;
768 }
769
770 let idx_opt = {
771 let pred = &mut self.pred;
773 self.v.iter().rposition(|x| (*pred)(x))
774 };
775 match idx_opt {
776 None => self.finish(),
777 Some(idx) => {
778 let tmp = mem::take(&mut self.v);
779 let (head, tail) = tmp.split_at_mut(idx);
780 self.v = head;
781 Some(&mut tail[1..])
782 }
783 }
784 }
785}
786
787#[stable(feature = "fused", since = "1.26.0")]
788impl<T, P> FusedIterator for SplitMut<'_, T, P> where P: FnMut(&T) -> bool {}
789
790#[stable(feature = "split_inclusive", since = "1.51.0")]
806#[must_use = "iterators are lazy and do nothing unless consumed"]
807pub struct SplitInclusiveMut<'a, T: 'a, P>
808where
809 P: FnMut(&T) -> bool,
810{
811 v: &'a mut [T],
812 pred: P,
813 finished: bool,
814}
815
816impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitInclusiveMut<'a, T, P> {
817 #[inline]
818 pub(super) fn new(slice: &'a mut [T], pred: P) -> Self {
819 let finished = slice.is_empty();
820 Self { v: slice, pred, finished }
821 }
822}
823
824#[stable(feature = "split_inclusive", since = "1.51.0")]
825impl<T: fmt::Debug, P> fmt::Debug for SplitInclusiveMut<'_, T, P>
826where
827 P: FnMut(&T) -> bool,
828{
829 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
830 f.debug_struct("SplitInclusiveMut")
831 .field("v", &self.v)
832 .field("finished", &self.finished)
833 .finish()
834 }
835}
836
837#[stable(feature = "split_inclusive", since = "1.51.0")]
838impl<'a, T, P> Iterator for SplitInclusiveMut<'a, T, P>
839where
840 P: FnMut(&T) -> bool,
841{
842 type Item = &'a mut [T];
843
844 #[inline]
845 fn next(&mut self) -> Option<&'a mut [T]> {
846 if self.finished {
847 return None;
848 }
849
850 let idx_opt = {
851 let pred = &mut self.pred;
853 self.v.iter().position(|x| (*pred)(x))
854 };
855 let idx = idx_opt.map(|idx| idx + 1).unwrap_or(self.v.len());
856 if idx == self.v.len() {
857 self.finished = true;
858 }
859 let tmp = mem::take(&mut self.v);
860 let (head, tail) = tmp.split_at_mut(idx);
861 self.v = tail;
862 Some(head)
863 }
864
865 #[inline]
866 fn size_hint(&self) -> (usize, Option<usize>) {
867 if self.finished {
868 (0, Some(0))
869 } else {
870 (1, Some(cmp::max(1, self.v.len())))
874 }
875 }
876}
877
878#[stable(feature = "split_inclusive", since = "1.51.0")]
879impl<'a, T, P> DoubleEndedIterator for SplitInclusiveMut<'a, T, P>
880where
881 P: FnMut(&T) -> bool,
882{
883 #[inline]
884 fn next_back(&mut self) -> Option<&'a mut [T]> {
885 if self.finished {
886 return None;
887 }
888
889 let idx_opt = if self.v.is_empty() {
890 None
891 } else {
892 let pred = &mut self.pred;
894
895 let remainder = &self.v[..(self.v.len() - 1)];
899 remainder.iter().rposition(|x| (*pred)(x))
900 };
901 let idx = idx_opt.map(|idx| idx + 1).unwrap_or(0);
902 if idx == 0 {
903 self.finished = true;
904 }
905 let tmp = mem::take(&mut self.v);
906 let (head, tail) = tmp.split_at_mut(idx);
907 self.v = head;
908 Some(tail)
909 }
910}
911
912#[stable(feature = "split_inclusive", since = "1.51.0")]
913impl<T, P> FusedIterator for SplitInclusiveMut<'_, T, P> where P: FnMut(&T) -> bool {}
914
915#[stable(feature = "slice_rsplit", since = "1.27.0")]
933#[must_use = "iterators are lazy and do nothing unless consumed"]
934pub struct RSplit<'a, T: 'a, P>
935where
936 P: FnMut(&T) -> bool,
937{
938 inner: Split<'a, T, P>,
939}
940
941impl<'a, T: 'a, P: FnMut(&T) -> bool> RSplit<'a, T, P> {
942 #[inline]
943 pub(super) fn new(slice: &'a [T], pred: P) -> Self {
944 Self { inner: Split::new(slice, pred) }
945 }
946}
947
948#[stable(feature = "slice_rsplit", since = "1.27.0")]
949impl<T: fmt::Debug, P> fmt::Debug for RSplit<'_, T, P>
950where
951 P: FnMut(&T) -> bool,
952{
953 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
954 f.debug_struct("RSplit")
955 .field("v", &self.inner.v)
956 .field("finished", &self.inner.finished)
957 .finish()
958 }
959}
960
961#[stable(feature = "slice_rsplit", since = "1.27.0")]
963impl<T, P> Clone for RSplit<'_, T, P>
964where
965 P: Clone + FnMut(&T) -> bool,
966{
967 fn clone(&self) -> Self {
968 RSplit { inner: self.inner.clone() }
969 }
970}
971
972#[stable(feature = "slice_rsplit", since = "1.27.0")]
973impl<'a, T, P> Iterator for RSplit<'a, T, P>
974where
975 P: FnMut(&T) -> bool,
976{
977 type Item = &'a [T];
978
979 #[inline]
980 fn next(&mut self) -> Option<&'a [T]> {
981 self.inner.next_back()
982 }
983
984 #[inline]
985 fn size_hint(&self) -> (usize, Option<usize>) {
986 self.inner.size_hint()
987 }
988}
989
990#[stable(feature = "slice_rsplit", since = "1.27.0")]
991impl<'a, T, P> DoubleEndedIterator for RSplit<'a, T, P>
992where
993 P: FnMut(&T) -> bool,
994{
995 #[inline]
996 fn next_back(&mut self) -> Option<&'a [T]> {
997 self.inner.next()
998 }
999}
1000
1001#[stable(feature = "slice_rsplit", since = "1.27.0")]
1002impl<'a, T, P> SplitIter for RSplit<'a, T, P>
1003where
1004 P: FnMut(&T) -> bool,
1005{
1006 #[inline]
1007 fn finish(&mut self) -> Option<&'a [T]> {
1008 self.inner.finish()
1009 }
1010}
1011
1012#[stable(feature = "slice_rsplit", since = "1.27.0")]
1013impl<T, P> FusedIterator for RSplit<'_, T, P> where P: FnMut(&T) -> bool {}
1014
1015#[stable(feature = "slice_rsplit", since = "1.27.0")]
1030#[must_use = "iterators are lazy and do nothing unless consumed"]
1031pub struct RSplitMut<'a, T: 'a, P>
1032where
1033 P: FnMut(&T) -> bool,
1034{
1035 inner: SplitMut<'a, T, P>,
1036}
1037
1038impl<'a, T: 'a, P: FnMut(&T) -> bool> RSplitMut<'a, T, P> {
1039 #[inline]
1040 pub(super) fn new(slice: &'a mut [T], pred: P) -> Self {
1041 Self { inner: SplitMut::new(slice, pred) }
1042 }
1043}
1044
1045#[stable(feature = "slice_rsplit", since = "1.27.0")]
1046impl<T: fmt::Debug, P> fmt::Debug for RSplitMut<'_, T, P>
1047where
1048 P: FnMut(&T) -> bool,
1049{
1050 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1051 f.debug_struct("RSplitMut")
1052 .field("v", &self.inner.v)
1053 .field("finished", &self.inner.finished)
1054 .finish()
1055 }
1056}
1057
1058#[stable(feature = "slice_rsplit", since = "1.27.0")]
1059impl<'a, T, P> SplitIter for RSplitMut<'a, T, P>
1060where
1061 P: FnMut(&T) -> bool,
1062{
1063 #[inline]
1064 fn finish(&mut self) -> Option<&'a mut [T]> {
1065 self.inner.finish()
1066 }
1067}
1068
1069#[stable(feature = "slice_rsplit", since = "1.27.0")]
1070impl<'a, T, P> Iterator for RSplitMut<'a, T, P>
1071where
1072 P: FnMut(&T) -> bool,
1073{
1074 type Item = &'a mut [T];
1075
1076 #[inline]
1077 fn next(&mut self) -> Option<&'a mut [T]> {
1078 self.inner.next_back()
1079 }
1080
1081 #[inline]
1082 fn size_hint(&self) -> (usize, Option<usize>) {
1083 self.inner.size_hint()
1084 }
1085}
1086
1087#[stable(feature = "slice_rsplit", since = "1.27.0")]
1088impl<'a, T, P> DoubleEndedIterator for RSplitMut<'a, T, P>
1089where
1090 P: FnMut(&T) -> bool,
1091{
1092 #[inline]
1093 fn next_back(&mut self) -> Option<&'a mut [T]> {
1094 self.inner.next()
1095 }
1096}
1097
1098#[stable(feature = "slice_rsplit", since = "1.27.0")]
1099impl<T, P> FusedIterator for RSplitMut<'_, T, P> where P: FnMut(&T) -> bool {}
1100
1101#[derive(Debug)]
1105struct GenericSplitN<I> {
1106 iter: I,
1107 count: usize,
1108}
1109
1110impl<T, I: SplitIter<Item = T>> Iterator for GenericSplitN<I> {
1111 type Item = T;
1112
1113 #[inline]
1114 fn next(&mut self) -> Option<T> {
1115 match self.count {
1116 0 => None,
1117 1 => {
1118 self.count -= 1;
1119 self.iter.finish()
1120 }
1121 _ => {
1122 self.count -= 1;
1123 self.iter.next()
1124 }
1125 }
1126 }
1127
1128 #[inline]
1129 fn size_hint(&self) -> (usize, Option<usize>) {
1130 let (lower, upper_opt) = self.iter.size_hint();
1131 (
1132 cmp::min(self.count, lower),
1133 Some(upper_opt.map_or(self.count, |upper| cmp::min(self.count, upper))),
1134 )
1135 }
1136}
1137
1138#[stable(feature = "rust1", since = "1.0.0")]
1156#[must_use = "iterators are lazy and do nothing unless consumed"]
1157pub struct SplitN<'a, T: 'a, P>
1158where
1159 P: FnMut(&T) -> bool,
1160{
1161 inner: GenericSplitN<Split<'a, T, P>>,
1162}
1163
1164impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitN<'a, T, P> {
1165 #[inline]
1166 pub(super) fn new(s: Split<'a, T, P>, n: usize) -> Self {
1167 Self { inner: GenericSplitN { iter: s, count: n } }
1168 }
1169}
1170
1171#[stable(feature = "core_impl_debug", since = "1.9.0")]
1172impl<T: fmt::Debug, P> fmt::Debug for SplitN<'_, T, P>
1173where
1174 P: FnMut(&T) -> bool,
1175{
1176 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1177 f.debug_struct("SplitN").field("inner", &self.inner).finish()
1178 }
1179}
1180
1181#[stable(feature = "rust1", since = "1.0.0")]
1200#[must_use = "iterators are lazy and do nothing unless consumed"]
1201pub struct RSplitN<'a, T: 'a, P>
1202where
1203 P: FnMut(&T) -> bool,
1204{
1205 inner: GenericSplitN<RSplit<'a, T, P>>,
1206}
1207
1208impl<'a, T: 'a, P: FnMut(&T) -> bool> RSplitN<'a, T, P> {
1209 #[inline]
1210 pub(super) fn new(s: RSplit<'a, T, P>, n: usize) -> Self {
1211 Self { inner: GenericSplitN { iter: s, count: n } }
1212 }
1213}
1214
1215#[stable(feature = "core_impl_debug", since = "1.9.0")]
1216impl<T: fmt::Debug, P> fmt::Debug for RSplitN<'_, T, P>
1217where
1218 P: FnMut(&T) -> bool,
1219{
1220 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1221 f.debug_struct("RSplitN").field("inner", &self.inner).finish()
1222 }
1223}
1224
1225#[stable(feature = "rust1", since = "1.0.0")]
1240#[must_use = "iterators are lazy and do nothing unless consumed"]
1241pub struct SplitNMut<'a, T: 'a, P>
1242where
1243 P: FnMut(&T) -> bool,
1244{
1245 inner: GenericSplitN<SplitMut<'a, T, P>>,
1246}
1247
1248impl<'a, T: 'a, P: FnMut(&T) -> bool> SplitNMut<'a, T, P> {
1249 #[inline]
1250 pub(super) fn new(s: SplitMut<'a, T, P>, n: usize) -> Self {
1251 Self { inner: GenericSplitN { iter: s, count: n } }
1252 }
1253}
1254
1255#[stable(feature = "core_impl_debug", since = "1.9.0")]
1256impl<T: fmt::Debug, P> fmt::Debug for SplitNMut<'_, T, P>
1257where
1258 P: FnMut(&T) -> bool,
1259{
1260 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1261 f.debug_struct("SplitNMut").field("inner", &self.inner).finish()
1262 }
1263}
1264
1265#[stable(feature = "rust1", since = "1.0.0")]
1281#[must_use = "iterators are lazy and do nothing unless consumed"]
1282pub struct RSplitNMut<'a, T: 'a, P>
1283where
1284 P: FnMut(&T) -> bool,
1285{
1286 inner: GenericSplitN<RSplitMut<'a, T, P>>,
1287}
1288
1289impl<'a, T: 'a, P: FnMut(&T) -> bool> RSplitNMut<'a, T, P> {
1290 #[inline]
1291 pub(super) fn new(s: RSplitMut<'a, T, P>, n: usize) -> Self {
1292 Self { inner: GenericSplitN { iter: s, count: n } }
1293 }
1294}
1295
1296#[stable(feature = "core_impl_debug", since = "1.9.0")]
1297impl<T: fmt::Debug, P> fmt::Debug for RSplitNMut<'_, T, P>
1298where
1299 P: FnMut(&T) -> bool,
1300{
1301 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1302 f.debug_struct("RSplitNMut").field("inner", &self.inner).finish()
1303 }
1304}
1305
1306forward_iterator! { SplitN: T, &'a [T] }
1307forward_iterator! { RSplitN: T, &'a [T] }
1308forward_iterator! { SplitNMut: T, &'a mut [T] }
1309forward_iterator! { RSplitNMut: T, &'a mut [T] }
1310
1311#[derive(Debug)]
1329#[stable(feature = "rust1", since = "1.0.0")]
1330#[must_use = "iterators are lazy and do nothing unless consumed"]
1331pub struct Windows<'a, T: 'a> {
1332 v: &'a [T],
1333 size: NonZero<usize>,
1334}
1335
1336impl<'a, T: 'a> Windows<'a, T> {
1337 #[inline]
1338 pub(super) const fn new(slice: &'a [T], size: NonZero<usize>) -> Self {
1339 Self { v: slice, size }
1340 }
1341}
1342
1343#[stable(feature = "rust1", since = "1.0.0")]
1345impl<T> Clone for Windows<'_, T> {
1346 fn clone(&self) -> Self {
1347 Windows { v: self.v, size: self.size }
1348 }
1349}
1350
1351#[stable(feature = "rust1", since = "1.0.0")]
1352impl<'a, T> Iterator for Windows<'a, T> {
1353 type Item = &'a [T];
1354
1355 #[inline]
1356 fn next(&mut self) -> Option<&'a [T]> {
1357 if self.size.get() > self.v.len() {
1358 None
1359 } else {
1360 let ret = Some(&self.v[..self.size.get()]);
1361 self.v = &self.v[1..];
1362 ret
1363 }
1364 }
1365
1366 #[inline]
1367 fn size_hint(&self) -> (usize, Option<usize>) {
1368 if self.size.get() > self.v.len() {
1369 (0, Some(0))
1370 } else {
1371 let size = self.v.len() - self.size.get() + 1;
1372 (size, Some(size))
1373 }
1374 }
1375
1376 #[inline]
1377 fn count(self) -> usize {
1378 self.len()
1379 }
1380
1381 #[inline]
1382 fn nth(&mut self, n: usize) -> Option<Self::Item> {
1383 let size = self.size.get();
1384 if let Some(rest) = self.v.get(n..)
1385 && let Some(nth) = rest.get(..size)
1386 {
1387 self.v = &rest[1..];
1388 Some(nth)
1389 } else {
1390 self.v = &self.v[..0]; None
1393 }
1394 }
1395
1396 #[inline]
1397 fn last(self) -> Option<Self::Item> {
1398 if self.size.get() > self.v.len() {
1399 None
1400 } else {
1401 let start = self.v.len() - self.size.get();
1402 Some(&self.v[start..])
1403 }
1404 }
1405
1406 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
1407 unsafe { from_raw_parts(self.v.as_ptr().add(idx), self.size.get()) }
1412 }
1413}
1414
1415#[stable(feature = "rust1", since = "1.0.0")]
1416impl<'a, T> DoubleEndedIterator for Windows<'a, T> {
1417 #[inline]
1418 fn next_back(&mut self) -> Option<Self::Item> {
1419 self.nth_back(0)
1420 }
1421
1422 #[inline]
1423 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
1424 if let Some(end) = self.v.len().checked_sub(n)
1425 && let Some(start) = end.checked_sub(self.size.get())
1426 {
1427 let res = &self.v[start..end];
1428 self.v = &self.v[..end - 1];
1429 Some(res)
1430 } else {
1431 self.v = &self.v[..0]; None
1433 }
1434 }
1435}
1436
1437#[stable(feature = "rust1", since = "1.0.0")]
1438impl<T> ExactSizeIterator for Windows<'_, T> {}
1439
1440#[unstable(feature = "trusted_len", issue = "37572")]
1441unsafe impl<T> TrustedLen for Windows<'_, T> {}
1442
1443#[stable(feature = "fused", since = "1.26.0")]
1444impl<T> FusedIterator for Windows<'_, T> {}
1445
1446#[doc(hidden)]
1447#[unstable(feature = "trusted_random_access", issue = "none")]
1448unsafe impl<'a, T> TrustedRandomAccess for Windows<'a, T> {}
1449
1450#[doc(hidden)]
1451#[unstable(feature = "trusted_random_access", issue = "none")]
1452unsafe impl<'a, T> TrustedRandomAccessNoCoerce for Windows<'a, T> {
1453 const MAY_HAVE_SIDE_EFFECT: bool = false;
1454}
1455
1456#[derive(Debug)]
1478#[stable(feature = "rust1", since = "1.0.0")]
1479#[must_use = "iterators are lazy and do nothing unless consumed"]
1480pub struct Chunks<'a, T: 'a> {
1481 v: &'a [T],
1482 chunk_size: usize,
1483}
1484
1485impl<'a, T: 'a> Chunks<'a, T> {
1486 #[inline]
1487 pub(super) const fn new(slice: &'a [T], size: usize) -> Self {
1488 Self { v: slice, chunk_size: size }
1489 }
1490}
1491
1492#[stable(feature = "rust1", since = "1.0.0")]
1494impl<T> Clone for Chunks<'_, T> {
1495 fn clone(&self) -> Self {
1496 Chunks { v: self.v, chunk_size: self.chunk_size }
1497 }
1498}
1499
1500#[stable(feature = "rust1", since = "1.0.0")]
1501impl<'a, T> Iterator for Chunks<'a, T> {
1502 type Item = &'a [T];
1503
1504 #[inline]
1505 fn next(&mut self) -> Option<&'a [T]> {
1506 if self.v.is_empty() {
1507 None
1508 } else {
1509 let chunksz = cmp::min(self.v.len(), self.chunk_size);
1510 let (fst, snd) = self.v.split_at(chunksz);
1511 self.v = snd;
1512 Some(fst)
1513 }
1514 }
1515
1516 #[inline]
1517 fn size_hint(&self) -> (usize, Option<usize>) {
1518 if self.v.is_empty() {
1519 (0, Some(0))
1520 } else {
1521 let n = self.v.len().div_ceil(self.chunk_size);
1522 (n, Some(n))
1523 }
1524 }
1525
1526 #[inline]
1527 fn count(self) -> usize {
1528 self.len()
1529 }
1530
1531 #[inline]
1532 fn nth(&mut self, n: usize) -> Option<Self::Item> {
1533 if let Some(start) = n.checked_mul(self.chunk_size)
1534 && start < self.v.len()
1535 {
1536 let rest = &self.v[start..];
1537 let (chunk, rest) = rest.split_at(self.chunk_size.min(rest.len()));
1538 self.v = rest;
1539 Some(chunk)
1540 } else {
1541 self.v = &self.v[..0]; None
1543 }
1544 }
1545
1546 #[inline]
1547 fn last(self) -> Option<Self::Item> {
1548 if self.v.is_empty() {
1549 None
1550 } else {
1551 let start = (self.v.len() - 1) / self.chunk_size * self.chunk_size;
1552 Some(&self.v[start..])
1553 }
1554 }
1555
1556 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
1557 let start = idx * self.chunk_size;
1558 unsafe {
1566 let len = cmp::min(self.v.len().unchecked_sub(start), self.chunk_size);
1567 from_raw_parts(self.v.as_ptr().add(start), len)
1568 }
1569 }
1570}
1571
1572#[stable(feature = "rust1", since = "1.0.0")]
1573impl<'a, T> DoubleEndedIterator for Chunks<'a, T> {
1574 #[inline]
1575 fn next_back(&mut self) -> Option<&'a [T]> {
1576 if self.v.is_empty() {
1577 None
1578 } else {
1579 let remainder = self.v.len() % self.chunk_size;
1580 let chunksz = if remainder != 0 { remainder } else { self.chunk_size };
1581 let (fst, snd) = unsafe { self.v.split_at_unchecked(self.v.len() - chunksz) };
1596 self.v = fst;
1597 Some(snd)
1598 }
1599 }
1600
1601 #[inline]
1602 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
1603 let len = self.len();
1604 if n < len {
1605 let start = (len - 1 - n) * self.chunk_size;
1606 let end = start + (self.v.len() - start).min(self.chunk_size);
1607 let nth_back = &self.v[start..end];
1608 self.v = &self.v[..start];
1609 Some(nth_back)
1610 } else {
1611 self.v = &self.v[..0]; None
1613 }
1614 }
1615}
1616
1617#[stable(feature = "rust1", since = "1.0.0")]
1618impl<T> ExactSizeIterator for Chunks<'_, T> {}
1619
1620#[unstable(feature = "trusted_len", issue = "37572")]
1621unsafe impl<T> TrustedLen for Chunks<'_, T> {}
1622
1623#[stable(feature = "fused", since = "1.26.0")]
1624impl<T> FusedIterator for Chunks<'_, T> {}
1625
1626#[doc(hidden)]
1627#[unstable(feature = "trusted_random_access", issue = "none")]
1628unsafe impl<'a, T> TrustedRandomAccess for Chunks<'a, T> {}
1629
1630#[doc(hidden)]
1631#[unstable(feature = "trusted_random_access", issue = "none")]
1632unsafe impl<'a, T> TrustedRandomAccessNoCoerce for Chunks<'a, T> {
1633 const MAY_HAVE_SIDE_EFFECT: bool = false;
1634}
1635
1636#[derive(Debug)]
1654#[stable(feature = "rust1", since = "1.0.0")]
1655#[must_use = "iterators are lazy and do nothing unless consumed"]
1656pub struct ChunksMut<'a, T: 'a> {
1657 v: *mut [T],
1664 chunk_size: usize,
1665 _marker: PhantomData<&'a mut T>,
1666}
1667
1668impl<'a, T: 'a> ChunksMut<'a, T> {
1669 #[inline]
1670 pub(super) const fn new(slice: &'a mut [T], size: usize) -> Self {
1671 Self { v: slice, chunk_size: size, _marker: PhantomData }
1672 }
1673}
1674
1675#[stable(feature = "rust1", since = "1.0.0")]
1676impl<'a, T> Iterator for ChunksMut<'a, T> {
1677 type Item = &'a mut [T];
1678
1679 #[inline]
1680 fn next(&mut self) -> Option<&'a mut [T]> {
1681 if self.v.is_empty() {
1682 None
1683 } else {
1684 let sz = cmp::min(self.v.len(), self.chunk_size);
1685 let (head, tail) = unsafe { self.v.split_at_mut(sz) };
1687 self.v = tail;
1688 Some(unsafe { &mut *head })
1690 }
1691 }
1692
1693 #[inline]
1694 fn size_hint(&self) -> (usize, Option<usize>) {
1695 if self.v.is_empty() {
1696 (0, Some(0))
1697 } else {
1698 let n = self.v.len().div_ceil(self.chunk_size);
1699 (n, Some(n))
1700 }
1701 }
1702
1703 #[inline]
1704 fn count(self) -> usize {
1705 self.len()
1706 }
1707
1708 #[inline]
1709 fn nth(&mut self, n: usize) -> Option<&'a mut [T]> {
1710 if let Some(start) = n.checked_mul(self.chunk_size)
1711 && start < self.v.len()
1712 {
1713 let (_, rest) = unsafe { self.v.split_at_mut(start) };
1715 let (chunk, rest) = unsafe { rest.split_at_mut(self.chunk_size.min(rest.len())) };
1717 self.v = rest;
1718 Some(unsafe { &mut *chunk })
1720 } else {
1721 self.v = &mut [];
1722 None
1723 }
1724 }
1725
1726 #[inline]
1727 fn last(self) -> Option<Self::Item> {
1728 if self.v.is_empty() {
1729 None
1730 } else {
1731 let start = (self.v.len() - 1) / self.chunk_size * self.chunk_size;
1732 Some(unsafe { &mut *self.v.get_unchecked_mut(start..) })
1734 }
1735 }
1736
1737 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
1738 let start = idx * self.chunk_size;
1739 unsafe {
1746 let len = cmp::min(self.v.len().unchecked_sub(start), self.chunk_size);
1747 from_raw_parts_mut(self.v.as_mut_ptr().add(start), len)
1748 }
1749 }
1750}
1751
1752#[stable(feature = "rust1", since = "1.0.0")]
1753impl<'a, T> DoubleEndedIterator for ChunksMut<'a, T> {
1754 #[inline]
1755 fn next_back(&mut self) -> Option<&'a mut [T]> {
1756 if self.v.is_empty() {
1757 None
1758 } else {
1759 let remainder = self.v.len() % self.chunk_size;
1760 let sz = if remainder != 0 { remainder } else { self.chunk_size };
1761 let len = self.v.len();
1762 let (head, tail) = unsafe { self.v.split_at_mut_unchecked(len - sz) };
1764 self.v = head;
1765 Some(unsafe { &mut *tail })
1767 }
1768 }
1769
1770 #[inline]
1771 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
1772 let len = self.len();
1773 if n < len {
1774 let start = (len - 1 - n) * self.chunk_size;
1775 let end = match start.checked_add(self.chunk_size) {
1776 Some(res) => cmp::min(self.v.len(), res),
1777 None => self.v.len(),
1778 };
1779 let (temp, _tail) = unsafe { self.v.split_at_mut(end) };
1781 let (head, nth_back) = unsafe { temp.split_at_mut(start) };
1783 self.v = head;
1784 Some(unsafe { &mut *nth_back })
1786 } else {
1787 self.v = &mut [];
1788 None
1789 }
1790 }
1791}
1792
1793#[stable(feature = "rust1", since = "1.0.0")]
1794impl<T> ExactSizeIterator for ChunksMut<'_, T> {}
1795
1796#[unstable(feature = "trusted_len", issue = "37572")]
1797unsafe impl<T> TrustedLen for ChunksMut<'_, T> {}
1798
1799#[stable(feature = "fused", since = "1.26.0")]
1800impl<T> FusedIterator for ChunksMut<'_, T> {}
1801
1802#[doc(hidden)]
1803#[unstable(feature = "trusted_random_access", issue = "none")]
1804unsafe impl<'a, T> TrustedRandomAccess for ChunksMut<'a, T> {}
1805
1806#[doc(hidden)]
1807#[unstable(feature = "trusted_random_access", issue = "none")]
1808unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksMut<'a, T> {
1809 const MAY_HAVE_SIDE_EFFECT: bool = false;
1810}
1811
1812#[stable(feature = "rust1", since = "1.0.0")]
1813unsafe impl<T> Send for ChunksMut<'_, T> where T: Send {}
1814
1815#[stable(feature = "rust1", since = "1.0.0")]
1816unsafe impl<T> Sync for ChunksMut<'_, T> where T: Sync {}
1817
1818#[derive(Debug)]
1841#[stable(feature = "chunks_exact", since = "1.31.0")]
1842#[must_use = "iterators are lazy and do nothing unless consumed"]
1843pub struct ChunksExact<'a, T: 'a> {
1844 v: &'a [T],
1845 rem: &'a [T],
1846 chunk_size: usize,
1847}
1848
1849impl<'a, T> ChunksExact<'a, T> {
1850 #[inline]
1851 pub(super) const fn new(slice: &'a [T], chunk_size: usize) -> Self {
1852 let rem = slice.len() % chunk_size;
1853 let fst_len = slice.len() - rem;
1854 let (fst, snd) = unsafe { slice.split_at_unchecked(fst_len) };
1856 Self { v: fst, rem: snd, chunk_size }
1857 }
1858
1859 #[must_use]
1877 #[stable(feature = "chunks_exact", since = "1.31.0")]
1878 pub fn remainder(&self) -> &'a [T] {
1879 self.rem
1880 }
1881}
1882
1883#[stable(feature = "chunks_exact", since = "1.31.0")]
1885impl<T> Clone for ChunksExact<'_, T> {
1886 fn clone(&self) -> Self {
1887 ChunksExact { v: self.v, rem: self.rem, chunk_size: self.chunk_size }
1888 }
1889}
1890
1891#[stable(feature = "chunks_exact", since = "1.31.0")]
1892impl<'a, T> Iterator for ChunksExact<'a, T> {
1893 type Item = &'a [T];
1894
1895 #[inline]
1896 fn next(&mut self) -> Option<&'a [T]> {
1897 self.v.split_at_checked(self.chunk_size).and_then(|(chunk, rest)| {
1898 self.v = rest;
1899 Some(chunk)
1900 })
1901 }
1902
1903 #[inline]
1904 fn size_hint(&self) -> (usize, Option<usize>) {
1905 let n = self.v.len() / self.chunk_size;
1906 (n, Some(n))
1907 }
1908
1909 #[inline]
1910 fn count(self) -> usize {
1911 self.len()
1912 }
1913
1914 #[inline]
1915 fn nth(&mut self, n: usize) -> Option<Self::Item> {
1916 if let Some(start) = n.checked_mul(self.chunk_size)
1917 && start < self.v.len()
1918 {
1919 self.v = &self.v[start..];
1920 self.next()
1921 } else {
1922 self.v = &self.v[..0]; None
1924 }
1925 }
1926
1927 #[inline]
1928 fn last(mut self) -> Option<Self::Item> {
1929 self.next_back()
1930 }
1931
1932 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
1933 let start = idx * self.chunk_size;
1934 unsafe { from_raw_parts(self.v.as_ptr().add(start), self.chunk_size) }
1936 }
1937}
1938
1939#[stable(feature = "chunks_exact", since = "1.31.0")]
1940impl<'a, T> DoubleEndedIterator for ChunksExact<'a, T> {
1941 #[inline]
1942 fn next_back(&mut self) -> Option<&'a [T]> {
1943 if self.v.len() < self.chunk_size {
1944 None
1945 } else {
1946 let (fst, snd) = self.v.split_at(self.v.len() - self.chunk_size);
1947 self.v = fst;
1948 Some(snd)
1949 }
1950 }
1951
1952 #[inline]
1953 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
1954 let len = self.len();
1955 if n < len {
1956 let start = (len - 1 - n) * self.chunk_size;
1957 let end = start + self.chunk_size;
1958 let nth_back = &self.v[start..end];
1959 self.v = &self.v[..start];
1960 Some(nth_back)
1961 } else {
1962 self.v = &self.v[..0]; None
1964 }
1965 }
1966}
1967
1968#[stable(feature = "chunks_exact", since = "1.31.0")]
1969impl<T> ExactSizeIterator for ChunksExact<'_, T> {
1970 fn is_empty(&self) -> bool {
1971 self.v.is_empty()
1972 }
1973}
1974
1975#[unstable(feature = "trusted_len", issue = "37572")]
1976unsafe impl<T> TrustedLen for ChunksExact<'_, T> {}
1977
1978#[stable(feature = "chunks_exact", since = "1.31.0")]
1979impl<T> FusedIterator for ChunksExact<'_, T> {}
1980
1981#[doc(hidden)]
1982#[unstable(feature = "trusted_random_access", issue = "none")]
1983unsafe impl<'a, T> TrustedRandomAccess for ChunksExact<'a, T> {}
1984
1985#[doc(hidden)]
1986#[unstable(feature = "trusted_random_access", issue = "none")]
1987unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksExact<'a, T> {
1988 const MAY_HAVE_SIDE_EFFECT: bool = false;
1989}
1990
1991#[derive(Debug)]
2011#[stable(feature = "chunks_exact", since = "1.31.0")]
2012#[must_use = "iterators are lazy and do nothing unless consumed"]
2013pub struct ChunksExactMut<'a, T: 'a> {
2014 v: *mut [T],
2021 rem: &'a mut [T], chunk_size: usize,
2023 _marker: PhantomData<&'a mut T>,
2024}
2025
2026impl<'a, T> ChunksExactMut<'a, T> {
2027 #[inline]
2028 pub(super) const fn new(slice: &'a mut [T], chunk_size: usize) -> Self {
2029 let rem = slice.len() % chunk_size;
2030 let fst_len = slice.len() - rem;
2031 let (fst, snd) = unsafe { slice.split_at_mut_unchecked(fst_len) };
2033 Self { v: fst, rem: snd, chunk_size, _marker: PhantomData }
2034 }
2035
2036 #[must_use = "`self` will be dropped if the result is not used"]
2040 #[stable(feature = "chunks_exact", since = "1.31.0")]
2041 pub fn into_remainder(self) -> &'a mut [T] {
2042 self.rem
2043 }
2044}
2045
2046#[stable(feature = "chunks_exact", since = "1.31.0")]
2047impl<'a, T> Iterator for ChunksExactMut<'a, T> {
2048 type Item = &'a mut [T];
2049
2050 #[inline]
2051 fn next(&mut self) -> Option<&'a mut [T]> {
2052 unsafe { &mut *self.v }.split_at_mut_checked(self.chunk_size).and_then(|(chunk, rest)| {
2054 self.v = rest;
2055 Some(chunk)
2056 })
2057 }
2058
2059 #[inline]
2060 fn size_hint(&self) -> (usize, Option<usize>) {
2061 let n = self.v.len() / self.chunk_size;
2062 (n, Some(n))
2063 }
2064
2065 #[inline]
2066 fn count(self) -> usize {
2067 self.len()
2068 }
2069
2070 #[inline]
2071 fn nth(&mut self, n: usize) -> Option<&'a mut [T]> {
2072 if let Some(start) = n.checked_mul(self.chunk_size)
2073 && start < self.v.len()
2074 {
2075 self.v = unsafe { self.v.split_at_mut(start).1 };
2077 self.next()
2078 } else {
2079 self.v = &mut [];
2080 None
2081 }
2082 }
2083
2084 #[inline]
2085 fn last(mut self) -> Option<Self::Item> {
2086 self.next_back()
2087 }
2088
2089 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
2090 let start = idx * self.chunk_size;
2091 unsafe { from_raw_parts_mut(self.v.as_mut_ptr().add(start), self.chunk_size) }
2093 }
2094}
2095
2096#[stable(feature = "chunks_exact", since = "1.31.0")]
2097impl<'a, T> DoubleEndedIterator for ChunksExactMut<'a, T> {
2098 #[inline]
2099 fn next_back(&mut self) -> Option<&'a mut [T]> {
2100 if self.v.len() < self.chunk_size {
2101 None
2102 } else {
2103 let (head, tail) = unsafe { self.v.split_at_mut(self.v.len() - self.chunk_size) };
2105 self.v = head;
2106 Some(unsafe { &mut *tail })
2108 }
2109 }
2110
2111 #[inline]
2112 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
2113 let len = self.len();
2114 if n < len {
2115 let start = (len - 1 - n) * self.chunk_size;
2116 let end = start + self.chunk_size;
2117 let (temp, _tail) = unsafe { mem::replace(&mut self.v, &mut []).split_at_mut(end) };
2119 let (head, nth_back) = unsafe { temp.split_at_mut(start) };
2121 self.v = head;
2122 Some(unsafe { &mut *nth_back })
2124 } else {
2125 self.v = &mut [];
2126 None
2127 }
2128 }
2129}
2130
2131#[stable(feature = "chunks_exact", since = "1.31.0")]
2132impl<T> ExactSizeIterator for ChunksExactMut<'_, T> {
2133 fn is_empty(&self) -> bool {
2134 self.v.is_empty()
2135 }
2136}
2137
2138#[unstable(feature = "trusted_len", issue = "37572")]
2139unsafe impl<T> TrustedLen for ChunksExactMut<'_, T> {}
2140
2141#[stable(feature = "chunks_exact", since = "1.31.0")]
2142impl<T> FusedIterator for ChunksExactMut<'_, T> {}
2143
2144#[doc(hidden)]
2145#[unstable(feature = "trusted_random_access", issue = "none")]
2146unsafe impl<'a, T> TrustedRandomAccess for ChunksExactMut<'a, T> {}
2147
2148#[doc(hidden)]
2149#[unstable(feature = "trusted_random_access", issue = "none")]
2150unsafe impl<'a, T> TrustedRandomAccessNoCoerce for ChunksExactMut<'a, T> {
2151 const MAY_HAVE_SIDE_EFFECT: bool = false;
2152}
2153
2154#[stable(feature = "chunks_exact", since = "1.31.0")]
2155unsafe impl<T> Send for ChunksExactMut<'_, T> where T: Send {}
2156
2157#[stable(feature = "chunks_exact", since = "1.31.0")]
2158unsafe impl<T> Sync for ChunksExactMut<'_, T> where T: Sync {}
2159
2160#[derive(Debug, Clone, Copy)]
2179#[stable(feature = "array_windows", since = "CURRENT_RUSTC_VERSION")]
2180#[must_use = "iterators are lazy and do nothing unless consumed"]
2181pub struct ArrayWindows<'a, T: 'a, const N: usize> {
2182 v: &'a [T],
2183}
2184
2185impl<'a, T: 'a, const N: usize> ArrayWindows<'a, T, N> {
2186 #[inline]
2187 pub(super) const fn new(slice: &'a [T]) -> Self {
2188 Self { v: slice }
2189 }
2190}
2191
2192#[stable(feature = "array_windows", since = "CURRENT_RUSTC_VERSION")]
2193impl<'a, T, const N: usize> Iterator for ArrayWindows<'a, T, N> {
2194 type Item = &'a [T; N];
2195
2196 #[inline]
2197 fn next(&mut self) -> Option<Self::Item> {
2198 let ret = self.v.first_chunk();
2199 if ret.is_some() {
2200 self.v = &self.v[1..];
2201 }
2202 ret
2203 }
2204
2205 #[inline]
2206 fn size_hint(&self) -> (usize, Option<usize>) {
2207 let size = self.v.len().saturating_sub(N - 1);
2208 (size, Some(size))
2209 }
2210
2211 #[inline]
2212 fn count(self) -> usize {
2213 self.len()
2214 }
2215
2216 #[inline]
2217 fn nth(&mut self, n: usize) -> Option<Self::Item> {
2218 let idx = n.min(self.v.len());
2219 self.v = &self.v[idx..];
2220 self.next()
2221 }
2222
2223 #[inline]
2224 fn last(self) -> Option<Self::Item> {
2225 self.v.last_chunk()
2226 }
2227}
2228
2229#[stable(feature = "array_windows", since = "CURRENT_RUSTC_VERSION")]
2230impl<'a, T, const N: usize> DoubleEndedIterator for ArrayWindows<'a, T, N> {
2231 #[inline]
2232 fn next_back(&mut self) -> Option<&'a [T; N]> {
2233 let ret = self.v.last_chunk();
2234 if ret.is_some() {
2235 self.v = &self.v[..self.v.len() - 1];
2236 }
2237 ret
2238 }
2239
2240 #[inline]
2241 fn nth_back(&mut self, n: usize) -> Option<&'a [T; N]> {
2242 let idx = self.v.len().saturating_sub(n);
2243 self.v = &self.v[..idx];
2244 self.next_back()
2245 }
2246}
2247
2248#[stable(feature = "array_windows", since = "CURRENT_RUSTC_VERSION")]
2249impl<T, const N: usize> ExactSizeIterator for ArrayWindows<'_, T, N> {
2250 fn is_empty(&self) -> bool {
2251 self.v.len() < N
2252 }
2253}
2254
2255#[derive(Debug)]
2277#[stable(feature = "rchunks", since = "1.31.0")]
2278#[must_use = "iterators are lazy and do nothing unless consumed"]
2279pub struct RChunks<'a, T: 'a> {
2280 v: &'a [T],
2281 chunk_size: usize,
2282}
2283
2284impl<'a, T: 'a> RChunks<'a, T> {
2285 #[inline]
2286 pub(super) const fn new(slice: &'a [T], size: usize) -> Self {
2287 Self { v: slice, chunk_size: size }
2288 }
2289}
2290
2291#[stable(feature = "rchunks", since = "1.31.0")]
2293impl<T> Clone for RChunks<'_, T> {
2294 fn clone(&self) -> Self {
2295 RChunks { v: self.v, chunk_size: self.chunk_size }
2296 }
2297}
2298
2299#[stable(feature = "rchunks", since = "1.31.0")]
2300impl<'a, T> Iterator for RChunks<'a, T> {
2301 type Item = &'a [T];
2302
2303 #[inline]
2304 fn next(&mut self) -> Option<&'a [T]> {
2305 if self.v.is_empty() {
2306 None
2307 } else {
2308 let idx = self.v.len().saturating_sub(self.chunk_size);
2309 let (rest, chunk) = unsafe { self.v.split_at_unchecked(idx) };
2312 self.v = rest;
2313 Some(chunk)
2314 }
2315 }
2316
2317 #[inline]
2318 fn size_hint(&self) -> (usize, Option<usize>) {
2319 if self.v.is_empty() {
2320 (0, Some(0))
2321 } else {
2322 let n = self.v.len().div_ceil(self.chunk_size);
2323 (n, Some(n))
2324 }
2325 }
2326
2327 #[inline]
2328 fn count(self) -> usize {
2329 self.len()
2330 }
2331
2332 #[inline]
2333 fn nth(&mut self, n: usize) -> Option<Self::Item> {
2334 if let Some(end) = n.checked_mul(self.chunk_size)
2335 && end < self.v.len()
2336 {
2337 let end = self.v.len() - end;
2338 let rest = &self.v[..end];
2339 let (rest, chunk) = rest.split_at(end.saturating_sub(self.chunk_size));
2340 self.v = rest;
2341 Some(chunk)
2342 } else {
2343 self.v = &self.v[..0]; None
2345 }
2346 }
2347
2348 #[inline]
2349 fn last(self) -> Option<Self::Item> {
2350 if self.v.is_empty() {
2351 None
2352 } else {
2353 let rem = self.v.len() % self.chunk_size;
2354 let end = if rem == 0 { self.chunk_size } else { rem };
2355 Some(&self.v[0..end])
2356 }
2357 }
2358
2359 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
2360 let end = self.v.len() - idx * self.chunk_size;
2361 let start = end.saturating_sub(self.chunk_size);
2362 unsafe { from_raw_parts(self.v.as_ptr().add(start), end - start) }
2364 }
2365}
2366
2367#[stable(feature = "rchunks", since = "1.31.0")]
2368impl<'a, T> DoubleEndedIterator for RChunks<'a, T> {
2369 #[inline]
2370 fn next_back(&mut self) -> Option<&'a [T]> {
2371 if self.v.is_empty() {
2372 None
2373 } else {
2374 let remainder = self.v.len() % self.chunk_size;
2375 let chunksz = if remainder != 0 { remainder } else { self.chunk_size };
2376 let (fst, snd) = unsafe { self.v.split_at_unchecked(chunksz) };
2378 self.v = snd;
2379 Some(fst)
2380 }
2381 }
2382
2383 #[inline]
2384 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
2385 let len = self.len();
2386 if n < len {
2387 let offset_from_end = (len - 1 - n) * self.chunk_size;
2388 let end = self.v.len() - offset_from_end;
2389 let start = end.saturating_sub(self.chunk_size);
2390 let nth_back = &self.v[start..end];
2391 self.v = &self.v[end..];
2392 Some(nth_back)
2393 } else {
2394 self.v = &self.v[..0]; None
2396 }
2397 }
2398}
2399
2400#[stable(feature = "rchunks", since = "1.31.0")]
2401impl<T> ExactSizeIterator for RChunks<'_, T> {}
2402
2403#[unstable(feature = "trusted_len", issue = "37572")]
2404unsafe impl<T> TrustedLen for RChunks<'_, T> {}
2405
2406#[stable(feature = "rchunks", since = "1.31.0")]
2407impl<T> FusedIterator for RChunks<'_, T> {}
2408
2409#[doc(hidden)]
2410#[unstable(feature = "trusted_random_access", issue = "none")]
2411unsafe impl<'a, T> TrustedRandomAccess for RChunks<'a, T> {}
2412
2413#[doc(hidden)]
2414#[unstable(feature = "trusted_random_access", issue = "none")]
2415unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunks<'a, T> {
2416 const MAY_HAVE_SIDE_EFFECT: bool = false;
2417}
2418
2419#[derive(Debug)]
2437#[stable(feature = "rchunks", since = "1.31.0")]
2438#[must_use = "iterators are lazy and do nothing unless consumed"]
2439pub struct RChunksMut<'a, T: 'a> {
2440 v: *mut [T],
2447 chunk_size: usize,
2448 _marker: PhantomData<&'a mut T>,
2449}
2450
2451impl<'a, T: 'a> RChunksMut<'a, T> {
2452 #[inline]
2453 pub(super) const fn new(slice: &'a mut [T], size: usize) -> Self {
2454 Self { v: slice, chunk_size: size, _marker: PhantomData }
2455 }
2456}
2457
2458#[stable(feature = "rchunks", since = "1.31.0")]
2459impl<'a, T> Iterator for RChunksMut<'a, T> {
2460 type Item = &'a mut [T];
2461
2462 #[inline]
2463 fn next(&mut self) -> Option<&'a mut [T]> {
2464 if self.v.is_empty() {
2465 None
2466 } else {
2467 let idx = self.v.len().saturating_sub(self.chunk_size);
2468 let (rest, chunk) = unsafe { self.v.split_at_mut_unchecked(idx) };
2471 self.v = rest;
2472 Some(unsafe { &mut *chunk })
2474 }
2475 }
2476
2477 #[inline]
2478 fn size_hint(&self) -> (usize, Option<usize>) {
2479 if self.v.is_empty() {
2480 (0, Some(0))
2481 } else {
2482 let n = self.v.len().div_ceil(self.chunk_size);
2483 (n, Some(n))
2484 }
2485 }
2486
2487 #[inline]
2488 fn count(self) -> usize {
2489 self.len()
2490 }
2491
2492 #[inline]
2493 fn nth(&mut self, n: usize) -> Option<&'a mut [T]> {
2494 if let Some(end) = n.checked_mul(self.chunk_size)
2495 && end < self.v.len()
2496 {
2497 let end = self.v.len() - end;
2498 let start = match end.checked_sub(self.chunk_size) {
2499 Some(sum) => sum,
2500 None => 0,
2501 };
2502 let (head, tail) = unsafe { self.v.split_at_mut(start) };
2505 let (nth, _) = unsafe { tail.split_at_mut(end - start) };
2508 self.v = head;
2509 Some(unsafe { &mut *nth })
2511 } else {
2512 self.v = &mut [];
2513 None
2514 }
2515 }
2516
2517 #[inline]
2518 fn last(self) -> Option<Self::Item> {
2519 if self.v.is_empty() {
2520 None
2521 } else {
2522 let rem = self.v.len() % self.chunk_size;
2523 let end = if rem == 0 { self.chunk_size } else { rem };
2524 Some(unsafe { &mut *self.v.get_unchecked_mut(0..end) })
2526 }
2527 }
2528
2529 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
2530 let end = self.v.len() - idx * self.chunk_size;
2531 let start = end.saturating_sub(self.chunk_size);
2532 unsafe { from_raw_parts_mut(self.v.as_mut_ptr().add(start), end - start) }
2535 }
2536}
2537
2538#[stable(feature = "rchunks", since = "1.31.0")]
2539impl<'a, T> DoubleEndedIterator for RChunksMut<'a, T> {
2540 #[inline]
2541 fn next_back(&mut self) -> Option<&'a mut [T]> {
2542 if self.v.is_empty() {
2543 None
2544 } else {
2545 let remainder = self.v.len() % self.chunk_size;
2546 let sz = if remainder != 0 { remainder } else { self.chunk_size };
2547 let (head, tail) = unsafe { self.v.split_at_mut_unchecked(sz) };
2549 self.v = tail;
2550 Some(unsafe { &mut *head })
2552 }
2553 }
2554
2555 #[inline]
2556 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
2557 let len = self.len();
2558 if n < len {
2559 let offset_from_end = (len - 1 - n) * self.chunk_size;
2561 let end = self.v.len() - offset_from_end;
2562 let start = end.saturating_sub(self.chunk_size);
2563 let (tmp, tail) = unsafe { self.v.split_at_mut(end) };
2565 let (_, nth_back) = unsafe { tmp.split_at_mut(start) };
2567 self.v = tail;
2568 Some(unsafe { &mut *nth_back })
2570 } else {
2571 self.v = &mut [];
2572 None
2573 }
2574 }
2575}
2576
2577#[stable(feature = "rchunks", since = "1.31.0")]
2578impl<T> ExactSizeIterator for RChunksMut<'_, T> {}
2579
2580#[unstable(feature = "trusted_len", issue = "37572")]
2581unsafe impl<T> TrustedLen for RChunksMut<'_, T> {}
2582
2583#[stable(feature = "rchunks", since = "1.31.0")]
2584impl<T> FusedIterator for RChunksMut<'_, T> {}
2585
2586#[doc(hidden)]
2587#[unstable(feature = "trusted_random_access", issue = "none")]
2588unsafe impl<'a, T> TrustedRandomAccess for RChunksMut<'a, T> {}
2589
2590#[doc(hidden)]
2591#[unstable(feature = "trusted_random_access", issue = "none")]
2592unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunksMut<'a, T> {
2593 const MAY_HAVE_SIDE_EFFECT: bool = false;
2594}
2595
2596#[stable(feature = "rchunks", since = "1.31.0")]
2597unsafe impl<T> Send for RChunksMut<'_, T> where T: Send {}
2598
2599#[stable(feature = "rchunks", since = "1.31.0")]
2600unsafe impl<T> Sync for RChunksMut<'_, T> where T: Sync {}
2601
2602#[derive(Debug)]
2625#[stable(feature = "rchunks", since = "1.31.0")]
2626#[must_use = "iterators are lazy and do nothing unless consumed"]
2627pub struct RChunksExact<'a, T: 'a> {
2628 v: &'a [T],
2629 rem: &'a [T],
2630 chunk_size: usize,
2631}
2632
2633impl<'a, T> RChunksExact<'a, T> {
2634 #[inline]
2635 pub(super) const fn new(slice: &'a [T], chunk_size: usize) -> Self {
2636 let rem = slice.len() % chunk_size;
2637 let (fst, snd) = unsafe { slice.split_at_unchecked(rem) };
2639 Self { v: snd, rem: fst, chunk_size }
2640 }
2641
2642 #[must_use]
2660 #[stable(feature = "rchunks", since = "1.31.0")]
2661 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
2662 pub const fn remainder(&self) -> &'a [T] {
2663 self.rem
2664 }
2665}
2666
2667#[stable(feature = "rchunks", since = "1.31.0")]
2669impl<'a, T> Clone for RChunksExact<'a, T> {
2670 fn clone(&self) -> RChunksExact<'a, T> {
2671 RChunksExact { v: self.v, rem: self.rem, chunk_size: self.chunk_size }
2672 }
2673}
2674
2675#[stable(feature = "rchunks", since = "1.31.0")]
2676impl<'a, T> Iterator for RChunksExact<'a, T> {
2677 type Item = &'a [T];
2678
2679 #[inline]
2680 fn next(&mut self) -> Option<&'a [T]> {
2681 if self.v.len() < self.chunk_size {
2682 None
2683 } else {
2684 let (fst, snd) = self.v.split_at(self.v.len() - self.chunk_size);
2685 self.v = fst;
2686 Some(snd)
2687 }
2688 }
2689
2690 #[inline]
2691 fn size_hint(&self) -> (usize, Option<usize>) {
2692 let n = self.v.len() / self.chunk_size;
2693 (n, Some(n))
2694 }
2695
2696 #[inline]
2697 fn count(self) -> usize {
2698 self.len()
2699 }
2700
2701 #[inline]
2702 fn nth(&mut self, n: usize) -> Option<Self::Item> {
2703 if let Some(end) = n.checked_mul(self.chunk_size)
2704 && end < self.v.len()
2705 {
2706 self.v = &self.v[..self.v.len() - end];
2707 self.next()
2708 } else {
2709 self.v = &self.v[..0]; None
2711 }
2712 }
2713
2714 #[inline]
2715 fn last(mut self) -> Option<Self::Item> {
2716 self.next_back()
2717 }
2718
2719 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
2720 let end = self.v.len() - idx * self.chunk_size;
2721 let start = end - self.chunk_size;
2722 unsafe { from_raw_parts(self.v.as_ptr().add(start), self.chunk_size) }
2724 }
2725}
2726
2727#[stable(feature = "rchunks", since = "1.31.0")]
2728impl<'a, T> DoubleEndedIterator for RChunksExact<'a, T> {
2729 #[inline]
2730 fn next_back(&mut self) -> Option<&'a [T]> {
2731 if self.v.len() < self.chunk_size {
2732 None
2733 } else {
2734 let (fst, snd) = self.v.split_at(self.chunk_size);
2735 self.v = snd;
2736 Some(fst)
2737 }
2738 }
2739
2740 #[inline]
2741 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
2742 let len = self.len();
2743 if n < len {
2744 let offset = (len - n) * self.chunk_size;
2747 let start = self.v.len() - offset;
2748 let end = start + self.chunk_size;
2749 let nth_back = &self.v[start..end];
2750 self.v = &self.v[end..];
2751 Some(nth_back)
2752 } else {
2753 self.v = &self.v[..0]; None
2755 }
2756 }
2757}
2758
2759#[stable(feature = "rchunks", since = "1.31.0")]
2760impl<'a, T> ExactSizeIterator for RChunksExact<'a, T> {
2761 fn is_empty(&self) -> bool {
2762 self.v.is_empty()
2763 }
2764}
2765
2766#[unstable(feature = "trusted_len", issue = "37572")]
2767unsafe impl<T> TrustedLen for RChunksExact<'_, T> {}
2768
2769#[stable(feature = "rchunks", since = "1.31.0")]
2770impl<T> FusedIterator for RChunksExact<'_, T> {}
2771
2772#[doc(hidden)]
2773#[unstable(feature = "trusted_random_access", issue = "none")]
2774unsafe impl<'a, T> TrustedRandomAccess for RChunksExact<'a, T> {}
2775
2776#[doc(hidden)]
2777#[unstable(feature = "trusted_random_access", issue = "none")]
2778unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunksExact<'a, T> {
2779 const MAY_HAVE_SIDE_EFFECT: bool = false;
2780}
2781
2782#[derive(Debug)]
2802#[stable(feature = "rchunks", since = "1.31.0")]
2803#[must_use = "iterators are lazy and do nothing unless consumed"]
2804pub struct RChunksExactMut<'a, T: 'a> {
2805 v: *mut [T],
2812 rem: &'a mut [T],
2813 chunk_size: usize,
2814}
2815
2816impl<'a, T> RChunksExactMut<'a, T> {
2817 #[inline]
2818 pub(super) const fn new(slice: &'a mut [T], chunk_size: usize) -> Self {
2819 let rem = slice.len() % chunk_size;
2820 let (fst, snd) = unsafe { slice.split_at_mut_unchecked(rem) };
2822 Self { v: snd, rem: fst, chunk_size }
2823 }
2824
2825 #[must_use = "`self` will be dropped if the result is not used"]
2829 #[stable(feature = "rchunks", since = "1.31.0")]
2830 #[rustc_const_unstable(feature = "const_slice_make_iter", issue = "137737")]
2831 pub const fn into_remainder(self) -> &'a mut [T] {
2832 self.rem
2833 }
2834}
2835
2836#[stable(feature = "rchunks", since = "1.31.0")]
2837impl<'a, T> Iterator for RChunksExactMut<'a, T> {
2838 type Item = &'a mut [T];
2839
2840 #[inline]
2841 fn next(&mut self) -> Option<&'a mut [T]> {
2842 if self.v.len() < self.chunk_size {
2843 None
2844 } else {
2845 let len = self.v.len();
2846 let (head, tail) = unsafe { self.v.split_at_mut(len - self.chunk_size) };
2848 self.v = head;
2849 Some(unsafe { &mut *tail })
2851 }
2852 }
2853
2854 #[inline]
2855 fn size_hint(&self) -> (usize, Option<usize>) {
2856 let n = self.v.len() / self.chunk_size;
2857 (n, Some(n))
2858 }
2859
2860 #[inline]
2861 fn count(self) -> usize {
2862 self.len()
2863 }
2864
2865 #[inline]
2866 fn nth(&mut self, n: usize) -> Option<&'a mut [T]> {
2867 if let Some(end) = n.checked_mul(self.chunk_size)
2868 && end < self.v.len()
2869 {
2870 let idx = self.v.len() - end;
2871 let (fst, _) = unsafe { self.v.split_at_mut(idx) };
2873 self.v = fst;
2874 self.next()
2875 } else {
2876 self.v = &mut [];
2877 None
2878 }
2879 }
2880
2881 #[inline]
2882 fn last(mut self) -> Option<Self::Item> {
2883 self.next_back()
2884 }
2885
2886 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
2887 let end = self.v.len() - idx * self.chunk_size;
2888 let start = end - self.chunk_size;
2889 unsafe { from_raw_parts_mut(self.v.as_mut_ptr().add(start), self.chunk_size) }
2891 }
2892}
2893
2894#[stable(feature = "rchunks", since = "1.31.0")]
2895impl<'a, T> DoubleEndedIterator for RChunksExactMut<'a, T> {
2896 #[inline]
2897 fn next_back(&mut self) -> Option<&'a mut [T]> {
2898 if self.v.len() < self.chunk_size {
2899 None
2900 } else {
2901 let (head, tail) = unsafe { self.v.split_at_mut(self.chunk_size) };
2903 self.v = tail;
2904 Some(unsafe { &mut *head })
2906 }
2907 }
2908
2909 #[inline]
2910 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
2911 let len = self.len();
2912 if n < len {
2913 let offset = (len - n) * self.chunk_size;
2916 let start = self.v.len() - offset;
2917 let end = start + self.chunk_size;
2918 let (tmp, tail) = unsafe { self.v.split_at_mut(end) };
2920 let (_, nth_back) = unsafe { tmp.split_at_mut(start) };
2922 self.v = tail;
2923 Some(unsafe { &mut *nth_back })
2925 } else {
2926 self.v = &mut [];
2927 None
2928 }
2929 }
2930}
2931
2932#[stable(feature = "rchunks", since = "1.31.0")]
2933impl<T> ExactSizeIterator for RChunksExactMut<'_, T> {
2934 fn is_empty(&self) -> bool {
2935 self.v.is_empty()
2936 }
2937}
2938
2939#[unstable(feature = "trusted_len", issue = "37572")]
2940unsafe impl<T> TrustedLen for RChunksExactMut<'_, T> {}
2941
2942#[stable(feature = "rchunks", since = "1.31.0")]
2943impl<T> FusedIterator for RChunksExactMut<'_, T> {}
2944
2945#[doc(hidden)]
2946#[unstable(feature = "trusted_random_access", issue = "none")]
2947unsafe impl<'a, T> TrustedRandomAccess for RChunksExactMut<'a, T> {}
2948
2949#[doc(hidden)]
2950#[unstable(feature = "trusted_random_access", issue = "none")]
2951unsafe impl<'a, T> TrustedRandomAccessNoCoerce for RChunksExactMut<'a, T> {
2952 const MAY_HAVE_SIDE_EFFECT: bool = false;
2953}
2954
2955#[stable(feature = "rchunks", since = "1.31.0")]
2956unsafe impl<T> Send for RChunksExactMut<'_, T> where T: Send {}
2957
2958#[stable(feature = "rchunks", since = "1.31.0")]
2959unsafe impl<T> Sync for RChunksExactMut<'_, T> where T: Sync {}
2960
2961#[doc(hidden)]
2962#[unstable(feature = "trusted_random_access", issue = "none")]
2963unsafe impl<'a, T> TrustedRandomAccess for Iter<'a, T> {}
2964
2965#[doc(hidden)]
2966#[unstable(feature = "trusted_random_access", issue = "none")]
2967unsafe impl<'a, T> TrustedRandomAccessNoCoerce for Iter<'a, T> {
2968 const MAY_HAVE_SIDE_EFFECT: bool = false;
2969}
2970
2971#[doc(hidden)]
2972#[unstable(feature = "trusted_random_access", issue = "none")]
2973unsafe impl<'a, T> TrustedRandomAccess for IterMut<'a, T> {}
2974
2975#[doc(hidden)]
2976#[unstable(feature = "trusted_random_access", issue = "none")]
2977unsafe impl<'a, T> TrustedRandomAccessNoCoerce for IterMut<'a, T> {
2978 const MAY_HAVE_SIDE_EFFECT: bool = false;
2979}
2980
2981#[stable(feature = "slice_group_by", since = "1.77.0")]
2988#[must_use = "iterators are lazy and do nothing unless consumed"]
2989pub struct ChunkBy<'a, T: 'a, P> {
2990 slice: &'a [T],
2991 predicate: P,
2992}
2993
2994#[stable(feature = "slice_group_by", since = "1.77.0")]
2995impl<'a, T: 'a, P> ChunkBy<'a, T, P> {
2996 pub(super) const fn new(slice: &'a [T], predicate: P) -> Self {
2997 ChunkBy { slice, predicate }
2998 }
2999}
3000
3001#[stable(feature = "slice_group_by", since = "1.77.0")]
3002impl<'a, T: 'a, P> Iterator for ChunkBy<'a, T, P>
3003where
3004 P: FnMut(&T, &T) -> bool,
3005{
3006 type Item = &'a [T];
3007
3008 #[inline]
3009 fn next(&mut self) -> Option<Self::Item> {
3010 if self.slice.is_empty() {
3011 None
3012 } else {
3013 let mut len = 1;
3014 let mut iter = self.slice.windows(2);
3015 while let Some([l, r]) = iter.next() {
3016 if (self.predicate)(l, r) { len += 1 } else { break }
3017 }
3018 let (head, tail) = self.slice.split_at(len);
3019 self.slice = tail;
3020 Some(head)
3021 }
3022 }
3023
3024 #[inline]
3025 fn size_hint(&self) -> (usize, Option<usize>) {
3026 if self.slice.is_empty() { (0, Some(0)) } else { (1, Some(self.slice.len())) }
3027 }
3028
3029 #[inline]
3030 fn last(mut self) -> Option<Self::Item> {
3031 self.next_back()
3032 }
3033}
3034
3035#[stable(feature = "slice_group_by", since = "1.77.0")]
3036impl<'a, T: 'a, P> DoubleEndedIterator for ChunkBy<'a, T, P>
3037where
3038 P: FnMut(&T, &T) -> bool,
3039{
3040 #[inline]
3041 fn next_back(&mut self) -> Option<Self::Item> {
3042 if self.slice.is_empty() {
3043 None
3044 } else {
3045 let mut len = 1;
3046 let mut iter = self.slice.windows(2);
3047 while let Some([l, r]) = iter.next_back() {
3048 if (self.predicate)(l, r) { len += 1 } else { break }
3049 }
3050 let (head, tail) = self.slice.split_at(self.slice.len() - len);
3051 self.slice = head;
3052 Some(tail)
3053 }
3054 }
3055}
3056
3057#[stable(feature = "slice_group_by", since = "1.77.0")]
3058impl<'a, T: 'a, P> FusedIterator for ChunkBy<'a, T, P> where P: FnMut(&T, &T) -> bool {}
3059
3060#[stable(feature = "slice_group_by_clone", since = "1.89.0")]
3061impl<'a, T: 'a, P: Clone> Clone for ChunkBy<'a, T, P> {
3062 fn clone(&self) -> Self {
3063 Self { slice: self.slice, predicate: self.predicate.clone() }
3064 }
3065}
3066
3067#[stable(feature = "slice_group_by", since = "1.77.0")]
3068impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for ChunkBy<'a, T, P> {
3069 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3070 f.debug_struct("ChunkBy").field("slice", &self.slice).finish()
3071 }
3072}
3073
3074#[stable(feature = "slice_group_by", since = "1.77.0")]
3082#[must_use = "iterators are lazy and do nothing unless consumed"]
3083pub struct ChunkByMut<'a, T: 'a, P> {
3084 slice: &'a mut [T],
3085 predicate: P,
3086}
3087
3088#[stable(feature = "slice_group_by", since = "1.77.0")]
3089impl<'a, T: 'a, P> ChunkByMut<'a, T, P> {
3090 pub(super) const fn new(slice: &'a mut [T], predicate: P) -> Self {
3091 ChunkByMut { slice, predicate }
3092 }
3093}
3094
3095#[stable(feature = "slice_group_by", since = "1.77.0")]
3096impl<'a, T: 'a, P> Iterator for ChunkByMut<'a, T, P>
3097where
3098 P: FnMut(&T, &T) -> bool,
3099{
3100 type Item = &'a mut [T];
3101
3102 #[inline]
3103 fn next(&mut self) -> Option<Self::Item> {
3104 if self.slice.is_empty() {
3105 None
3106 } else {
3107 let mut len = 1;
3108 let mut iter = self.slice.windows(2);
3109 while let Some([l, r]) = iter.next() {
3110 if (self.predicate)(l, r) { len += 1 } else { break }
3111 }
3112 let slice = mem::take(&mut self.slice);
3113 let (head, tail) = slice.split_at_mut(len);
3114 self.slice = tail;
3115 Some(head)
3116 }
3117 }
3118
3119 #[inline]
3120 fn size_hint(&self) -> (usize, Option<usize>) {
3121 if self.slice.is_empty() { (0, Some(0)) } else { (1, Some(self.slice.len())) }
3122 }
3123
3124 #[inline]
3125 fn last(mut self) -> Option<Self::Item> {
3126 self.next_back()
3127 }
3128}
3129
3130#[stable(feature = "slice_group_by", since = "1.77.0")]
3131impl<'a, T: 'a, P> DoubleEndedIterator for ChunkByMut<'a, T, P>
3132where
3133 P: FnMut(&T, &T) -> bool,
3134{
3135 #[inline]
3136 fn next_back(&mut self) -> Option<Self::Item> {
3137 if self.slice.is_empty() {
3138 None
3139 } else {
3140 let mut len = 1;
3141 let mut iter = self.slice.windows(2);
3142 while let Some([l, r]) = iter.next_back() {
3143 if (self.predicate)(l, r) { len += 1 } else { break }
3144 }
3145 let slice = mem::take(&mut self.slice);
3146 let (head, tail) = slice.split_at_mut(slice.len() - len);
3147 self.slice = head;
3148 Some(tail)
3149 }
3150 }
3151}
3152
3153#[stable(feature = "slice_group_by", since = "1.77.0")]
3154impl<'a, T: 'a, P> FusedIterator for ChunkByMut<'a, T, P> where P: FnMut(&T, &T) -> bool {}
3155
3156#[stable(feature = "slice_group_by", since = "1.77.0")]
3157impl<'a, T: 'a + fmt::Debug, P> fmt::Debug for ChunkByMut<'a, T, P> {
3158 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3159 f.debug_struct("ChunkByMut").field("slice", &self.slice).finish()
3160 }
3161}