11use crate :: char;
22use crate :: convert:: TryFrom ;
3+ use crate :: marker:: Destruct ;
34use crate :: mem;
45use crate :: ops:: { self , Try } ;
56
@@ -523,6 +524,7 @@ macro_rules! range_incl_exact_iter_impl {
523524}
524525
525526/// Specialization implementations for `Range`.
527+ #[ const_trait]
526528trait RangeIteratorImpl {
527529 type Item ;
528530
@@ -537,7 +539,7 @@ trait RangeIteratorImpl {
537539 fn spec_advance_back_by ( & mut self , n : usize ) -> Result < ( ) , usize > ;
538540}
539541
540- impl < A : Step > RangeIteratorImpl for ops:: Range < A > {
542+ impl < A : ~ const Step + ~ const Destruct > const RangeIteratorImpl for ops:: Range < A > {
541543 type Item = A ;
542544
543545 #[ inline]
@@ -623,7 +625,7 @@ impl<A: Step> RangeIteratorImpl for ops::Range<A> {
623625 }
624626}
625627
626- impl < T : TrustedStep > RangeIteratorImpl for ops:: Range < T > {
628+ impl < T : ~ const TrustedStep + ~ const Destruct > const RangeIteratorImpl for ops:: Range < T > {
627629 #[ inline]
628630 fn spec_next ( & mut self ) -> Option < T > {
629631 if self . start < self . end {
@@ -711,6 +713,70 @@ impl<T: TrustedStep> RangeIteratorImpl for ops::Range<T> {
711713}
712714
713715#[ 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) ]
714780impl < A : Step > Iterator for ops:: Range < A > {
715781 type Item = A ;
716782
@@ -821,6 +887,27 @@ range_incl_exact_iter_impl! {
821887}
822888
823889#[ 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) ]
824911impl < A : Step > DoubleEndedIterator for ops:: Range < A > {
825912 #[ inline]
826913 fn next_back ( & mut self ) -> Option < A > {
0 commit comments