Skip to content

Commit e15f043

Browse files
committed
Show, String, Eq impls for Ranges
1 parent 2a8cb67 commit e15f043

File tree

1 file changed

+84
-4
lines changed

1 file changed

+84
-4
lines changed

src/libcore/ops.rs

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use clone::Clone;
6565
use iter::{Step, Iterator,DoubleEndedIterator,ExactSizeIterator};
6666
use marker::Sized;
6767
use option::Option::{self, Some, None};
68+
use fmt;
6869

6970
/// The `Drop` trait is used to run some code when a value goes out of scope. This
7071
/// is sometimes called a 'destructor'.
@@ -847,13 +848,27 @@ pub trait IndexMut<Index: ?Sized> {
847848
}
848849

849850
/// An unbounded range.
850-
#[derive(Copy)]
851+
#[derive(Copy, PartialEq, Eq)]
851852
#[lang="full_range"]
852853
#[unstable = "API still in development"]
853854
pub struct FullRange;
854855

856+
#[unstable = "API still in development"]
857+
impl fmt::Show for FullRange {
858+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
859+
fmt::Show::fmt("..", fmt)
860+
}
861+
}
862+
863+
#[unstable = "API still in development"]
864+
impl fmt::String for FullRange {
865+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
866+
fmt::String::fmt("..", fmt)
867+
}
868+
}
869+
855870
/// A (half-open) range which is bounded at both ends.
856-
#[derive(Copy)]
871+
#[derive(Copy, PartialEq, Eq)]
857872
#[lang="range"]
858873
#[unstable = "API still in development"]
859874
pub struct Range<Idx> {
@@ -904,8 +919,29 @@ impl<Idx: Clone + Step> DoubleEndedIterator for Range<Idx> {
904919
#[unstable = "API still in development"]
905920
impl<Idx: Clone + Step> ExactSizeIterator for Range<Idx> {}
906921

922+
#[unstable = "API still in development"]
923+
impl<Idx: fmt::Show> fmt::Show for Range<Idx> {
924+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
925+
write!(fmt, "{:?}..{:?}", self.start, self.end)
926+
}
927+
}
928+
#[cfg(stage0)]
929+
#[unstable = "API still in development"]
930+
impl<Idx: fmt::String + fmt::Show> fmt::String for Range<Idx> {
931+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
932+
write!(fmt, "{}..{}", self.start, self.end)
933+
}
934+
}
935+
#[cfg(not(stage0))]
936+
#[unstable = "API still in development"]
937+
impl<Idx: fmt::String> fmt::String for Range<Idx> {
938+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
939+
write!(fmt, "{}..{}", self.start, self.end)
940+
}
941+
}
942+
907943
/// A range which is only bounded below.
908-
#[derive(Copy)]
944+
#[derive(Copy, PartialEq, Eq)]
909945
#[lang="range_from"]
910946
#[unstable = "API still in development"]
911947
pub struct RangeFrom<Idx> {
@@ -926,15 +962,59 @@ impl<Idx: Clone + Step> Iterator for RangeFrom<Idx> {
926962
}
927963
}
928964

965+
#[unstable = "API still in development"]
966+
impl<Idx: fmt::Show> fmt::Show for RangeFrom<Idx> {
967+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
968+
write!(fmt, "{:?}..", self.start)
969+
}
970+
}
971+
972+
#[cfg(stage0)]
973+
#[unstable = "API still in development"]
974+
impl<Idx: fmt::String + fmt::Show> fmt::String for RangeFrom<Idx> {
975+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
976+
write!(fmt, "{}..", self.start)
977+
}
978+
}
979+
#[cfg(not(stage0))]
980+
#[unstable = "API still in development"]
981+
impl<Idx: fmt::String> fmt::String for RangeFrom<Idx> {
982+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
983+
write!(fmt, "{}..", self.start)
984+
}
985+
}
986+
929987
/// A range which is only bounded above.
930-
#[derive(Copy)]
988+
#[derive(Copy, PartialEq, Eq)]
931989
#[lang="range_to"]
932990
#[unstable = "API still in development"]
933991
pub struct RangeTo<Idx> {
934992
/// The upper bound of the range (exclusive).
935993
pub end: Idx,
936994
}
937995

996+
#[unstable = "API still in development"]
997+
impl<Idx: fmt::Show> fmt::Show for RangeTo<Idx> {
998+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
999+
write!(fmt, "..{:?}", self.end)
1000+
}
1001+
}
1002+
1003+
#[cfg(stage0)]
1004+
#[unstable = "API still in development"]
1005+
impl<Idx: fmt::String + fmt::Show> fmt::String for RangeTo<Idx> {
1006+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1007+
write!(fmt, "..{}", self.end)
1008+
}
1009+
}
1010+
#[cfg(not(stage0))]
1011+
#[unstable = "API still in development"]
1012+
impl<Idx: fmt::String> fmt::String for RangeTo<Idx> {
1013+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1014+
write!(fmt, "..{}", self.end)
1015+
}
1016+
}
1017+
9381018

9391019
/// The `Deref` trait is used to specify the functionality of dereferencing
9401020
/// operations like `*v`.

0 commit comments

Comments
 (0)