Skip to content

Commit 5334226

Browse files
committed
Rename Step::{add,sub}_usize to Step::forward and Step::backward
1 parent 3e8a762 commit 5334226

File tree

2 files changed

+65
-45
lines changed

2 files changed

+65
-45
lines changed

src/libcore/iter/range.rs

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,42 @@ use usize;
1515

1616
use super::{FusedIterator, TrustedLen};
1717

18-
/// Objects that can be stepped over in both directions.
19-
///
20-
/// The `steps_between` function provides a way to efficiently compare
21-
/// two `Step` objects.
18+
/// Objects that have a notion of *successor* and *predecessor*
19+
/// for the purpose of range iterators.
2220
#[unstable(feature = "step_trait",
23-
reason = "likely to be replaced by finer-grained traits",
21+
reason = "recently redesigned",
2422
issue = "42168")]
2523
pub trait Step: Clone + PartialOrd + Sized {
26-
/// Returns the number of steps between two step objects. The count is
27-
/// inclusive of `start` and exclusive of `end`.
24+
/// Returns the number of *successor* steps needed to get from `start` to `end`.
2825
///
29-
/// Returns `None` if it is not possible to calculate `steps_between`
30-
/// without overflow.
26+
/// Returns `None` if that number would overflow `usize`
27+
/// (or is infinite, if `end` would never be reached).
28+
/// Returns `Some(0)` if `start` comes after (is greater than) or equals `end`.
3129
fn steps_between(start: &Self, end: &Self) -> Option<usize>;
3230

33-
/// Add an usize, returning None on overflow
34-
fn add_usize(&self, n: usize) -> Option<Self>;
31+
/// Returns the value that would be obtained by taking the *successor* of `self`,
32+
/// `step_count` times.
33+
///
34+
/// Returns `None` if this would overflow the range of values supported by the type `Self`.
35+
///
36+
/// Note: `step_count == 1` is a common case,
37+
/// used for example in `Iterator::next` for ranges.
38+
fn forward(&self, step_count: usize) -> Option<Self>;
3539

36-
/// Subtracts an usize, returning None on overflow
37-
fn sub_usize(&self, n: usize) -> Option<Self>;
40+
/// Returns the value that would be obtained by taking the *predecessor* of `self`,
41+
/// `step_count` times.
42+
///
43+
/// Returns `None` if this would overflow the range of values supported by the type `Self`.
44+
///
45+
/// Note: `step_count == 1` is a common case,
46+
/// used for example in `Iterator::next_back` for ranges.
47+
fn backward(&self, step_count: usize) -> Option<Self>;
3848
}
3949

