impl<T> ops::Slice<uint, [T]> for Vec<T> {
#[inline]
- fn as_slice_<'a>(&'a self) -> &'a [T] {
- self.as_slice()
+ fn index_mut<'a>(&'a mut self, index: &uint) -> &'a mut T {
+ &mut self.as_mut_slice()[*index]
}
+}
+impl<T> ops::Index<ops::Range<uint>, [T]> for Vec<T> {
#[inline]
- fn slice_from_or_fail<'a>(&'a self, start: &uint) -> &'a [T] {
- self.as_slice().slice_from_or_fail(start)
+ fn index(&self, &index: &ops::Range<uint>) -> &[T] {
+ self.as_slice().index(index)
}
+}
+impl<T> ops::Index<ops::RangeTo<uint>, [T]> for Vec<T> {
#[inline]
- fn slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [T] {
- self.as_slice().slice_to_or_fail(end)
+ fn index(&self, &index: &ops::RangeTo<uint>) -> &[T] {
+ self.as_slice().index(index)
}
+}
+
+impl<T> ops::Index<ops::RangeFrom<uint>, [T]> for Vec<T> {
#[inline]
- fn slice_or_fail<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] {
- self.as_slice().slice_or_fail(start, end)
+ fn index(&self, &index: &ops::RangeFrom<uint>) -> &[T] {
+ self.as_slice().index(index)
}
}
-impl<T> ops::SliceMut<uint, [T]> for Vec<T> {
+impl<T> ops::Index<ops::FullRange<uint>, [T]> for Vec<T> {
#[inline]
- fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
- self.as_mut_slice()
+ fn index(&self, &index: &ops::FullRange<uint>) -> &[T] {
+ self.as_slice()
}
+}
+impl<T> ops::IndexMut<ops::Range<uint>, [T]> for Vec<T> {
#[inline]
- fn slice_from_or_fail_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] {
- self.as_mut_slice().slice_from_or_fail_mut(start)
+ fn index_mut(&mut self, &index: &ops::Range<uint>) -> &mut [T] {
+ self.as_mut_slice().index_mut(index)
}
+}
+impl<T> ops::IndexMut<ops::RangeTo<uint>, [T]> for Vec<T> {
#[inline]
- fn slice_to_or_fail_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] {
- self.as_mut_slice().slice_to_or_fail_mut(end)
+ fn index_mut(&mut self, &index: &ops::RangeTo<uint>) -> &mut [T] {
+ self.as_mut_slice().index_mut(index)
}
+}
+
+impl<T> ops::IndexMut<ops::RangeFrom<uint>, [T]> for Vec<T> {
#[inline]
- fn slice_or_fail_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] {
- self.as_mut_slice().slice_or_fail_mut(start, end)
+ fn index_mut(&mut self, &index: &ops::RangeFrom<uint>) -> &mut [T] {
+ self.as_mut_slice().index_mut(index)
+ }
+}
+
+impl<T> ops::IndexMut<ops::FullRange<uint>, [T]> for Vec<T> {
+ #[inline]
+ fn index_mut(&mut self, &index: &ops::FullRange<uint>) -> &mut [T] {
+ self.as_mut_slice()
}
}
fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Self::Output;
}
-/// The `Slice` trait is used to specify the functionality of slicing operations
-/// like `arr[from..to]` when used in an immutable context.
-///
-/// # Example
-///
-/// A trivial implementation of `Slice`. When `Foo[..Foo]` happens, it ends up
-/// calling `slice_to`, and therefore, `main` prints `Slicing!`.
-///
-/// ```ignore
-/// use std::ops::Slice;
-///
-/// #[derive(Copy)]
-/// struct Foo;
-///
-/// impl Slice<Foo, Foo> for Foo {
-/// fn as_slice_<'a>(&'a self) -> &'a Foo {
-/// println!("Slicing!");
-/// self
-/// }
-/// fn slice_from_or_fail<'a>(&'a self, _from: &Foo) -> &'a Foo {
-/// println!("Slicing!");
-/// self
-/// }
-/// fn slice_to_or_fail<'a>(&'a self, _to: &Foo) -> &'a Foo {
-/// println!("Slicing!");
-/// self
-/// }
-/// fn slice_or_fail<'a>(&'a self, _from: &Foo, _to: &Foo) -> &'a Foo {
-/// println!("Slicing!");
-/// self
-/// }
-/// }
-///
-/// fn main() {
-/// Foo[..Foo];
-/// }
-/// ```
-#[lang="slice"]
-pub trait Slice<Idx: ?Sized, Result: ?Sized> {
- /// The method for the slicing operation foo[]
- fn as_slice_<'a>(&'a self) -> &'a Result;
- /// The method for the slicing operation foo[from..]
- fn slice_from_or_fail<'a>(&'a self, from: &Idx) -> &'a Result;
- /// The method for the slicing operation foo[..to]
- fn slice_to_or_fail<'a>(&'a self, to: &Idx) -> &'a Result;
- /// The method for the slicing operation foo[from..to]
- fn slice_or_fail<'a>(&'a self, from: &Idx, to: &Idx) -> &'a Result;
-}
-
-/// The `SliceMut` trait is used to specify the functionality of slicing
-/// operations like `arr[from..to]`, when used in a mutable context.
-///
-/// # Example
-///
-/// A trivial implementation of `SliceMut`. When `Foo[Foo..]` happens, it ends up
-/// calling `slice_from_mut`, and therefore, `main` prints `Slicing!`.
-///
-/// ```ignore
-/// use std::ops::SliceMut;
-///
-/// #[derive(Copy)]
-/// struct Foo;
-///
-/// impl SliceMut<Foo, Foo> for Foo {
-/// fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Foo {
-/// println!("Slicing!");
-/// self
-/// }
-/// fn slice_from_or_fail_mut<'a>(&'a mut self, _from: &Foo) -> &'a mut Foo {
-/// println!("Slicing!");
-/// self
-/// }
-/// fn slice_to_or_fail_mut<'a>(&'a mut self, _to: &Foo) -> &'a mut Foo {
-/// println!("Slicing!");
-/// self
-/// }
-/// fn slice_or_fail_mut<'a>(&'a mut self, _from: &Foo, _to: &Foo) -> &'a mut Foo {
-/// println!("Slicing!");
-/// self
-/// }
-/// }
-///
-/// pub fn main() {
-/// Foo[mut Foo..];
-/// }
-/// ```
-#[lang="slice_mut"]
-pub trait SliceMut<Idx: ?Sized, Result: ?Sized> {
- /// The method for the slicing operation foo[]
- fn as_mut_slice_<'a>(&'a mut self) -> &'a mut Result;
- /// The method for the slicing operation foo[from..]
- fn slice_from_or_fail_mut<'a>(&'a mut self, from: &Idx) -> &'a mut Result;
- /// The method for the slicing operation foo[..to]
- fn slice_to_or_fail_mut<'a>(&'a mut self, to: &Idx) -> &'a mut Result;
- /// The method for the slicing operation foo[from..to]
- fn slice_or_fail_mut<'a>(&'a mut self, from: &Idx, to: &Idx) -> &'a mut Result;
-}
-
-
/// An unbounded range.
#[derive(Copy)]
#[lang="full_range"]
pub end: Idx,
}
-// FIXME(#19391) needs a snapshot
-//impl<Idx: Clone + Step<T=uint>> Iterator<Idx> for Range<Idx> {
#[unstable = "API still in development"]
impl<Idx: Clone + Step> Iterator for Range<Idx> {
type Item = Idx;
fn as_mut_slice(&mut self) -> &mut [T] { self }
fn slice_mut(&mut self, start: uint, end: uint) -> &mut [T] {
- ops::SliceMut::slice_or_fail_mut(self, &start, &end)
+ ops::IndexMut::index_mut(self, &ops::Range { start: start, end: end } )
}
#[inline]
fn slice_from_mut(&mut self, start: uint) -> &mut [T] {
- ops::SliceMut::slice_from_or_fail_mut(self, &start)
+ ops::IndexMut::index_mut(self, &ops::RangeFrom { start: start } )
}
#[inline]
fn slice_to_mut(&mut self, end: uint) -> &mut [T] {
- ops::SliceMut::slice_to_or_fail_mut(self, &end)
+ ops::IndexMut::index_mut(self, &ops::RangeTo { end: end } )
}
#[inline]
unsafe {
let self2: &mut [T] = mem::transmute_copy(&self);
- (ops::SliceMut::slice_to_or_fail_mut(self, &mid),
- ops::SliceMut::slice_from_or_fail_mut(self2, &mid))
+ (ops::IndexMut::index_mut(self, &ops::RangeTo { end: mid } ),
+ ops::IndexMut::index_mut(self2, &ops::RangeFrom { start: mid } ))
}
}
}
}
-impl<T> ops::Slice<uint, [T]> for [T] {
+impl<T> ops::Index<ops::Range<uint>, [T]> for [T] {
#[inline]
- fn as_slice_<'a>(&'a self) -> &'a [T] {
- self
+ fn index(&self, &index: &ops::Range<uint>) -> &[T] {
+ assert!(index.start <= index.end);
+ assert!(index.end <= self.len());
+ unsafe {
+ transmute(RawSlice {
+ data: self.as_ptr().offset(index.start as int),
+ len: index.end - index.start
+ })
+ }
+ }
+}
+
+impl<T> ops::Index<ops::RangeTo<uint>, [T]> for [T] {
+ #[inline]
+ fn index(&self, &index: &ops::RangeTo<uint>) -> &[T] {
+ self.index(&ops::Range{ start: 0, end: index.end })
}
+}
+impl<T> ops::Index<ops::RangeFrom<uint>, [T]> for [T] {
#[inline]
- fn slice_from_or_fail<'a>(&'a self, start: &uint) -> &'a [T] {
- self.slice_or_fail(start, &self.len())
+ fn index(&self, &index: &ops::RangeFrom<uint>) -> &[T] {
+ self.index(&ops::Range{ start: index.start, end: self.len() })
}
+}
+impl<T> ops::Index<ops::FullRange, [T]> for [T] {
#[inline]
- fn slice_to_or_fail<'a>(&'a self, end: &uint) -> &'a [T] {
- self.slice_or_fail(&0, end)
+ fn index(&self, &index: &ops::FullRange) -> &[T] {
+ self
}
+}
+
+impl<T> ops::IndexMut<ops::Range<uint>, [T]> for [T] {
#[inline]
- fn slice_or_fail<'a>(&'a self, start: &uint, end: &uint) -> &'a [T] {
- assert!(*start <= *end);
- assert!(*end <= self.len());
+ fn index_mut(&mut self, &index: &ops::Range<uint>) -> &mut [T] {
+ assert!(index.start <= index.end);
+ assert!(index.end <= self.len());
unsafe {
transmute(RawSlice {
- data: self.as_ptr().offset(*start as int),
- len: (*end - *start)
+ data: self.as_ptr().offset(index.start as int),
+ len: index.end - index.start
})
}
}
}
-impl<T> ops::SliceMut<uint, [T]> for [T] {
+impl<T> ops::IndexMut<ops::RangeTo<uint>, [T]> for [T] {
#[inline]
- fn as_mut_slice_<'a>(&'a mut self) -> &'a mut [T] {
- self
+ fn index_mut(&mut self, &index: &ops::RangeTo<uint>) -> &mut [T] {
+ self.index_mut(&ops::Range{ start: 0, end: index.end })
}
+}
+impl<T> ops::IndexMut<ops::RangeFrom<uint>, [T]> for [T] {
#[inline]
- fn slice_from_or_fail_mut<'a>(&'a mut self, start: &uint) -> &'a mut [T] {
- let len = &self.len();
- self.slice_or_fail_mut(start, len)
+ fn index_mut(&mut self, &index: &ops::RangeFrom<uint>) -> &mut [T] {
+ let len = self.len();
+ self.index_mut(&ops::Range{ start: index.start, end: len })
}
+}
+impl<T> ops::IndexMut<ops::FullRange, [T]> for [T] {
#[inline]
- fn slice_to_or_fail_mut<'a>(&'a mut self, end: &uint) -> &'a mut [T] {
- self.slice_or_fail_mut(&0, end)
- }
- #[inline]
- fn slice_or_fail_mut<'a>(&'a mut self, start: &uint, end: &uint) -> &'a mut [T] {
- assert!(*start <= *end);
- assert!(*end <= self.len());
- unsafe {
- transmute(RawSlice {
- data: self.as_ptr().offset(*start as int),
- len: (*end - *start)
- })
- }
+ fn index_mut(&mut self, &index: &ops::FullRange) -> &mut [T] {
+ self
}
}
+
////////////////////////////////////////////////////////////////////////////////
// Common traits
////////////////////////////////////////////////////////////////////////////////
}
#[experimental]
-impl<'a, T> ops::Slice<uint, [T]> for Iter<'a, T> {
- fn as_slice_(&self) -> &[T] {
- self.as_slice()
+impl<'a, T> ops::Index<ops::Range<uint>, [T]> for Iter<'a, T> {
+ #[inline]
+ fn index(&self, index: &ops::Range<uint>) -> &[T] {
+ self.as_slice().index(index)
}
- fn slice_from_or_fail<'b>(&'b self, from: &uint) -> &'b [T] {
- use ops::Slice;
- self.as_slice().slice_from_or_fail(from)
+}
+
+#[experimental]
+impl<'a, T> ops::Index<ops::RangeTo<uint>, [T]> for Iter<'a, T> {
+ #[inline]
+ fn index(&self, index: &ops::RangeTo<uint>) -> &[T] {
+ self.as_slice().index(index)
}
- fn slice_to_or_fail<'b>(&'b self, to: &uint) -> &'b [T] {
- use ops::Slice;
- self.as_slice().slice_to_or_fail(to)
+}
+
+#[experimental]
+impl<'a, T> ops::Index<ops::RangeFrom<uint>, [T]> for Iter<'a, T> {
+ #[inline]
+ fn index(&self, index: &ops::RangeFrom<uint>) -> &[T] {
+ self.as_slice().index(index)
}
- fn slice_or_fail<'b>(&'b self, from: &uint, to: &uint) -> &'b [T] {
- use ops::Slice;
- self.as_slice().slice_or_fail(from, to)
+}
+
+#[experimental]
+impl<'a, T> ops::Index<ops::FullRange, [T]> for Iter<'a, T> {
+ #[inline]
+ fn index(&self, index: &ops::FullRange) -> &[T] {
+ self.as_slice()
}
}
+
impl<'a, T> Iter<'a, T> {
/// View the underlying data as a subslice of the original data.
///
}
#[experimental]
-impl<'a, T> ops::Slice<uint, [T]> for IterMut<'a, T> {
- fn as_slice_<'b>(&'b self) -> &'b [T] {
- make_slice!(T -> &'b [T]: self.ptr, self.end)
+impl<'a, T> ops::Index<ops::Range<uint>, [T]> for IterMut<'a, T> {
+ #[inline]
+ fn index(&self, index: &ops::Range<uint>) -> &[T] {
+ self.index(&ops::FullRange).index(index)
}
- fn slice_from_or_fail<'b>(&'b self, from: &uint) -> &'b [T] {
- use ops::Slice;
- self.as_slice_().slice_from_or_fail(from)
+}
+
+#[experimental]
+impl<'a, T> ops::Index<ops::RangeTo<uint>, [T]> for IterMut<'a, T> {
+ #[inline]
+ fn index(&self, index: &ops::RangeTo<uint>) -> &[T] {
+ self.index(&ops::FullRange).index(index)
}
- fn slice_to_or_fail<'b>(&'b self, to: &uint) -> &'b [T] {
- use ops::Slice;
- self.as_slice_().slice_to_or_fail(to)
+}
+
+#[experimental]
+impl<'a, T> ops::Index<ops::RangeFrom<uint>, [T]> for IterMut<'a, T> {
+ #[inline]
+ fn index(&self, index: &ops::RangeFrom<uint>) -> &[T] {
+ self.index(&ops::FullRange).index(index)
}
- fn slice_or_fail<'b>(&'b self, from: &uint, to: &uint) -> &'b [T] {
- use ops::Slice;
- self.as_slice_().slice_or_fail(from, to)
+}
+
+#[experimental]
+impl<'a, T> ops::Index<ops::FullRange, [T]> for IterMut<'a, T> {
+ #[inline]
+ fn index(&self, index: &ops::FullRange) -> &[T] {
+ make_slice!(T -> &[T]: self.ptr, self.end)
}
}
#[experimental]
-impl<'a, T> ops::SliceMut<uint, [T]> for IterMut<'a, T> {
- fn as_mut_slice_<'b>(&'b mut self) -> &'b mut [T] {
- make_slice!(T -> &'b mut [T]: self.ptr, self.end)
+impl<'a, T> ops::IndexMut<ops::Range<uint>, [T]> for IterMut<'a, T> {
+ #[inline]
+ fn index_mut(&mut self, index: &ops::Range<uint>) -> &mut [T] {
+ self.index_mut(&ops::FullRange).index_mut(index)
}
- fn slice_from_or_fail_mut<'b>(&'b mut self, from: &uint) -> &'b mut [T] {
- use ops::SliceMut;
- self.as_mut_slice_().slice_from_or_fail_mut(from)
+}
+
+#[experimental]
+impl<'a, T> ops::IndexMut<ops::RangeTo<uint>, [T]> for IterMut<'a, T> {
+ #[inline]
+ fn index_mut(&mut self, index: &ops::RangeTo<uint>) -> &mut [T] {
+ self.index_mut(&ops::FullRange).index_mut(index)
}
- fn slice_to_or_fail_mut<'b>(&'b mut self, to: &uint) -> &'b mut [T] {
- use ops::SliceMut;
- self.as_mut_slice_().slice_to_or_fail_mut(to)
+}
+
+#[experimental]
+impl<'a, T> ops::IndexMut<ops::RangeFrom<uint>, [T]> for IterMut<'a, T> {
+ #[inline]
+ fn index_mut(&mut self, index: &ops::RangeFrom<uint>) -> &mut [T] {
+ self.index_mut(&ops::FullRange).index_mut(index)
}
- fn slice_or_fail_mut<'b>(&'b mut self, from: &uint, to: &uint) -> &'b mut [T] {
- use ops::SliceMut;
- self.as_mut_slice_().slice_or_fail_mut(from, to)
+}
+
+#[experimental]
+impl<'a, T> ops::IndexMut<ops::FullRange, [T]> for IterMut<'a, T> {
+ #[inline]
+ fn index_mut(&mut self, index: &ops::FullRange) -> &mut [T] {
+ make_slice!(T -> &mut [T]: self.ptr, self.end)
}
}
+
impl<'a, T> IterMut<'a, T> {
/// View the underlying data as a subslice of the original data.
///