Skip to content

Commit 6cf36ca

Browse files
nrcnikomatsakis
authored andcommitted
Impls using the new scheme for slicing
1 parent 03268bb commit 6cf36ca

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
@@ -842,25 +842,31 @@ impl<'a> Add<&'a str> for String {
842842
}
843843
}
844844

845-
impl ops::Slice<uint, str> for String {
845+
impl<T> ops::Index<ops::Range<uint>, str> for String {
846846
#[inline]
847-
fn as_slice_<'a>(&'a self) -> &'a str {
848-
unsafe { mem::transmute(self.vec.as_slice()) }
847+
fn index(&self, &index: &ops::Range<uint>) -> &str {
848+
self[][*index]
849849
}
850+
}
850851

852+
impl<T> ops::Index<ops::RangeTo<uint>, str> for String {
851853
#[inline]
852-
fn slice_from_or_fail<'a>(&'a self, from: &uint) -> &'a str {
853-
self[][*from..]
854+
fn index(&self, &index: &ops::RangeTo<uint>) -> &str {
855+
self[][*index]
854856
}
857+
}
855858

859+
impl<T> ops::Index<ops::RangeFrom<uint>, str> for String {
856860
#[inline]
857-
fn slice_to_or_fail<'a>(&'a self, to: &uint) -> &'a str {
858-
self[][..*to]
861+
fn index(&self, &index: &ops::RangeFrom<uint>) -> &str {
862+
self[][*index]
859863
}
864+
}
860865

866+
impl<T> ops::Index<ops::FullRange<uint>, str> for String {
861867
#[inline]
862-
fn slice_or_fail<'a>(&'a self, from: &uint, to: &uint) -> &'a str {
863-
self[][*from..*to]
868+
fn index(&self, &index: &ops::FullRange<uint>) -> &str {
869+
unsafe { mem::transmute(self.vec.as_slice()) }
864870
}
865871
}
866872

src/libcollections/vec.rs

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

12331233
impl<T> ops::Slice<uint, [T]> for Vec<T> {
12341234
#[inline]
1235-
fn as_slice_<'a>(&'a self) -> &'a [T] {
1236-
self.as_slice()
1235+
fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut T {
1236+
&mut self.as_mut_slice()[*index]
12371237
}
1238+
}
12381239

1240+
impl<T> ops::Index<ops::Range<uint>, [T]> for Vec<T> {
12391241
#[inline]
1240-
fn slice_from_or_fail<'a>(&'a self, start: &uint) -> &'a [T] {
1241-
self.as_slice().slice_from_or_fail(start)
1242+
fn index(&self, &index: &ops::Range<uint>) -> &[T] {
1243+
self.as_slice().index(index)
12421244
}
1245+
}
12431246

