Simplify util::range::RangeIndex to always require std::num::Int,

and fix remaining warnings.
This commit is contained in:
Simon Sapin 2014-12-18 11:34:23 +00:00
parent e689f9c160
commit bf4480bb79
3 changed files with 29 additions and 44 deletions

View file

@ -7,7 +7,7 @@ use self::GlyphInfo::*;
use servo_util::vec::*; use servo_util::vec::*;
use servo_util::range; use servo_util::range;
use servo_util::range::{Range, RangeIndex, IntRangeIndex, EachIndex}; use servo_util::range::{Range, RangeIndex, EachIndex};
use servo_util::geometry::Au; use servo_util::geometry::Au;
use std::cmp::PartialOrd; use std::cmp::PartialOrd;

View file

@ -28,7 +28,7 @@ use gfx::text::glyph::CharIndex;
use servo_util::geometry::Au; use servo_util::geometry::Au;
use servo_util::logical_geometry::{LogicalRect, LogicalSize, WritingMode}; use servo_util::logical_geometry::{LogicalRect, LogicalSize, WritingMode};
use servo_util::opts; use servo_util::opts;
use servo_util::range::{IntRangeIndex, Range, RangeIndex}; use servo_util::range::{Range, RangeIndex};
use servo_util::arc_ptr_eq; use servo_util::arc_ptr_eq;
use std::cmp::max; use std::cmp::max;
use std::fmt; use std::fmt;

View file

