From b77bdf1818bc6f1aaba4df68fd0f8a432e78131f Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 4 Oct 2017 09:54:28 +0200 Subject: [PATCH] Remove usage the unstable Step trait --- components/gfx/text/glyph.rs | 2 +- components/range/lib.rs | 45 +++++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/components/gfx/text/glyph.rs b/components/gfx/text/glyph.rs index ce3bad9cda2..c0e1be96da7 100644 --- a/components/gfx/text/glyph.rs +++ b/components/gfx/text/glyph.rs @@ -696,7 +696,7 @@ pub struct GlyphIterator<'a> { store: &'a GlyphStore, byte_index: ByteIndex, byte_range: Range, - glyph_range: Option>, + glyph_range: Option>, } impl<'a> GlyphIterator<'a> { diff --git a/components/range/lib.rs b/components/range/lib.rs index b2ae5ae9ce6..d125c88f817 100644 --- a/components/range/lib.rs +++ b/components/range/lib.rs @@ -2,8 +2,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#![feature(step_trait)] - #![deny(unsafe_code)] extern crate heapsize; @@ -13,8 +11,6 @@ extern crate num_traits; use std::cmp::{self, max, min}; use std::fmt; -use std::iter; -use std::marker::PhantomData; use std::ops; pub trait Int: @@ -27,6 +23,7 @@ pub trait Int: fn one() -> Self; fn max_value() -> Self; fn from_usize(n: usize) -> Option; + fn to_usize(self) -> usize; } impl Int for isize { #[inline] @@ -37,6 +34,8 @@ impl Int for isize { fn max_value() -> isize { ::std::isize::MAX } #[inline] fn from_usize(n: usize) -> Option { num_traits::NumCast::from(n) } + #[inline] + fn to_usize(self) -> usize { num_traits::NumCast::from(self).unwrap() } } impl Int for usize { #[inline] @@ -47,6 +46,8 @@ impl Int for usize { fn max_value() -> usize { ::std::usize::MAX } #[inline] fn from_usize(n: usize) -> Option { Some(n) } + #[inline] + fn to_usize(self) -> usize { self } } /// An index type to be used by a `Range` @@ -89,7 +90,7 @@ macro_rules! int_range_index { } } - impl RangeIndex for $Self_ { + impl $crate::RangeIndex for $Self_ { type Index = $T; #[inline] fn new(x: $T) -> $Self_ { @@ -111,6 +112,8 @@ macro_rules! int_range_index { fn max_value() -> $Self_ { $Self_($crate::Int::max_value()) } #[inline] fn from_usize(n: usize) -> Option<$Self_> { $crate::Int::from_usize(n).map($Self_) } + #[inline] + fn to_usize(self) -> usize { self.to_usize() } } impl ::std::ops::Add<$Self_> for $Self_ { @@ -156,27 +159,37 @@ impl fmt::Debug for Range { } /// An iterator over each index in a range -pub struct EachIndex { - it: ops::Range, - phantom: PhantomData, +pub struct EachIndex { + start: I, + stop: I, } -pub fn each_index>(start: I, stop: I) -> EachIndex { - EachIndex { it: start.get()..stop.get(), phantom: PhantomData } +pub fn each_index(start: I, stop: I) -> EachIndex { + EachIndex { start, stop } } -impl> Iterator for EachIndex -where T: Int + iter::Step, for<'a> &'a T: ops::Add<&'a T, Output = T> { +impl Iterator for EachIndex { type Item = I; #[inline] fn next(&mut self) -> Option { - self.it.next().map(RangeIndex::new) + if self.start < self.stop { + let next = self.start; + self.start = next + I::one(); + Some(next) + } else { + None + } } #[inline] fn size_hint(&self) -> (usize, Option) { - self.it.size_hint() + if self.start < self.stop { + let len = (self.stop - self.start).to_usize(); + (len, Some(len)) + } else { + (0, Some(0)) + } } } @@ -317,10 +330,10 @@ impl Range { } /// Methods for `Range`s with indices based on integer values -impl> Range { +impl Range { /// Returns an iterater that increments over `[begin, end)`. #[inline] - pub fn each_index(&self) -> EachIndex { + pub fn each_index(&self) -> EachIndex { each_index(self.begin(), self.end()) } }