From 2e37b5e8fe979e719f55bbae779266b20942e920 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Wed, 14 May 2014 15:41:00 -0700 Subject: [PATCH] Make RangeIndex trait more general --- src/components/gfx/text/glyph.rs | 4 +- src/components/main/layout/inline.rs | 6 +- src/components/util/range.rs | 108 ++++++++++++++------------- 3 files changed, 63 insertions(+), 55 deletions(-) diff --git a/src/components/gfx/text/glyph.rs b/src/components/gfx/text/glyph.rs index 62bf4fb9f05..09e4c773d00 100644 --- a/src/components/gfx/text/glyph.rs +++ b/src/components/gfx/text/glyph.rs @@ -4,7 +4,7 @@ use servo_util::vec::*; use servo_util::range; -use servo_util::range::{Range, RangeIndex, EachIndex}; +use servo_util::range::{Range, RangeIndex, IntRangeIndex, EachIndex}; use servo_util::geometry::Au; use std::cmp::{Ord, Eq}; @@ -526,7 +526,7 @@ pub struct GlyphStore { is_whitespace: bool, } -range_index! { +int_range_index! { #[doc = "An index that refers to a character in a text run. This could \ point to the middle of a glyph."] struct CharIndex(int) diff --git a/src/components/main/layout/inline.rs b/src/components/main/layout/inline.rs index e9ee308a3e4..9099d5b5ce2 100644 --- a/src/components/main/layout/inline.rs +++ b/src/components/main/layout/inline.rs @@ -20,7 +20,7 @@ use gfx::font::FontMetrics; use gfx::font_context::FontContext; use servo_util::geometry::Au; use servo_util::geometry; -use servo_util::range::{Range, RangeIndex}; +use servo_util::range::{Range, RangeIndex, IntRangeIndex}; use std::iter::Enumerate; use std::fmt; use std::mem; @@ -61,7 +61,7 @@ pub struct LineBox { pub green_zone: Size2D } -range_index! { +int_range_index! { struct BoxIndex(int) } @@ -981,7 +981,7 @@ impl fmt::Show for InlineFlow { } } -range_index! { +int_range_index! { #[doc = "The index of a DOM element into the flat list of fragments."] struct FragmentIndex(int) } diff --git a/src/components/util/range.rs b/src/components/util/range.rs index 05421d9af87..13baf86d2e0 100644 --- a/src/components/util/range.rs +++ b/src/components/util/range.rs @@ -9,21 +9,26 @@ use std::num; use std::num::{Bounded, Zero}; /// An index type to be used by a `Range` -pub trait RangeIndex: Eq + Ord - + Clone - + Copy - + Zero - + TotalEq - + TotalOrd - + Add - + Sub - + Neg - + fmt::Show { +pub trait RangeIndex: Copy + + Clone + + fmt::Show + + Eq + + Ord + + TotalEq + + TotalOrd + + Add + + Sub + + Neg + + Zero {} + +pub trait IntRangeIndex: RangeIndex + Copy { fn new(x: T) -> Self; fn get(self) -> T; } -impl RangeIndex for int { +impl RangeIndex for int {} + +impl IntRangeIndex for int { #[inline] fn new(x: int) -> int { x } @@ -33,7 +38,7 @@ impl RangeIndex for int { /// Implements a range index type with operator overloads #[macro_export] -macro_rules! range_index { +macro_rules! int_range_index { ($(#[$attr:meta])* struct $Self:ident($T:ty)) => ( #[deriving(Clone, Eq, Ord, TotalEq, TotalOrd, Show, Zero)] $(#[$attr])* @@ -46,7 +51,9 @@ macro_rules! range_index { } } - impl RangeIndex<$T> for $Self { + impl RangeIndex for $Self {} + + impl IntRangeIndex<$T> for $Self { #[inline] fn new(x: $T) -> $Self { $Self(x) @@ -99,7 +106,7 @@ pub struct Range { len: I, } -impl> fmt::Show for Range { +impl fmt::Show for Range { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f.buf, "[{} .. {})", self.begin(), self.end()) } @@ -110,16 +117,14 @@ pub struct EachIndex { it: iter::Range, } -pub fn each_index>(start: I, stop: I) -> EachIndex { - EachIndex { - it: iter::range(start.get(), stop.get()) - } +pub fn each_index>(start: I, stop: I) -> EachIndex { + EachIndex { it: iter::range(start.get(), stop.get()) } } -impl> Iterator for EachIndex { +impl> Iterator for EachIndex { #[inline] fn next(&mut self) -> Option { - self.it.next().map(|i| RangeIndex::new(i)) + self.it.next().map(|i| IntRangeIndex::new(i)) } #[inline] @@ -128,7 +133,7 @@ impl> Iterator for EachIndex { } } -impl> Range { +impl Range { #[inline] pub fn new(off: I, len: I) -> Range { Range { off: off, len: len } @@ -146,39 +151,11 @@ impl> Range { #[inline] pub fn end(&self) -> I { self.off + self.len } - #[inline] - pub fn each_index(&self) -> EachIndex { - each_index(self.off, self.off + self.len) - } - #[inline] pub fn contains(&self, i: I) -> bool { i >= self.begin() && i < self.end() } - #[inline] - pub fn is_valid_for_string(&self, s: &str) -> bool { - let s_len = s.len(); - match num::cast::(s_len) { - Some(len) => { - let len = RangeIndex::new(len); - self.begin() < len - && self.end() <= len - && self.length() <= len - }, - None => { - debug!("Range::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 = RangeIndex::new(max); - val - }); - false - }, - } - } - #[inline] pub fn is_empty(&self) -> bool { self.len.is_zero() @@ -254,6 +231,37 @@ impl> Range { fail!("relation_to_range(): didn't classify self={}, other={}", self, other); } +} + +/// Methods for `Range`s with indices based on integer values +impl> Range { + #[inline] + pub fn each_index(&self) -> EachIndex { + each_index(self.off, self.off + self.len) + } + + #[inline] + pub fn is_valid_for_string(&self, s: &str) -> bool { + let s_len = s.len(); + match num::cast::(s_len) { + Some(len) => { + let len = IntRangeIndex::new(len); + self.begin() < len + && self.end() <= len + && self.length() <= len + }, + None => { + debug!("Range::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); + val + }); + false + }, + } + } #[inline] pub fn repair_after_coalesced_range(&mut self, other: &Range) { @@ -261,7 +269,7 @@ impl> Range { debug!("repair_after_coalesced_range: possibly repairing range {}", *self); debug!("repair_after_coalesced_range: relation of original range and coalesced range {}: {}", *other, relation); - let _1: I = RangeIndex::new(num::one::()); + let _1: I = IntRangeIndex::new(num::one::()); match relation { EntirelyBefore => {}, EntirelyAfter => { self.shift_by(-other.length()); },