@ -6,29 +6,15 @@ use std::cmp::{max, min};
use std::iter; use std::iter;
use std::fmt; use std::fmt;
use std::num; use std::num;
use std::num::{Int, Bounded, Zero}; use std::num::Int;
/// An index type to be used by a `Range` /// An index type to be used by a `Range`
pub trait RangeIndex: Copy pub trait RangeIndex<T>: Int + fmt::Show {
+ Clone
+ fmt::Show
+ PartialEq
+ PartialOrd
+ Eq
+ Ord
+ Add<Self, Self>
+ Sub<Self, Self>
+ Neg<Self>
+ Zero {}
pub trait IntRangeIndex<T>: RangeIndex + Copy {
fn new(x: T) -> Self; fn new(x: T) -> Self;
fn get(self) -> T; fn get(self) -> T;
} }
impl RangeIndex for int {} impl RangeIndex<int> for int {
impl IntRangeIndex<int> for int {
#[inline] #[inline]
fn new(x: int) -> int { x } fn new(x: int) -> int { x }
@ -40,7 +26,7 @@ impl IntRangeIndex<int> for int {
#[macro_export] #[macro_export]
macro_rules! int_range_index { macro_rules! int_range_index {
($(#[$attr:meta])* struct $Self:ident($T:ty)) => ( ($(#[$attr:meta])* struct $Self:ident($T:ty)) => (
#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Show, Zero)] #[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Show)]
$(#[$attr])* $(#[$attr])*
pub struct $Self(pub $T); pub struct $Self(pub $T);
@ -51,7 +37,18 @@ macro_rules! int_range_index {
} }
} }
impl RangeIndex for $Self {} impl RangeIndex<$T> for $Self {
#[inline]
fn new(x: $T) -> $Self {
$Self(x)
}
#[inline]
fn get(self) -> $T {
match self { $Self(x) => x }
}
}
impl ::std::num::Int for $Self { impl ::std::num::Int for $Self {
fn zero() -> $Self { $Self(0) } fn zero() -> $Self { $Self(0) }
fn one() -> $Self { $Self(1) } fn one() -> $Self { $Self(1) }
@ -77,18 +74,6 @@ macro_rules! int_range_index {
} }
} }
impl IntRangeIndex<$T> for $Self {
#[inline]
fn new(x: $T) -> $Self {
$Self(x)
}
#[inline]
fn get(self) -> $T {
match self { $Self(x) => x }
}
}
impl Add<$Self, $Self> for $Self { impl Add<$Self, $Self> for $Self {
#[inline] #[inline]
fn add(&self, other: &$Self) -> $Self { fn add(&self, other: &$Self) -> $Self {
@ -196,7 +181,7 @@ pub struct Range<I> {
length: I, length: I,
} }
impl<I: RangeIndex> fmt::Show for Range<I> { impl<I: RangeIndex<T>, T> fmt::Show for Range<I> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{} .. {})", self.begin(), self.end()) write!(f, "[{} .. {})", self.begin(), self.end())
} }
@ -207,14 +192,14 @@ pub struct EachIndex<T, I> {
it: iter::Range<T>, it: iter::Range<T>,
} }
pub fn each_index<T: Int, I: IntRangeIndex<T>>(start: I, stop: I) -> EachIndex<T, I> { pub fn each_index<T: Int, I: RangeIndex<T>>(start: I, stop: I) -> EachIndex<T, I> {
EachIndex { it: iter::range(start.get(), stop.get()) } EachIndex { it: iter::range(start.get(), stop.get()) }
} }
impl<T: Int, I: IntRangeIndex<T>> Iterator<I> for EachIndex<T, I> { impl<T: Int, I: RangeIndex<T>> Iterator<I> for EachIndex<T, I> {
#[inline] #[inline]
fn next(&mut self) -> Option<I> { fn next(&mut self) -> Option<I> {
self.it.next().map(|i| IntRangeIndex::new(i)) self.it.next().map(|i| RangeIndex::new(i))
} }
#[inline] #[inline]
@ -223,7 +208,7 @@ impl<T: Int, I: IntRangeIndex<T>> Iterator<I> for EachIndex<T, I> {
} }
} }
impl<I: RangeIndex> Range<I> { impl<I: RangeIndex<T>, T> Range<I> {
/// Create a new range from beginning and length offsets. This could be /// Create a new range from beginning and length offsets. This could be
/// denoted as `[begin, begin + length)`. /// denoted as `[begin, begin + length)`.
/// ///
@ -239,7 +224,7 @@ impl<I: RangeIndex> Range<I> {
#[inline] #[inline]
pub fn empty() -> Range<I> { pub fn empty() -> Range<I> {
Range::new(num::zero(), num::zero()) Range::new(Int::zero(), Int::zero())
} }
/// The index offset to the beginning of the range. /// The index offset to the beginning of the range.
@ -287,7 +272,7 @@ impl<I: RangeIndex> Range<I> {
/// `true` if the offset from the beginning to the end of the range is zero. /// `true` if the offset from the beginning to the end of the range is zero.
#[inline] #[inline]
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.length().is_zero() self.length() == Int::zero()
} }
/// Shift the entire range by the supplied index delta. /// Shift the entire range by the supplied index delta.
@ -360,7 +345,7 @@ impl<I: RangeIndex> Range<I> {
} }
/// Methods for `Range`s with indices based on integer values /// Methods for `Range`s with indices based on integer values
impl<T: Int+Bounded, I: IntRangeIndex<T>> Range<I> { impl<T: Int, I: RangeIndex<T>> Range<I> {
/// Returns an iterater that increments over `[begin, end)`. /// Returns an iterater that increments over `[begin, end)`.
#[inline] #[inline]
pub fn each_index(&self) -> EachIndex<T, I> { pub fn each_index(&self) -> EachIndex<T, I> {
@ -372,7 +357,7 @@ impl<T: Int+Bounded, I: IntRangeIndex<T>> Range<I> {
let s_len = s.len(); let s_len = s.len();
match num::cast::<uint, T>(s_len) { match num::cast::<uint, T>(s_len) {
Some(len) => { Some(len) => {
let len = IntRangeIndex::new(len); let len = RangeIndex::new(len);
self.begin() < len self.begin() < len
&& self.end() <= len && self.end() <= len
&& self.length() <= len && self.length() <= len
@ -381,8 +366,8 @@ impl<T: Int+Bounded, I: IntRangeIndex<T>> Range<I> {
debug!("Range<T>::is_valid_for_string: string length (len={}) is longer than the \ debug!("Range<T>::is_valid_for_string: string length (len={}) is longer than the \
max value for the range index (max={})", s_len, max value for the range index (max={})", s_len,
{ {
let max: T = Bounded::max_value(); let max: T = Int::max_value();
let val: I = IntRangeIndex::new(max); let val: I = RangeIndex::new(max);
val val
}); });
false false