Skip to content

Commit 5eb5796

Browse files
committed
Impls using the new scheme for slicing
1 parent 81618d7 commit 5eb5796

File tree

6 files changed

+197
-209
lines changed

6 files changed

+197
-209
lines changed

src/libcollections/string.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -818,25 +818,31 @@ impl<'a> Add<&'a str> for String {
818818
}
819819
}
820820

821-
impl ops::Slice<uint, str> for String {
821+
impl<T> ops::Index<ops::Range<uint>, str> for String {
822822
#[inline]
823-
fn as_slice_<'a>(&'a self) -> &'a str {
824-
unsafe { mem::transmute(self.vec.as_slice()) }
823+
fn index(&self, &index: &ops::Range<uint>) -> &str {
824+
self[][*index]
825825
}
826+
}
826827

828+
impl<T> ops::Index<ops::RangeTo<uint>, str> for String {
827829
#[inline]
828-
fn slice_from_or_fail<'a>(&'a self, from: &uint) -> &'a str {
829-
self[][*from..]
830+
fn index(&self, &index: &ops::RangeTo<uint>) -> &str {
831+
self[][*index]
830832
}
833+
}
831834

835+
impl<T> ops::Index<ops::RangeFrom<uint>, str> for String {
832836
#[inline]
833-
fn slice_to_or_fail<'a>(&'a self, to: &uint) -> &'a str {
834-
self[][..*to]
837+
fn index(&self, &index: &ops::RangeFrom<uint>) -> &str {
838+
self[][*index]
835839
}
840+
}
836841

842+
impl<T> ops::Index<ops::FullRange<uint>, str> for String {
837843
#[inline]
838-
fn slice_or_fail<'a>(&'a self, from: &uint, to: &uint) -> &'a str {
839-
self[][*from..*to]
844+
fn index(&self, &index: &ops::FullRange<uint>) -> &str {
845+
unsafe { mem::transmute(self.vec.as_slice()) }
840846
}
841847
}
842848

src/libcollections/vec.rs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,43 +1211,64 @@ impl<T> IndexMut<uint> for Vec<T> {
12111211

12121212
impl<T> ops::Slice<uint, [T]> for Vec<T> {
12131213
#[inline]
1214-
fn as_slice_<'a>(&'a self) -> &'a [T] {
1215-
self.as_slice()
1214+
fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut T {
1215+
&mut self.as_mut_slice()[*index]
12161216
}
1217+
}
12171218

1219+
impl<T> ops::Index<ops::Range<uint>, [T]> for Vec<T> {
12181220
#[inline]
1219-
fn slice_from_or_fail<'a>(&'a self, start: &uint) -> &'a [T] {
1220-
self.as_slice().slice_from_or_fail(start)
1221+
fn index(&self, &index: &ops::Range<uint>) -> &[T] {
1222+
self.as_slice().index(index)
12211223
}
1224+
}
12221225

