diff options
Diffstat (limited to 'components/util/range.rs')
-rw-r--r-- | components/util/range.rs | 92 |
1 files changed, 54 insertions, 38 deletions
diff --git a/components/util/range.rs b/components/util/range.rs index ef6e7e0ff47..fbf14f38400 100644 --- a/components/util/range.rs +++ b/components/util/range.rs @@ -26,7 +26,7 @@ impl RangeIndex<int> for int { #[macro_export] macro_rules! int_range_index { ($(#[$attr:meta])* struct $Self:ident($T:ty)) => ( - #[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Show, Copy)] + #[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Show, Copy)] $(#[$attr])* pub struct $Self(pub $T); @@ -74,40 +74,42 @@ macro_rules! int_range_index { } } - impl Add<$Self, $Self> for $Self { + impl Add<$Self> for $Self { + type Output = $Self; + #[inline] - fn add(&self, other: &$Self) -> $Self { + fn add(self, other: $Self) -> $Self { $Self(self.get() + other.get()) } } - impl Sub<$Self, $Self> for $Self { + impl Sub<$Self> for $Self { + type Output = $Self; + #[inline] - fn sub(&self, other: &$Self) -> $Self { + fn sub(self, other: $Self) -> $Self { $Self(self.get() - other.get()) } } - impl Mul<$Self, $Self> for $Self { + impl Mul<$Self> for $Self { + type Output = $Self; + #[inline] - fn mul(&self, other: &$Self) -> $Self { + fn mul(self, other: $Self) -> $Self { $Self(self.get() * other.get()) } } - impl Neg<$Self> for $Self { + impl Neg for $Self { + type Output = $Self; + #[inline] - fn neg(&self) -> $Self { + fn neg(self) -> $Self { $Self(-self.get()) } } - impl ::std::num::One for $Self { - fn one() -> $Self { - $Self(1) - } - } - impl ToPrimitive for $Self { fn to_i64(&self) -> Option<i64> { Some(self.get() as i64) @@ -124,66 +126,75 @@ macro_rules! int_range_index { } } - impl Div<$Self, $Self> for $Self { - fn div(&self, other: &$Self) -> $Self { + impl Div<$Self> for $Self { + type Output = $Self; + fn div(self, other: $Self) -> $Self { $Self(self.get() / other.get()) } } - impl Rem<$Self, $Self> for $Self { - fn rem(&self, other: &$Self) -> $Self { + impl Rem<$Self> for $Self { + type Output = $Self; + fn rem(self, other: $Self) -> $Self { $Self(self.get() % other.get()) } } - impl Not<$Self> for $Self { - fn not(&self) -> $Self { + impl Not for $Self { + type Output = $Self; + fn not(self) -> $Self { $Self(!self.get()) } } - impl BitAnd<$Self, $Self> for $Self { - fn bitand(&self, other: &$Self) -> $Self { + impl BitAnd<$Self> for $Self { + type Output = $Self; + fn bitand(self, other: $Self) -> $Self { $Self(self.get() & other.get()) } } - impl BitOr<$Self, $Self> for $Self { - fn bitor(&self, other: &$Self) -> $Self { + impl BitOr<$Self> for $Self { + type Output = $Self; + fn bitor(self, other: $Self) -> $Self { $Self(self.get() | other.get()) } } - impl BitXor<$Self, $Self> for $Self { - fn bitxor(&self, other: &$Self) -> $Self { + impl BitXor<$Self> for $Self { + type Output = $Self; + fn bitxor(self, other: $Self) -> $Self { $Self(self.get() ^ other.get()) } } - impl Shl<uint, $Self> for $Self { - fn shl(&self, n: &uint) -> $Self { - $Self(self.get() << *n) + impl Shl<uint> for $Self { + type Output = $Self; + fn shl(self, n: uint) -> $Self { + $Self(self.get() << n) } } - impl Shr<uint, $Self> for $Self { - fn shr(&self, n: &uint) -> $Self { - $Self(self.get() >> *n) + impl Shr<uint> for $Self { + type Output = $Self; + fn shr(self, n: uint) -> $Self { + $Self(self.get() >> n) } } ) } /// A range of indices -#[deriving(Clone, Encodable, Copy)] +#[derive(Clone, RustcEncodable, Copy)] pub struct Range<I> { begin: I, length: I, } +#[old_impl_check] 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()) + write!(f, "[{:?} .. {:?})", self.begin(), self.end()) } } @@ -196,7 +207,9 @@ 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: RangeIndex<T>> Iterator<I> for EachIndex<T, I> { +impl<T: Int, I: RangeIndex<T>> Iterator for EachIndex<T, I> { + type Item = I; + #[inline] fn next(&mut self) -> Option<I> { self.it.next().map(|i| RangeIndex::new(i)) @@ -208,6 +221,7 @@ impl<T: Int, I: RangeIndex<T>> Iterator<I> for EachIndex<T, I> { } } +#[old_impl_check] impl<I: RangeIndex<T>, T> Range<I> { /// Create a new range from beginning and length offsets. This could be /// denoted as `[begin, begin + length)`. @@ -345,6 +359,7 @@ impl<I: RangeIndex<T>, T> Range<I> { } /// Methods for `Range`s with indices based on integer values +#[old_impl_check] impl<T: Int, I: RangeIndex<T>> Range<I> { /// Returns an iterater that increments over `[begin, end)`. #[inline] @@ -363,8 +378,9 @@ impl<T: Int, I: RangeIndex<T>> Range<I> { && self.length() <= len }, None => { - debug!("Range<T>::is_valid_for_string: string length (len={}) is longer than the \ - max value for the range index (max={})", s_len, + 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 = Int::max_value(); let val: I = RangeIndex::new(max); |