Skip to content

Commit a54401e

Browse files
committed
implement Step for Idx types
This way, we can iterate over a `Range<T>` where `T: Idx`
1 parent f498e4e commit a54401e

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
#![feature(trusted_len)]
7070
#![feature(vec_remove_item)]
7171
#![feature(catch_expr)]
72+
#![feature(step_trait)]
7273
#![feature(integer_atomics)]
7374
#![feature(test)]
7475
#![feature(in_band_lifetimes)]

src/librustc_data_structures/indexed_vec.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,35 @@ macro_rules! newtype_index {
8989
}
9090
}
9191

92+
impl ::std::iter::Step for $type {
93+
fn steps_between(start: &Self, end: &Self) -> Option<usize> {
94+
<usize as ::std::iter::Step>::steps_between(
95+
&Idx::index(*start),
96+
&Idx::index(*end),
97+
)
98+
}
99+
100+
fn replace_one(&mut self) -> Self {
101+
::std::mem::replace(self, Self::new(1))
102+
}
103+
104+
fn replace_zero(&mut self) -> Self {
105+
::std::mem::replace(self, Self::new(0))
106+
}
107+
108+
fn add_one(&self) -> Self {
109+
Self::new(Idx::index(*self) + 1)
110+
}
111+
112+
fn sub_one(&self) -> Self {
113+
Self::new(Idx::index(*self) - 1)
114+
}
115+
116+
fn add_usize(&self, u: usize) -> Option<Self> {
117+
Idx::index(*self).checked_add(u).map(Self::new)
118+
}
119+
}
120+
92121
newtype_index!(
93122
@handle_debug
94123
@derives [$($derives,)*]

src/librustc_mir/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
3636
#![feature(specialization)]
3737
#![feature(try_trait)]
3838
#![feature(unicode_internals)]
39+
#![feature(step_trait)]
3940

4041
#![recursion_limit="256"]
4142

0 commit comments

Comments
 (0)