1
1
use crate :: char;
2
2
use crate :: convert:: TryFrom ;
3
+ use crate :: marker:: Destruct ;
3
4
use crate :: mem;
4
5
use crate :: ops:: { self , Try } ;
5
6
@@ -523,6 +524,7 @@ macro_rules! range_incl_exact_iter_impl {
523
524
}
524
525
525
526
/// Specialization implementations for `Range`.
527
+ #[ const_trait]
526
528
trait RangeIteratorImpl {
527
529
type Item ;
528
530
@@ -537,7 +539,7 @@ trait RangeIteratorImpl {
537
539
fn spec_advance_back_by ( & mut self , n : usize ) -> Result < ( ) , usize > ;
538
540
}
539
541
540
- impl < A : Step > RangeIteratorImpl for ops:: Range < A > {
542
+ impl < A : ~ const Step + ~ const Destruct > const RangeIteratorImpl for ops:: Range < A > {
541
543
type Item = A ;
542
544
543
545
#[ inline]
@@ -623,7 +625,7 @@ impl<A: Step> RangeIteratorImpl for ops::Range<A> {
623
625
}
624
626
}
625
627
626
- impl < T : TrustedStep > RangeIteratorImpl for ops:: Range < T > {
628
+ impl < T : ~ const TrustedStep + ~ const Destruct > const RangeIteratorImpl for ops:: Range < T > {
627
629
#[ inline]
628
630
fn spec_next ( & mut self ) -> Option < T > {
629
631
if self . start < self . end {
@@ -711,6 +713,70 @@ impl<T: TrustedStep> RangeIteratorImpl for ops::Range<T> {
711
713
}
712
714
713
715
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
716
+ #[ rustc_const_unstable( feature = "const_iter" , issue = "92476" ) ]
717
+ #[ cfg( not( bootstrap) ) ]
718
+ impl < A : ~const Step + ~const Destruct > const Iterator for ops:: Range < A > {
719
+ type Item = A ;
720
+
721
+ #[ inline]
722
+ fn next ( & mut self ) -> Option < A > {
723
+ self . spec_next ( )
724
+ }
725
+
726
+ #[ inline]
727
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
728
+ if self . start < self . end {
729
+ let hint = Step :: steps_between ( & self . start , & self . end ) ;
730
+ ( hint. unwrap_or ( usize:: MAX ) , hint)
731
+ } else {
732
+ ( 0 , Some ( 0 ) )
733
+ }
734
+ }
735
+
736
+ #[ inline]
737
+ fn nth ( & mut self , n : usize ) -> Option < A > {
738
+ self . spec_nth ( n)
739
+ }
740
+
741
+ #[ inline]
742
+ fn last ( mut self ) -> Option < A > {
743
+ self . next_back ( )
744
+ }
745
+
746
+ #[ inline]
747
+ fn min ( mut self ) -> Option < A > {
748
+ self . next ( )
749
+ }
750
+
751
+ #[ inline]
752
+ fn max ( mut self ) -> Option < A > {
753
+ self . next_back ( )
754
+ }
755
+
756
+ #[ inline]
757
+ fn is_sorted ( self ) -> bool {
758
+ true
759
+ }
760
+
761
+ #[ inline]
762
+ fn advance_by ( & mut self , n : usize ) -> Result < ( ) , usize > {
763
+ self . spec_advance_by ( n)
764
+ }
765
+
766
+ #[ inline]
767
+ unsafe fn __iterator_get_unchecked ( & mut self , idx : usize ) -> Self :: Item
768
+ where
769
+ Self : TrustedRandomAccessNoCoerce ,
770
+ {
771
+ // SAFETY: The TrustedRandomAccess contract requires that callers only pass an index
772
+ // that is in bounds.
773
+ // Additionally Self: TrustedRandomAccess is only implemented for Copy types
774
+ // which means even repeated reads of the same index would be safe.
775
+ unsafe { Step :: forward_unchecked ( self . start . clone ( ) , idx) }
776
+ }
777
+ }
778
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
779
+ #[ cfg( bootstrap) ]
714
780
impl < A : Step > Iterator for ops:: Range < A > {
715
781
type Item = A ;
716
782
@@ -821,6 +887,27 @@ range_incl_exact_iter_impl! {
821
887
}
822
888
823
889
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
890
+ #[ rustc_const_unstable( feature = "const_iter" , issue = "92476" ) ]
891
+ #[ cfg( not( bootstrap) ) ]
892
+ impl < A : ~const Step + ~const Destruct > const DoubleEndedIterator for ops:: Range < A > {
893
+ #[ inline]
894
+ fn next_back ( & mut self ) -> Option < A > {
895
+ self . spec_next_back ( )
896
+ }
897
+
898
+ #[ inline]
899
+ fn nth_back ( & mut self , n : usize ) -> Option < A > {
900
+ self . spec_nth_back ( n)
901
+ }
902
+
903
+ #[ inline]
904
+ fn advance_back_by ( & mut self , n : usize ) -> Result < ( ) , usize > {
905
+ self . spec_advance_back_by ( n)
906
+ }
907
+ }
908
+
909
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
910
+ #[ cfg( bootstrap) ]
824
911
impl < A : Step > DoubleEndedIterator for ops:: Range < A > {
825
912
#[ inline]
826
913
fn next_back ( & mut self ) -> Option < A > {
0 commit comments