1247+
impl<T> ops::Index<ops::RangeTo<uint>, [T]> for Vec<T> {
12441248
#[inline]
1245-
fn slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [T] {
1246-
self.as_slice().slice_to_or_fail(end)
1249+
fn index(&self, &index: &ops::RangeTo<uint>) -> &[T] {
1250+
self.as_slice().index(index)
12471251
}
1252+
}
1253+
1254+
impl<T> ops::Index<ops::RangeFrom<uint>, [T]> for Vec<T> {
12481255
#[inline]
1249-
fn slice_or_fail<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] {
1250-
self.as_slice().slice_or_fail(start, end)
1256+
fn index(&self, &index: &ops::RangeFrom<uint>) -> &[T] {
1257+
self.as_slice().index(index)
12511258
}
12521259
}
12531260

1254-
impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
1261+
impl<T> ops::Index<ops::FullRange<uint>, [T]> for Vec<T> {
12551262
#[inline]
1256-
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
1257-
self.as_mut_slice()
1263+
fn index(&self, &index: &ops::FullRange<uint>) -> &[T] {
1264+
self.as_slice()
12581265
}
1266+
}
12591267

1268+
impl<T> ops::IndexMut<ops::Range<uint>, [T]> for Vec<T> {
12601269
#[inline]
1261-
fn slice_from_or_fail_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] {
1262-
self.as_mut_slice().slice_from_or_fail_mut(start)
1270+
fn index_mut(&mut self, &index: &ops::Range<uint>) -> &mut [T] {
1271+
self.as_mut_slice().index_mut(index)
12631272
}
1273+
}
12641274

1275+
impl<T> ops::IndexMut<ops::RangeTo<uint>, [T]> for Vec<T> {
12651276
#[inline]
1266-
fn slice_to_or_fail_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] {
1267-
self.as_mut_slice().slice_to_or_fail_mut(end)
1277+
fn index_mut(&mut self, &index: &ops::RangeTo<uint>) -> &mut [T] {
1278+
self.as_mut_slice().index_mut(index)
12681279
}
1280+
}
1281+
1282+
impl<T> ops::IndexMut<ops::RangeFrom<uint>, [T]> for Vec<T> {
12691283
#[inline]
1270-
fn slice_or_fail_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] {
1271-
self.as_mut_slice().slice_or_fail_mut(start, end)
1284+
fn index_mut(&mut self, &index: &ops::RangeFrom<uint>) -> &mut [T] {
1285+
self.as_mut_slice().index_mut(index)
1286+
}
1287+
}
1288+
1289+
impl<T> ops::IndexMut<ops::FullRange<uint>, [T]> for Vec<T> {
1290+
#[inline]
1291+
fn index_mut(&mut self, &index: &ops::FullRange<uint>) -> &mut [T] {
1292+
self.as_mut_slice()
12721293
}
12731294
}
12741295

src/libcore/ops.rs

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

814-
/// The `Slice` trait is used to specify the functionality of slicing operations
815-
/// like `arr[from..to]` when used in an immutable context.
816-
///
817-
/// # Example
818-
///
819-
/// A trivial implementation of `Slice`. When `Foo[..Foo]` happens, it ends up
820-
/// calling `slice_to`, and therefore, `main` prints `Slicing!`.
821-
///
822-
/// ```ignore
823-
/// use std::ops::Slice;
824-
///
825-
/// #[derive(Copy)]
826-
/// struct Foo;
827-
///
828-
/// impl Slice<Foo, Foo> for Foo {
829-
/// fn as_slice_<'a>(&'a self) -> &'a Foo {
830-
/// println!("Slicing!");
831-
/// self
832-
/// }
833-
/// fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
834-
/// println!("Slicing!");
835-
/// self
836-
/// }
837-
/// fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
838-
/// println!("Slicing!");
839-
/// self
840-
/// }
841-
/// fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
842-
/// println!("Slicing!");
843-
/// self
844-
/// }
845-
/// }
846-
///
847-
/// fn main() {
848-
/// Foo[..Foo];
849-
/// }
850-
/// ```
851-
#[lang="slice"]
852-
pub trait Slice<Sized? Idx, Sized? Result> for Sized? {
853-
/// The method for the slicing operation foo[]
854-
fn as_slice_<'a>(&'a self) -> &'a Result;
855-
/// The method for the slicing operation foo[from..]
856-
fn slice_from_or_fail<'a>(&'a self, from: &Idx) -> &'a Result;
857-
/// The method for the slicing operation foo[..to]
858-
fn slice_to_or_fail<'a>(&'a self, to: &Idx) -> &'a Result;
859-
/// The method for the slicing operation foo[from..to]
860-
fn slice_or_fail<'a>(&'a self, from: &Idx, to: &Idx) -> &'a Result;
861-
}
862-
863-
/// The `SliceMut` trait is used to specify the functionality of slicing
864-
/// operations like `arr[from..to]`, when used in a mutable context.
865-
///
866-
/// # Example
867-
///
868-
/// A trivial implementation of `SliceMut`. When `Foo[Foo..]` happens, it ends up
869-
/// calling `slice_from_mut`, and therefore, `main` prints `Slicing!`.
870-
///
871-
/// ```ignore
872-
/// use std::ops::SliceMut;
873-
///
874-
/// #[derive(Copy)]
875-
/// struct Foo;
876-
///
877-
/// impl SliceMut<Foo, Foo> for Foo {
878-
/// fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
879-
/// println!("Slicing!");
880-
/// self
881-
/// }
882-
/// fn slice_from_or_fail_mut<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo {
883-
/// println!("Slicing!");
884-
/// self
885-
/// }
886-
/// fn slice_to_or_fail_mut<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo {
887-
/// println!("Slicing!");
888-
/// self
889-
/// }
890-
/// fn slice_or_fail_mut<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo {
891-
/// println!("Slicing!");
892-
/// self
893-
/// }
894-
/// }
895-
///
896-
/// pub fn main() {
897-
/// Foo[mut Foo..];
898-
/// }
899-
/// ```
900-
#[lang="slice_mut"]
901-
pub trait SliceMut<Sized? Idx, Sized? Result> for Sized? {
902-
/// The method for the slicing operation foo[]
903-
fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Result;
904-
/// The method for the slicing operation foo[from..]
905-
fn slice_from_or_fail_mut<'a>(&'a mut self, from: &Idx) -> &'a mut Result;
906-
/// The method for the slicing operation foo[..to]
907-
fn slice_to_or_fail_mut<'a>(&'a mut self, to: &Idx) -> &'a mut Result;
908-
/// The method for the slicing operation foo[from..to]
909-
fn slice_or_fail_mut<'a>(&'a mut self, from: &Idx, to: &Idx) -> &'a mut Result;
910-
}
911-
912-
913814
/// An unbounded range.
914815
#[derive(Copy)]
915816
#[lang="full_range"]
@@ -925,8 +826,6 @@ pub struct Range<Idx> {
925826
pub end: Idx,
926827
}
927828

928-
// FIXME(#19391) needs a snapshot
929-
//impl<Idx: Clone + Step<T=uint>> Iterator<Idx> for Range<Idx> {
930829
impl<Idx: Clone + Step> Iterator for Range<Idx> {
931830
type Item = Idx;
932831

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)