1226+
impl<T> ops::Index<ops::RangeTo<uint>, [T]> for Vec<T> {
12231227
#[inline]
1224-
fn slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [T] {
1225-
self.as_slice().slice_to_or_fail(end)
1228+
fn index(&self, &index: &ops::RangeTo<uint>) -> &[T] {
1229+
self.as_slice().index(index)
12261230
}
1231+
}
1232+
1233+
impl<T> ops::Index<ops::RangeFrom<uint>, [T]> for Vec<T> {
12271234
#[inline]
1228-
fn slice_or_fail<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] {
1229-
self.as_slice().slice_or_fail(start, end)
1235+
fn index(&self, &index: &ops::RangeFrom<uint>) -> &[T] {
1236+
self.as_slice().index(index)
12301237
}
12311238
}
12321239

1233-
impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
1240+
impl<T> ops::Index<ops::FullRange<uint>, [T]> for Vec<T> {
12341241
#[inline]
1235-
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
1236-
self.as_mut_slice()
1242+
fn index(&self, &index: &ops::FullRange<uint>) -> &[T] {
1243+
self.as_slice()
12371244
}
1245+
}
12381246

1247+
impl<T> ops::IndexMut<ops::Range<uint>, [T]> for Vec<T> {
12391248
#[inline]
1240-
fn slice_from_or_fail_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] {
1241-
self.as_mut_slice().slice_from_or_fail_mut(start)
1249+
fn index_mut(&mut self, &index: &ops::Range<uint>) -> &mut [T] {
1250+
self.as_mut_slice().index_mut(index)
12421251
}
1252+
}
12431253

1254+
impl<T> ops::IndexMut<ops::RangeTo<uint>, [T]> for Vec<T> {
12441255
#[inline]
1245-
fn slice_to_or_fail_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] {
1246-
self.as_mut_slice().slice_to_or_fail_mut(end)
1256+
fn index_mut(&mut self, &index: &ops::RangeTo<uint>) -> &mut [T] {
1257+
self.as_mut_slice().index_mut(index)
12471258
}
1259+
}
1260+
1261+
impl<T> ops::IndexMut<ops::RangeFrom<uint>, [T]> for Vec<T> {
12481262
#[inline]
1249-
fn slice_or_fail_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] {
1250-
self.as_mut_slice().slice_or_fail_mut(start, end)
1263+
fn index_mut(&mut self, &index: &ops::RangeFrom<uint>) -> &mut [T] {
1264+
self.as_mut_slice().index_mut(index)
1265+
}
1266+
}
1267+
1268+
impl<T> ops::IndexMut<ops::FullRange<uint>, [T]> for Vec<T> {
1269+
#[inline]
1270+
fn index_mut(&mut self, &index: &ops::FullRange<uint>) -> &mut [T] {
1271+
self.as_mut_slice()
12511272
}
12521273
}
12531274

src/libcore/ops.rs

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -846,105 +846,6 @@ pub trait IndexMut<Index: ?Sized> {
846846
fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Self::Output;
847847
}
848848

849-
/// The `Slice` trait is used to specify the functionality of slicing operations
850-
/// like `arr[from..to]` when used in an immutable context.
851-
///
852-
/// # Example
853-
///
854-
/// A trivial implementation of `Slice`. When `Foo[..Foo]` happens, it ends up
855-
/// calling `slice_to`, and therefore, `main` prints `Slicing!`.
856-
///
857-
/// ```ignore
858-
/// use std::ops::Slice;
859-
///
860-
/// #[derive(Copy)]
861-
/// struct Foo;
862-
///
863-
/// impl Slice<Foo, Foo> for Foo {
864-
/// fn as_slice_<'a>(&'a self) -> &'a Foo {
865-
/// println!("Slicing!");
866-
/// self
867-
/// }
868-
/// fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
869-
/// println!("Slicing!");
870-
/// self
871-
/// }
872-
/// fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
873-
/// println!("Slicing!");
874-
/// self
875-
/// }
876-
/// fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
877-
/// println!("Slicing!");
878-
/// self
879-
/// }
880-
/// }
881-
///
882-
/// fn main() {
883-
/// Foo[..Foo];
884-
/// }
885-
/// ```
886-
#[lang="slice"]
887-
pub trait Slice<Idx: ?Sized, Result: ?Sized> {
888-
/// The method for the slicing operation foo[]
889-
fn as_slice_<'a>(&'a self) -> &'a Result;
890-
/// The method for the slicing operation foo[from..]
891-
fn slice_from_or_fail<'a>(&'a self, from: &Idx) -> &'a Result;
892-
/// The method for the slicing operation foo[..to]
893-
fn slice_to_or_fail<'a>(&'a self, to: &Idx) -> &'a Result;
894-
/// The method for the slicing operation foo[from..to]
895-
fn slice_or_fail<'a>(&'a self, from: &Idx, to: &Idx) -> &'a Result;
896-
}
897-
898-
/// The `SliceMut` trait is used to specify the functionality of slicing
899-
/// operations like `arr[from..to]`, when used in a mutable context.
900-
///
901-
/// # Example
902-
///
903-
/// A trivial implementation of `SliceMut`. When `Foo[Foo..]` happens, it ends up
904-
/// calling `slice_from_mut`, and therefore, `main` prints `Slicing!`.
905-
///
906-
/// ```ignore
907-
/// use std::ops::SliceMut;
908-
///
909-
/// #[derive(Copy)]
910-
/// struct Foo;
911-
///
912-
/// impl SliceMut<Foo, Foo> for Foo {
913-
/// fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
914-
/// println!("Slicing!");
915-
/// self
916-
/// }
917-
/// fn slice_from_or_fail_mut<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo {
918-
/// println!("Slicing!");
919-
/// self
920-
/// }
921-
/// fn slice_to_or_fail_mut<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo {
922-
/// println!("Slicing!");
923-
/// self
924-
/// }
925-
/// fn slice_or_fail_mut<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo {
926-
/// println!("Slicing!");
927-
/// self
928-
/// }
929-
/// }
930-
///
931-
/// pub fn main() {
932-
/// Foo[mut Foo..];
933-
/// }
934-
/// ```
935-
#[lang="slice_mut"]
936-
pub trait SliceMut<Idx: ?Sized, Result: ?Sized> {
937-
/// The method for the slicing operation foo[]
938-
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Result;
939-
/// The method for the slicing operation foo[from..]
940-
fn slice_from_or_fail_mut<'a>(&'a mut self, from: &Idx) -> &'a mut Result;
941-
/// The method for the slicing operation foo[..to]
942-
fn slice_to_or_fail_mut<'a>(&'a mut self, to: &Idx) -> &'a mut Result;
943-
/// The method for the slicing operation foo[from..to]
944-
fn slice_or_fail_mut<'a>(&'a mut self, from: &Idx, to: &Idx) -> &'a mut Result;
945-
}
946-
947-
948849
/// An unbounded range.
949850
#[derive(Copy)]
950851
#[lang="full_range"]
@@ -962,8 +863,6 @@ pub struct Range<Idx> {
962863
pub end: Idx,
963864
}
964865

965-
// FIXME(#19391) needs a snapshot
966-
//impl<Idx: Clone + Step<T=uint>> Iterator<Idx> for Range<Idx> {
967866
#[unstable = "API still in development"]
968867
impl<Idx: Clone + Step> Iterator for Range<Idx> {
969868
type Item = Idx;

src/libcore/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
3131
// Reexported core operators
3232
pub use kinds::{Copy, Send, Sized, Sync};
33-
pub use ops::{Drop, Fn, FnMut, FnOnce};
33+
pub use ops::{Drop, Fn, FnMut, FnOnce, FullRange};
3434

3535
// Reexported functions
3636
pub use iter::range;

0 commit comments

Comments
 (0)