5252//! something to the screen.
5353
5454use clone:: Clone ;
55- use cmp:: Ord ;
56- use iter:: { Iterator , DoubleEndedIterator } ;
55+ use iter:: { Step , Iterator , DoubleEndedIterator , ExactSizeIterator } ;
5756use kinds:: Sized ;
58- use kinds:: Copy ;
5957use option:: Option :: { mod, Some , None } ;
6058
6159/// The `Drop` trait is used to run some code when a value goes out of scope. This
@@ -839,53 +837,13 @@ pub trait SliceMut<Sized? Idx, Sized? Result> for Sized? {
839837}
840838
841839
842-
843- /// REVIEW could be in a better module
844- /// The `Countable` trait identifies objects which are countable, i.e., are
845- /// analogous to the natural numbers. A countable object can be incremented and
846- /// and decremented and ordered. The `difference` function provides a way to
847- /// compare two Countable objects (it could be provided using increment and Ord,
848- /// but the implementation would be so inefficient as to be useless).
849- #[ unstable = "Trait is unstable." ]
850- pub trait Countable : Ord {
851- // FIXME(#19391) needs a snapshot
852- //type T;
853-
854- /// Change self to the next object.
855- fn increment ( & mut self ) ;
856- /// Change self to the previous object.
857- fn decrement ( & mut self ) ;
858- /// The difference between two countable objects.
859- /// Temporarily a uint, should be an associated type, but
860- // FIXME(#19391) needs a snapshot
861- fn difference ( a : & Self , b : & Self ) -> uint ;
862- //fn difference(a: &Self, b: &Self) -> <Self as Countable>::T;
863- }
864-
865- macro_rules! countable_impl(
866- ( $( $t: ty) * ) => ( $(
867- #[ unstable = "Trait is unstable." ]
868- impl Countable for $t {
869- // FIXME(#19391) needs a snapshot
870- //type T = uint;
871-
872- #[ inline]
873- fn increment( & mut self ) { * self += 1 ; }
874- #[ inline]
875- fn decrement( & mut self ) { * self -= 1 ; }
876- #[ inline]
877- fn difference( a: & $t, b: & $t) -> uint { ( * a - * b) as uint }
878- }
879- ) * )
880- )
881-
882- countable_impl ! ( uint u8 u16 u32 u64 int i8 i16 i32 i64 )
883-
884840/// An unbounded range.
841+ #[ deriving( Copy ) ]
885842#[ lang="full_range" ]
886843pub struct FullRange ;
887844
888- /// A range which i bounded at both ends.
845+ /// A (half-open) range which is bounded at both ends.
846+ #[ deriving( Copy ) ]
889847#[ lang="range" ]
890848pub struct Range < Idx > {
891849 /// The lower bound of the range (inclusive).
@@ -895,13 +853,13 @@ pub struct Range<Idx> {
895853}
896854
897855// FIXME(#19391) needs a snapshot
898- //impl<Idx: Clone + Countable <T=uint>> Iterator<Idx> for Range<Idx> {
899- impl < Idx : Clone + Countable > Iterator < Idx > for Range < Idx > {
856+ //impl<Idx: Clone + Step <T=uint>> Iterator<Idx> for Range<Idx> {
857+ impl < Idx : Clone + Step > Iterator < Idx > for Range < Idx > {
900858 #[ inline]
901859 fn next ( & mut self ) -> Option < Idx > {
902860 if self . start < self . end {
903861 let result = self . start . clone ( ) ;
904- self . start . increment ( ) ;
862+ self . start . step ( ) ;
905863 return Some ( result) ;
906864 }
907865
@@ -910,44 +868,46 @@ impl<Idx: Clone + Countable> Iterator<Idx> for Range<Idx> {
910868
911869 #[ inline]
912870 fn size_hint ( & self ) -> ( uint , Option < uint > ) {
913- let hint = Countable :: difference ( & self . end , & self . start ) ;
914- ( hint, Some ( hint) )
871+ if let Some ( hint) = Step :: steps_between ( & self . end , & self . start ) {
872+ ( hint, Some ( hint) )
873+ } else {
874+ ( 0 , None )
875+ }
915876 }
916877}
917878
918- impl < Idx : Clone + Countable > DoubleEndedIterator < Idx > for Range < Idx > {
879+ impl < Idx : Clone + Step > DoubleEndedIterator < Idx > for Range < Idx > {
919880 #[ inline]
920881 fn next_back ( & mut self ) -> Option < Idx > {
921882 if self . start < self . end {
922- self . end . decrement ( ) ;
883+ self . end . step_back ( ) ;
923884 return Some ( self . end . clone ( ) ) ;
924885 }
925886
926887 return None ;
927888 }
928889}
929890
891+ impl < Idx : Clone + Step > ExactSizeIterator < Idx > for Range < Idx > { }
892+
930893/// A range which is only bounded below.
894+ #[ deriving( Copy ) ]
931895#[ lang="range_from" ]
932896pub struct RangeFrom < Idx > {
933897 /// The lower bound of the range (inclusive).
934898 pub start : Idx ,
935899}
936900
937- impl < Idx : Clone + Countable > Iterator < Idx > for RangeFrom < Idx > {
901+ impl < Idx : Clone + Step > Iterator < Idx > for RangeFrom < Idx > {
938902 #[ inline]
939903 fn next ( & mut self ) -> Option < Idx > {
940904 // Deliberately overflow so we loop forever.
941905 let result = self . start . clone ( ) ;
942- self . start . increment ( ) ;
906+ self . start . step ( ) ;
943907 return Some ( result) ;
944908 }
945909}
946910
947- impl < Idx : Copy > Copy for Range < Idx > { }
948- impl < Idx : Copy > Copy for RangeFrom < Idx > { }
949- impl Copy for FullRange { }
950-
951911
952912/// The `Deref` trait is used to specify the functionality of dereferencing
953913/// operations like `*v`.
0 commit comments