mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
Simplify util::range::RangeIndex to always require std::num::Int,
and fix remaining warnings.
This commit is contained in:
parent
e689f9c160
commit
bf4480bb79
3 changed files with 29 additions and 44 deletions
|
@ -7,7 +7,7 @@ use self::GlyphInfo::*;
|
|||
|
||||
use servo_util::vec::*;
|
||||
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 std::cmp::PartialOrd;
|
||||
|
|
|
@ -28,7 +28,7 @@ use gfx::text::glyph::CharIndex;
|
|||
use servo_util::geometry::Au;
|
||||
use servo_util::logical_geometry::{LogicalRect, LogicalSize, WritingMode};
|
||||
use servo_util::opts;
|
||||
use servo_util::range::{IntRangeIndex, Range, RangeIndex};
|
||||
use servo_util::range::{Range, RangeIndex};
|
||||
use servo_util::arc_ptr_eq;
|
||||
use std::cmp::max;
|
||||
use std::fmt;
|
||||
|
|
|
@ -6,29 +6,15 @@ use std::cmp::{max, min};
|
|||
use std::iter;
|
||||
use std::fmt;
|
||||
use std::num;
|
||||
use std::num::{Int, Bounded, Zero};
|
||||
use std::num::Int;
|
||||
|
||||
/// An index type to be used by a `Range`
|
||||
pub trait RangeIndex: Copy
|
||||
+ Clone
|
||||
+ fmt::Show
|
||||
+ PartialEq
|
||||
+ PartialOrd
|
||||
+ Eq
|
||||
+ Ord
|
||||
+ Add<Self, Self>
|
||||
+ Sub<Self, Self>
|
||||
+ Neg<Self>
|
||||
+ Zero {}
|
||||
|
||||
pub trait IntRangeIndex<T>: RangeIndex + Copy {
|
||||
pub trait RangeIndex<T>: Int + fmt::Show {
|
||||
fn new(x: T) -> Self;
|
||||
fn get(self) -> T;
|
||||
}
|
||||
|
||||
impl RangeIndex for int {}
|
||||
|
||||
impl IntRangeIndex<int> for int {
|
||||
impl RangeIndex<int> for int {
|
||||
#[inline]
|
||||
fn new(x: int) -> int { x }
|
||||
|
||||
|
@ -40,7 +26,7 @@ impl IntRangeIndex<int> for int {
|
|||
#[macro_export]
|
||||
macro_rules! int_range_index {
|
||||
($(#[$attr:meta])* struct $Self:ident($T:ty)) => (
|
||||
#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Show, Zero)]
|
||||
#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Show)]
|
||||
$(#[$attr])*
|
||||
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 {
|
||||
fn zero() -> $Self { $Self(0) }
|
||||
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 {
|
||||
#[inline]
|
||||
fn add(&self, other: &$Self) -> $Self {
|
||||
|
@ -196,7 +181,7 @@ pub struct Range<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 {
|
||||
write!(f, "[{} .. {})", self.begin(), self.end())
|
||||
}
|
||||
|
@ -207,14 +192,14 @@ pub struct EachIndex<T, I> {
|
|||
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()) }
|
||||
}
|
||||
|
||||
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]
|
||||
fn next(&mut self) -> Option<I> {
|
||||
self.it.next().map(|i| IntRangeIndex::new(i))
|
||||
self.it.next().map(|i| RangeIndex::new(i))
|
||||
}
|
||||
|
||||
#[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
|
||||
/// denoted as `[begin, begin + length)`.
|
||||
///
|
||||
|
@ -239,7 +224,7 @@ impl<I: RangeIndex> Range<I> {
|
|||
|
||||
#[inline]
|
||||
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.
|
||||
|
@ -287,7 +272,7 @@ impl<I: RangeIndex> Range<I> {
|
|||
/// `true` if the offset from the beginning to the end of the range is zero.
|
||||
#[inline]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.length().is_zero()
|
||||
self.length() == Int::zero()
|
||||
}
|
||||
|
||||
/// 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
|
||||
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)`.
|
||||
#[inline]
|
||||
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();
|
||||
match num::cast::<uint, T>(s_len) {
|
||||
Some(len) => {
|
||||
let len = IntRangeIndex::new(len);
|
||||
let len = RangeIndex::new(len);
|
||||
self.begin() < len
|
||||
&& self.end() <= 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 \
|
||||
max value for the range index (max={})", s_len,
|
||||
{
|
||||
let max: T = Bounded::max_value();
|
||||
let val: I = IntRangeIndex::new(max);
|
||||
let max: T = Int::max_value();
|
||||
let val: I = RangeIndex::new(max);
|
||||
val
|
||||
});
|
||||
false
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue