52
52
//! something to the screen.
53
53
54
54
use clone:: Clone ;
55
- use cmp:: Ord ;
56
- use iter:: { Iterator , DoubleEndedIterator } ;
55
+ use iter:: { Step , Iterator , DoubleEndedIterator , ExactSizeIterator } ;
57
56
use kinds:: Sized ;
58
- use kinds:: Copy ;
59
57
use option:: Option :: { mod, Some , None } ;
60
58
61
59
/// 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? {
839
837
}
840
838
841
839
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
-
884
840
/// An unbounded range.
841
+ #[ deriving( Copy ) ]
885
842
#[ lang="full_range" ]
886
843
pub struct FullRange ;
887
844
888
- /// A range which i bounded at both ends.
845
+ /// A (half-open) range which is bounded at both ends.
846
+ #[ deriving( Copy ) ]
889
847
#[ lang="range" ]
890
848
pub struct Range < Idx > {
891
849
/// The lower bound of the range (inclusive).
@@ -895,13 +853,13 @@ pub struct Range<Idx> {
895
853
}
896
854
897
855
// 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 > {
900
858
#[ inline]
901
859
fn next ( & mut self ) -> Option < Idx > {
902
860
if self . start < self . end {
903
861
let result = self . start . clone ( ) ;
904
- self . start . increment ( ) ;
862
+ self . start . step ( ) ;
905
863
return Some ( result) ;
906
864
}
907
865
@@ -910,44 +868,46 @@ impl<Idx: Clone + Countable> Iterator<Idx> for Range<Idx> {
910
868
911
869
#[ inline]
912
870
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
+ }
915
876
}
916
877
}
917
878
918
- impl < Idx : Clone + Countable > DoubleEndedIterator < Idx > for Range < Idx > {
879
+ impl < Idx : Clone + Step > DoubleEndedIterator < Idx > for Range < Idx > {
919
880
#[ inline]
920
881
fn next_back ( & mut self ) -> Option < Idx > {
921
882
if self . start < self . end {
922
- self . end . decrement ( ) ;
883
+ self . end . step_back ( ) ;
923
884
return Some ( self . end . clone ( ) ) ;
924
885
}
925
886
926
887
return None ;
927
888
}
928
889
}
929
890
891
+ impl < Idx : Clone + Step > ExactSizeIterator < Idx > for Range < Idx > { }
892
+
930
893
/// A range which is only bounded below.
894
+ #[ deriving( Copy ) ]
931
895
#[ lang="range_from" ]
932
896
pub struct RangeFrom < Idx > {
933
897
/// The lower bound of the range (inclusive).
934
898
pub start : Idx ,
935
899
}
936
900
937
- impl < Idx : Clone + Countable > Iterator < Idx > for RangeFrom < Idx > {
901
+ impl < Idx : Clone + Step > Iterator < Idx > for RangeFrom < Idx > {
938
902
#[ inline]
939
903
fn next ( & mut self ) -> Option < Idx > {
940
904
// Deliberately overflow so we loop forever.
941
905
let result = self . start . clone ( ) ;
942
- self . start . increment ( ) ;
906
+ self . start . step ( ) ;
943
907
return Some ( result) ;
944
908
}
945
909
}
946
910
947
- impl < Idx : Copy > Copy for Range < Idx > { }
948
- impl < Idx : Copy > Copy for RangeFrom < Idx > { }
949
- impl Copy for FullRange { }
950
-
951
911
952
912
/// The `Deref` trait is used to specify the functionality of dereferencing
953
913
/// operations like `*v`.
0 commit comments