4050
macro_rules! step_impl_unsigned {
4151
($($t:ty)*) => ($(
4252
#[unstable(feature = "step_trait",
43-
reason = "likely to be replaced by finer-grained traits",
53+
reason = "recently redesigned",
4454
issue = "42168")]
4555
impl Step for $t {
4656
#[inline]
@@ -55,15 +65,15 @@ macro_rules! step_impl_unsigned {
5565
}
5666

5767
#[inline]
58-
fn add_usize(&self, n: usize) -> Option<Self> {
68+
fn forward(&self, n: usize) -> Option<Self> {
5969
match <$t>::try_from(n) {
6070
Ok(n_as_t) => self.checked_add(n_as_t),
6171
Err(_) => None,
6272
}
6373
}
6474

6575
#[inline]
66-
fn sub_usize(&self, n: usize) -> Option<Self> {
76+
fn backward(&self, n: usize) -> Option<Self> {
6777
match <$t>::try_from(n) {
6878
Ok(n_as_t) => self.checked_sub(n_as_t),
6979
Err(_) => None,
@@ -75,7 +85,7 @@ macro_rules! step_impl_unsigned {
7585
macro_rules! step_impl_signed {
7686
($( [$t:ty : $unsigned:ty] )*) => ($(
7787
#[unstable(feature = "step_trait",
78-
reason = "likely to be replaced by finer-grained traits",
88+
reason = "recently redesigned",
7989
issue = "42168")]
8090
impl Step for $t {
8191
#[inline]
@@ -92,11 +102,11 @@ macro_rules! step_impl_signed {
92102
}
93103

94104
#[inline]
95-
fn add_usize(&self, n: usize) -> Option<Self> {
105+
fn forward(&self, n: usize) -> Option<Self> {
96106
match <$unsigned>::try_from(n) {
97107
Ok(n_as_unsigned) => {
98108
// Wrapping in unsigned space handles cases like
99-
// `-120_i8.add_usize(200) == Some(80_i8)`,
109+
// `-120_i8.forward(200) == Some(80_i8)`,
100110
// even though 200_usize is out of range for i8.
101111
let wrapped = (*self as $unsigned).wrapping_add(n_as_unsigned) as $t;
102112
if wrapped >= *self {
@@ -109,11 +119,11 @@ macro_rules! step_impl_signed {
109119
}
110120
}
111121
#[inline]
112-
fn sub_usize(&self, n: usize) -> Option<Self> {
122+
fn backward(&self, n: usize) -> Option<Self> {
113123
match <$unsigned>::try_from(n) {
114124
Ok(n_as_unsigned) => {
115125
// Wrapping in unsigned space handles cases like
116-
// `-120_i8.add_usize(200) == Some(80_i8)`,
126+
// `-120_i8.forward(200) == Some(80_i8)`,
117127
// even though 200_usize is out of range for i8.
118128
let wrapped = (*self as $unsigned).wrapping_sub(n_as_unsigned) as $t;
119129
if wrapped <= *self {
@@ -132,7 +142,7 @@ macro_rules! step_impl_signed {
132142
macro_rules! step_impl_no_between {
133143
($($t:ty)*) => ($(
134144
#[unstable(feature = "step_trait",
135-
reason = "likely to be replaced by finer-grained traits",
145+
reason = "recently redesigned",
136146
issue = "42168")]
137147
impl Step for $t {
138148
#[inline]
@@ -141,12 +151,12 @@ macro_rules! step_impl_no_between {
141151
}
142152

143153
#[inline]
144-
fn add_usize(&self, n: usize) -> Option<Self> {
154+
fn forward(&self, n: usize) -> Option<Self> {
145155
self.checked_add(n as $t)
146156
}
147157

148158
#[inline]
149-
fn sub_usize(&self, n: usize) -> Option<Self> {
159+
fn backward(&self, n: usize) -> Option<Self> {
150160
self.checked_sub(n as $t)
151161
}
152162
}
@@ -205,7 +215,7 @@ impl<A: Step> Iterator for ops::Range<A> {
205215
fn next(&mut self) -> Option<A> {
206216
if self.start < self.end {
207217
// `start + 1` should not overflow since `end` exists such that `start < end`
208-
let mut n = self.start.add_usize(1).expect("overflow in Range::next");
218+
let mut n = self.start.forward(1).expect("overflow in Range::next");
209219
mem::swap(&mut n, &mut self.start);
210220
Some(n)
211221
} else {
@@ -223,10 +233,10 @@ impl<A: Step> Iterator for ops::Range<A> {
223233

224234
#[inline]
225235
fn nth(&mut self, n: usize) -> Option<A> {
226-
if let Some(plus_n) = self.start.add_usize(n) {
236+
if let Some(plus_n) = self.start.forward(n) {
227237
if plus_n < self.end {
228238
// `plus_n + 1` should not overflow since `end` exists such that `plus_n < end`
229-
self.start = plus_n.add_usize(1).expect("overflow in Range::nth");
239+
self.start = plus_n.forward(1).expect("overflow in Range::nth");
230240
return Some(plus_n)
231241
}
232242
}
@@ -256,7 +266,7 @@ impl<A: Step> DoubleEndedIterator for ops::Range<A> {
256266
fn next_back(&mut self) -> Option<A> {
257267
if self.start < self.end {
258268
// `end - 1` should not overflow since `start` exists such that `start < end`
259-
self.end = self.end.sub_usize(1).expect("overflow in Range::nth_back");
269+
self.end = self.end.backward(1).expect("overflow in Range::nth_back");
260270
Some(self.end.clone())
261271
} else {
262272
None
@@ -274,7 +284,7 @@ impl<A: Step> Iterator for ops::RangeFrom<A> {
274284
#[inline]
275285
fn next(&mut self) -> Option<A> {
276286
// Overflow can happen here. Panic when it does.
277-
let mut n = self.start.add_usize(1).expect("overflow in RangeFrom::next");
287+
let mut n = self.start.forward(1).expect("overflow in RangeFrom::next");
278288
mem::swap(&mut n, &mut self.start);
279289
Some(n)
280290
}
@@ -287,8 +297,8 @@ impl<A: Step> Iterator for ops::RangeFrom<A> {
287297
#[inline]
288298
fn nth(&mut self, n: usize) -> Option<A> {
289299
// Overflow can happen here. Panic when it does.
290-
let plus_n = self.start.add_usize(n).expect("overflow in RangeFrom::nth");
291-
self.start = plus_n.add_usize(1).expect("overflow in RangeFrom::nth");
300+
let plus_n = self.start.forward(n).expect("overflow in RangeFrom::nth");
301+
self.start = plus_n.forward(1).expect("overflow in RangeFrom::nth");
292302
Some(plus_n)
293303
}
294304
}
@@ -307,18 +317,18 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
307317
match self.start.partial_cmp(&self.end) {
308318
Some(Less) => {
309319
// `start + 1` should not overflow since `end` exists such that `start < end`
310-
let n = self.start.add_usize(1).expect("overflow in RangeInclusive::next");
320+
let n = self.start.forward(1).expect("overflow in RangeInclusive::next");
311321
Some(mem::replace(&mut self.start, n))
312322
},
313323
Some(Equal) => {
314324
let last;
315-
if let Some(end_plus_one) = self.end.add_usize(1) {
325+
if let Some(end_plus_one) = self.end.forward(1) {
316326
last = mem::replace(&mut self.start, end_plus_one);
317327
} else {
318328
last = self.start.clone();
319329
// `start == end`, and `end + 1` underflowed.
320330
// `start - 1` overflowing would imply a type with only one valid value?
321-
self.end = self.start.sub_usize(1).expect("overflow in RangeInclusive::next");
331+
self.end = self.start.backward(1).expect("overflow in RangeInclusive::next");
322332
}
323333
Some(last)
324334
},
@@ -340,35 +350,35 @@ impl<A: Step> Iterator for ops::RangeInclusive<A> {
340350

341351
#[inline]
342352
fn nth(&mut self, n: usize) -> Option<A> {
343-
if let Some(plus_n) = self.start.add_usize(n) {
353+
if let Some(plus_n) = self.start.forward(n) {
344354
use cmp::Ordering::*;
345355

346356
match plus_n.partial_cmp(&self.end) {
347357
Some(Less) => {
348358
// `plus_n + 1` should not overflow since `end` exists such that `plus_n < end`
349-
self.start = plus_n.add_usize(1).expect("overflow in RangeInclusive::nth");
359+
self.start = plus_n.forward(1).expect("overflow in RangeInclusive::nth");
350360
return Some(plus_n)
351361
}
352362
Some(Equal) => {
353-
if let Some(end_plus_one) = self.end.add_usize(1) {
363+
if let Some(end_plus_one) = self.end.forward(1) {
354364
self.start = end_plus_one
355365
} else {
356366
// `start == end`, and `end + 1` underflowed.
357367
// `start - 1` overflowing would imply a type with only one valid value?
358-
self.end = self.start.sub_usize(1).expect("overflow in RangeInclusive::nth")
368+
self.end = self.start.backward(1).expect("overflow in RangeInclusive::nth")
359369
}
360370
return Some(plus_n)
361371
}
362372
_ => {}
363373
}
364374
}
365375

366-
if let Some(end_plus_one) = self.end.add_usize(1) {
376+
if let Some(end_plus_one) = self.end.forward(1) {
367377
self.start = end_plus_one
368378
} else {
369379
// `start == end`, and `end + 1` underflowed.
370380
// `start - 1` overflowing would imply a type with only one valid value?
371-
self.end = self.start.sub_usize(1).expect("overflow in RangeInclusive::nth")
381+
self.end = self.start.backward(1).expect("overflow in RangeInclusive::nth")
372382
}
373383
None
374384
}
@@ -383,18 +393,18 @@ impl<A: Step> DoubleEndedIterator for ops::RangeInclusive<A> {
383393
match self.start.partial_cmp(&self.end) {
384394
Some(Less) => {
385395
// `end - 1` should not overflow since `start` exists such that `start < end`
386-
let n = self.end.sub_usize(1).expect("overflow in RangeInclusive::next_back");
396+
let n = self.end.backward(1).expect("overflow in RangeInclusive::next_back");
387397
Some(mem::replace(&mut self.end, n))
388398
},
389399
Some(Equal) => {
390400
let last;
391-
if let Some(start_minus_one) = self.start.sub_usize(1) {
401+
if let Some(start_minus_one) = self.start.backward(1) {
392402
last = mem::replace(&mut self.end, start_minus_one);
393403
} else {
394404
last = self.end.clone();
395405
// `start == end`, and `start - 1` underflowed.
396406
// `end + 1` overflowing would imply a type with only one valid value?
397-
self.start = self.start.add_usize(1).expect("overflow in RangeInclusive::next_back");
407+
self.start = self.start.forward(1).expect("overflow in RangeInclusive::next_back");
398408
}
399409
Some(last)
400410
},

src/libcore/tests/iter.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,16 @@ fn test_chain_fold() {
12841284
}
12851285

12861286
#[test]
1287-
fn test_step_add_usize() {
1288-
assert_eq!((-120_i8).add_usize(200), Some(80));
1287+
fn test_steps_between() {
1288+
assert_eq!(Step::steps_between(&-120_i8, &80_i8), Some(200_usize));
1289+
}
1290+
1291+
#[test]
1292+
fn test_step_forward() {
1293+
assert_eq!((-120_i8).forward(200_usize), Some(80_i8));
1294+
}
1295+
1296+
#[test]
1297+
fn test_step_backward() {
1298+
assert_eq!((120_i8).backward(200_usize), Some(-80_i8));
12891299
}

0 commit comments

Comments
 (0)