shr_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 }
+// NOTE(stage0) remove trait after a snapshot
+#[cfg(stage0)]
+#[allow(missing_docs)]
+#[lang="index"]
+pub trait Index<Sized? Index, Sized? Result> for Sized? {
+ /// The method for the indexing (`Foo[Bar]`) operation
+ fn index<'a>(&'a self, index: &Index) -> &'a Result;
+}
+
/// The `Index` trait is used to specify the functionality of indexing operations
/// like `arr[idx]` when used in an immutable context.
///
/// calling `index`, and therefore, `main` prints `Indexing!`.
///
/// ```
+/// #![feature(associated_types)]
+///
/// use std::ops::Index;
///
/// #[deriving(Copy)]
/// struct Foo;
///
-/// impl Index<Foo, Foo> for Foo {
+/// impl Index<Foo> for Foo {
+/// type Output = Foo;
+///
/// fn index<'a>(&'a self, _index: &Foo) -> &'a Foo {
/// println!("Indexing!");
/// self
/// Foo[Foo];
/// }
/// ```
+#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot
#[lang="index"]
-pub trait Index<Sized? Index, Sized? Result> for Sized? {
+pub trait Index<Sized? Index> for Sized? {
+ type Sized? Output;
+
/// The method for the indexing (`Foo[Bar]`) operation
- fn index<'a>(&'a self, index: &Index) -> &'a Result;
+ fn index<'a>(&'a self, index: &Index) -> &'a Self::Output;
+}
+
+// NOTE(stage0) remove trait after a snapshot
+#[cfg(stage0)]
+#[allow(missing_docs)]
+#[lang="index_mut"]
+pub trait IndexMut<Sized? Index, Sized? Result> for Sized? {
+ /// The method for the indexing (`Foo[Bar]`) operation
+ fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
}
/// The `IndexMut` trait is used to specify the functionality of indexing
/// calling `index_mut`, and therefore, `main` prints `Indexing!`.
///
/// ```
+/// #![feature(associated_types)]
+///
/// use std::ops::IndexMut;
///
/// #[deriving(Copy)]
/// struct Foo;
///
-/// impl IndexMut<Foo, Foo> for Foo {
+/// impl IndexMut<Foo> for Foo {
+/// type Output = Foo;
+///
/// fn index_mut<'a>(&'a mut self, _index: &Foo) -> &'a mut Foo {
/// println!("Indexing!");
/// self
/// &mut Foo[Foo];
/// }
/// ```
+#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot
#[lang="index_mut"]
-pub trait IndexMut<Sized? Index, Sized? Result> for Sized? {
+pub trait IndexMut<Sized? Index> for Sized? {
+ type Sized? Output;
+
/// The method for the indexing (`Foo[Bar]`) operation
- fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
+ 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
}
}
+// NOTE(stage0) remove impl after a snapshot
+#[cfg(stage0)]
impl<T> ops::Index<uint, T> for [T] {
fn index(&self, &index: &uint) -> &T {
assert!(index < self.len());
}
}
+#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot
+impl<T> ops::Index<uint> for [T] {
+ type Output = T;
+
+ fn index(&self, &index: &uint) -> &T {
+ assert!(index < self.len());
+
+ unsafe { mem::transmute(self.repr().data.offset(index as int)) }
+ }
+}
+
+// NOTE(stage0) remove impl after a snapshot
+#[cfg(stage0)]
impl<T> ops::IndexMut<uint, T> for [T] {
fn index_mut(&mut self, &index: &uint) -> &mut T {
assert!(index < self.len());
}
}
+#[cfg(not(stage0))] // NOTE(stage0) remove cfg after a snapshot
+impl<T> ops::IndexMut<uint> for [T] {
+ type Output = T;
+
+ fn index_mut(&mut self, &index: &uint) -> &mut T {
+ assert!(index < self.len());
+
+ unsafe { mem::transmute(self.repr().data.offset(index as int)) }
+ }
+}
+
impl<T> ops::Slice<uint, [T]> for [T] {
#[inline]
fn as_slice_<'a>(&'a self) -> &'a [T] {