@@ -9,15 +9,8 @@ use super::{FusedIterator, TrustedLen, TrustedRandomAccess};
9
9
///
10
10
/// The *successor* operation moves towards values that compare greater.
11
11
/// The *predecessor* operation moves towards values that compare lesser.
12
- ///
13
- /// # Safety
14
- ///
15
- /// This trait is `unsafe` because its implementation must be correct for
16
- /// the safety of `unsafe trait TrustedLen` implementations, and the results
17
- /// of using this trait can otherwise be trusted by `unsafe` code to be correct
18
- /// and fulfill the listed obligations.
19
12
#[ unstable( feature = "step_trait" , reason = "recently redesigned" , issue = "42168" ) ]
20
- pub unsafe trait Step : Clone + PartialOrd + Sized {
13
+ pub trait Step : Clone + PartialOrd + Sized {
21
14
/// Returns the number of *successor* steps required to get from `start` to `end`.
22
15
///
23
16
/// Returns `None` if the number of steps would overflow `usize`
@@ -237,7 +230,7 @@ macro_rules! step_integer_impls {
237
230
$(
238
231
#[ allow( unreachable_patterns) ]
239
232
#[ unstable( feature = "step_trait" , reason = "recently redesigned" , issue = "42168" ) ]
240
- unsafe impl Step for $u_narrower {
233
+ impl Step for $u_narrower {
241
234
step_identical_methods!( ) ;
242
235
243
236
#[ inline]
@@ -269,7 +262,7 @@ macro_rules! step_integer_impls {
269
262
270
263
#[ allow( unreachable_patterns) ]
271
264
#[ unstable( feature = "step_trait" , reason = "recently redesigned" , issue = "42168" ) ]
272
- unsafe impl Step for $i_narrower {
265
+ impl Step for $i_narrower {
273
266
step_identical_methods!( ) ;
274
267
275
268
#[ inline]
@@ -333,7 +326,7 @@ macro_rules! step_integer_impls {
333
326
$(
334
327
#[ allow( unreachable_patterns) ]
335
328
#[ unstable( feature = "step_trait" , reason = "recently redesigned" , issue = "42168" ) ]
336
- unsafe impl Step for $u_wider {
329
+ impl Step for $u_wider {
337
330
step_identical_methods!( ) ;
338
331
339
332
#[ inline]
@@ -358,7 +351,7 @@ macro_rules! step_integer_impls {
358
351
359
352
#[ allow( unreachable_patterns) ]
360
353
#[ unstable( feature = "step_trait" , reason = "recently redesigned" , issue = "42168" ) ]
361
- unsafe impl Step for $i_wider {
354
+ impl Step for $i_wider {
362
355
step_identical_methods!( ) ;
363
356
364
357
#[ inline]
@@ -408,7 +401,7 @@ step_integer_impls! {
408
401
}
409
402
410
403
#[ unstable( feature = "step_trait" , reason = "recently redesigned" , issue = "42168" ) ]
411
- unsafe impl Step for char {
404
+ impl Step for char {
412
405
#[ inline]
413
406
fn steps_between ( & start: & char , & end: & char ) -> Option < usize > {
414
407
let start = start as u32 ;
@@ -519,8 +512,8 @@ impl<A: Step> Iterator for ops::Range<A> {
519
512
#[ inline]
520
513
fn next ( & mut self ) -> Option < A > {
521
514
if self . start < self . end {
522
- // SAFETY: just checked precondition
523
- let n = unsafe { Step :: forward_unchecked ( self . start . clone ( ) , 1 ) } ;
515
+ let n =
516
+ Step :: forward_checked ( self . start . clone ( ) , 1 ) . expect ( "`Step` invariants not upheld" ) ;
524
517
Some ( mem:: replace ( & mut self . start , n) )
525
518
} else {
526
519
None
@@ -541,8 +534,8 @@ impl<A: Step> Iterator for ops::Range<A> {
541
534
fn nth ( & mut self , n : usize ) -> Option < A > {
542
535
if let Some ( plus_n) = Step :: forward_checked ( self . start . clone ( ) , n) {
543
536
if plus_n < self . end {
544
- // SAFETY: just checked precondition
545
- self . start = unsafe { Step :: forward_unchecked ( plus_n. clone ( ) , 1 ) } ;
537
+ self . start =
538
+ Step :: forward_checked ( plus_n. clone ( ) , 1 ) . expect ( "`Step` invariants not upheld" ) ;
546
539
return Some ( plus_n) ;
547
540
}
548
541
}
@@ -632,8 +625,8 @@ impl<A: Step> DoubleEndedIterator for ops::Range<A> {
632
625
#[ inline]
633
626
fn next_back ( & mut self ) -> Option < A > {
634
627
if self . start < self . end {
635
- // SAFETY: just checked precondition
636
- self . end = unsafe { Step :: backward_unchecked ( self . end . clone ( ) , 1 ) } ;
628
+ self . end =
629
+ Step :: backward_checked ( self . end . clone ( ) , 1 ) . expect ( "`Step` invariants not upheld" ) ;
637
630
Some ( self . end . clone ( ) )
638
631
} else {
639
632
None
@@ -644,8 +637,8 @@ impl<A: Step> DoubleEndedIterator for ops::Range<A> {
644
637
fn nth_back ( & mut self , n : usize ) -> Option < A > {
645
638
if let Some ( minus_n) = Step :: backward_checked ( self . end . clone ( ) , n) {
646
639
if minus_n > self . start {
647
- // SAFETY: just checked precondition
648
- self . end = unsafe { Step :: backward_unchecked ( minus_n, 1 ) } ;
640
+ self . end =
641
+ Step :: backward_checked ( minus_n, 1 ) . expect ( "`Step` invariants not upheld" ) ;
649
642
return Some ( self . end . clone ( ) ) ;
650
643
}
651
644
}
@@ -711,8 +704,8 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
711
704
}
712
705
let is_iterating = self . start < self . end ;
713
706
Some ( if is_iterating {
714
- // SAFETY: just checked precondition
715
- let n = unsafe { Step :: forward_unchecked ( self . start . clone ( ) , 1 ) } ;
707
+ let n =
708
+ Step :: forward_checked ( self . start . clone ( ) , 1 ) . expect ( "`Step` invariants not upheld" ) ;
716
709
mem:: replace ( & mut self . start , n)
717
710
} else {
718
711
self . exhausted = true ;
@@ -774,8 +767,8 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
774
767
let mut accum = init;
775
768
776
769
while self . start < self . end {
777
- // SAFETY: just checked precondition
778
- let n = unsafe { Step :: forward_unchecked ( self . start . clone ( ) , 1 ) } ;
770
+ let n =
771
+ Step :: forward_checked ( self . start . clone ( ) , 1 ) . expect ( "`Step` invariants not upheld" ) ;
779
772
let n = mem:: replace ( & mut self . start , n) ;
780
773
accum = f ( accum, n) ?;
781
774
}
@@ -828,8 +821,8 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
828
821
}
829
822
let is_iterating = self . start < self . end ;
830
823
Some ( if is_iterating {
831
- // SAFETY: just checked precondition
832
- let n = unsafe { Step :: backward_unchecked ( self . end . clone ( ) , 1 ) } ;
824
+ let n =
825
+ Step :: backward_checked ( self . end . clone ( ) , 1 ) . expect ( "`Step` invariants not upheld" ) ;
833
826
mem:: replace ( & mut self . end , n)
834
827
} else {
835
828
self . exhausted = true ;
@@ -879,8 +872,8 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
879
872
let mut accum = init;
880
873
881
874
while self . start < self . end {
882
- // SAFETY: just checked precondition
883
- let n = unsafe { Step :: backward_unchecked ( self . end . clone ( ) , 1 ) } ;
875
+ let n =
876
+ Step :: backward_checked ( self . end . clone ( ) , 1 ) . expect ( "`Step` invariants not upheld" ) ;
884
877
let n = mem:: replace ( & mut self . end , n) ;
885
878
accum = f ( accum, n) ?;
886
879
}
0 commit comments