]> git.lizzy.rs Git - rust.git/commitdiff
implement `Step` for `Idx` types
authorNiko Matsakis <niko@alum.mit.edu>
Sun, 22 Jul 2018 14:20:48 +0000 (17:20 +0300)
committerNiko Matsakis <niko@alum.mit.edu>
Tue, 24 Jul 2018 21:11:31 +0000 (00:11 +0300)
This way, we can iterate over a `Range<T>` where `T: Idx`

src/librustc/lib.rs
src/librustc_data_structures/indexed_vec.rs
src/librustc_mir/lib.rs

index c72952efc61447ef5f5b988bebffc9371f01057b..d709a4debd1d38bedc8d4e2f71a79a8faacfb6df 100644 (file)
@@ -69,6 +69,7 @@
 #![feature(trusted_len)]
 #![feature(vec_remove_item)]
 #![feature(catch_expr)]
+#![feature(step_trait)]
 #![feature(integer_atomics)]
 #![feature(test)]
 #![feature(in_band_lifetimes)]
index e7a75c149ccf03bdfc0ee56311a3603fe3f0ad3d..08c518d5ba634798989b80174f7debd3477e930b 100644 (file)
@@ -89,6 +89,35 @@ fn index(self) -> usize {
             }
         }
 
+        impl ::std::iter::Step for $type {
+            fn steps_between(start: &Self, end: &Self) -> Option<usize> {
+                <usize as ::std::iter::Step>::steps_between(
+                    &Idx::index(*start),
+                    &Idx::index(*end),
+                )
+            }
+
+            fn replace_one(&mut self) -> Self {
+                ::std::mem::replace(self, Self::new(1))
+            }
+
+            fn replace_zero(&mut self) -> Self {
+                ::std::mem::replace(self, Self::new(0))
+            }
+
+            fn add_one(&self) -> Self {
+                Self::new(Idx::index(*self) + 1)
+            }
+
+            fn sub_one(&self) -> Self {
+                Self::new(Idx::index(*self) - 1)
+            }
+
+            fn add_usize(&self, u: usize) -> Option<Self> {
+                Idx::index(*self).checked_add(u).map(Self::new)
+            }
+        }
+
         newtype_index!(
             @handle_debug
             @derives      [$($derives,)*]
index d58affbae75ed9ffb5199b4c83734dc7e4ff69c8..6483b9b3c68f0676fcf2616be67352ab8472f171 100644 (file)
@@ -36,6 +36,7 @@
 #![feature(specialization)]
 #![feature(try_trait)]
 #![feature(unicode_internals)]
+#![feature(step_trait)]
 
 #![recursion_limit="256"]