Remove usage the unstable Step trait

This commit is contained in:
Simon Sapin 2017-10-04 09:54:28 +02:00
parent 77afc3f33a
commit b77bdf1818
2 changed files with 30 additions and 17 deletions

View file

@ -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<Self>;
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<isize> { 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<usize> { 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<I: RangeIndex> fmt::Debug for Range<I> {
}
/// An iterator over each index in a range
pub struct EachIndex<T, I> {
it: ops::Range<T>,
phantom: PhantomData<I>,
pub struct EachIndex<I: RangeIndex> {
start: I,
stop: I,
}
pub fn each_index<T: Int, I: RangeIndex<Index=T>>(start: I, stop: I) -> EachIndex<T, I> {
EachIndex { it: start.get()..stop.get(), phantom: PhantomData }
pub fn each_index<I: RangeIndex>(start: I, stop: I) -> EachIndex<I> {
EachIndex { start, stop }
}
impl<T: Int, I: RangeIndex<Index=T>> Iterator for EachIndex<T, I>
where T: Int + iter::Step, for<'a> &'a T: ops::Add<&'a T, Output = T> {
impl<I: RangeIndex> Iterator for EachIndex<I> {
type Item = I;
#[inline]
fn next(&mut self) -> Option<I> {
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<usize>) {
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<I: RangeIndex> Range<I> {
}
/// Methods for `Range`s with indices based on integer values
impl<T: Int, I: RangeIndex<Index=T>> Range<I> {
impl<I: RangeIndex> Range<I> {
/// Returns an iterater that increments over `[begin, end)`.
#[inline]
pub fn each_index(&self) -> EachIndex<T, I> {
pub fn each_index(&self) -> EachIndex<I> {
each_index(self.begin(), self.end())
}
}