// option. This file may not be copied, modified, or distributed
// except according to those terms.
+#![feature(associated_types)]
+
use std::ops::Index;
struct MyVec<T> {
data: Vec<T>,
}
-impl<T> Index<uint, T> for MyVec<T> {
+impl<T> Index<uint> for MyVec<T> {
+ type Output = T;
+
fn index(&self, &i: &uint) -> &T {
&self.data[i]
}
// Test that we still see borrowck errors of various kinds when using
// indexing and autoderef in combination.
+#![feature(associated_types)]
+
use std::ops::{Index, IndexMut};
struct Foo {
y: int,
}
-impl Index<String,int> for Foo {
+impl Index<String> for Foo {
+ type Output = int;
+
fn index<'a>(&'a self, z: &String) -> &'a int {
if z.as_slice() == "x" {
&self.x
}
}
-impl IndexMut<String,int> for Foo {
+impl IndexMut<String> for Foo {
+ type Output = int;
+
fn index_mut<'a>(&'a mut self, z: &String) -> &'a mut int {
if z.as_slice() == "x" {
&mut self.x
// option. This file may not be copied, modified, or distributed
// except according to those terms.
+#![feature(associated_types)]
+
use std::ops::{Index, IndexMut};
struct Foo {
y: int,
}
-impl Index<String,int> for Foo {
+impl Index<String> for Foo {
+ type Output = int;
+
fn index<'a>(&'a self, z: &String) -> &'a int {
if z.as_slice() == "x" {
&self.x
}
}
-impl IndexMut<String,int> for Foo {
+impl IndexMut<String> for Foo {
+ type Output = int;
+
fn index_mut<'a>(&'a mut self, z: &String) -> &'a mut int {
if z.as_slice() == "x" {
&mut self.x
x: int,
}
-impl Index<int,int> for Bar {
+impl Index<int> for Bar {
+ type Output = int;
+
fn index<'a>(&'a self, z: &int) -> &'a int {
&self.x
}
// Test that overloaded index expressions with DST result types
// can't be used as rvalues
+#![feature(associated_types)]
+
use std::ops::Index;
use std::fmt::Show;
impl Copy for S {}
-impl Index<uint, str> for S {
+impl Index<uint> for S {
+ type Output = str;
+
fn index<'a>(&'a self, _: &uint) -> &'a str {
"hello"
}
impl Copy for T {}
-impl Index<uint, Show + 'static> for T {
+impl Index<uint> for T {
+ type Output = Show + 'static;
+
fn index<'a>(&'a self, idx: &uint) -> &'a (Show + 'static) {
static x: uint = 42;
&x
// Test that overloaded index expressions with DST result types
// work and don't ICE.
+#![feature(associated_types)]
+
use std::ops::Index;
use std::fmt::Show;
struct S;
-impl Index<uint, str> for S {
+impl Index<uint> for S {
+ type Output = str;
+
fn index<'a>(&'a self, _: &uint) -> &'a str {
"hello"
}
struct T;
-impl Index<uint, Show + 'static> for T {
+impl Index<uint> for T {
+ type Output = Show + 'static;
+
fn index<'a>(&'a self, idx: &uint) -> &'a (Show + 'static) {
static x: uint = 42;
&x
// If `Index` used an associated type for its output, this test would
// work more smoothly.
-#![feature(old_orphan_check)]
+
+#![feature(associated_types, old_orphan_check)]
use std::ops::Index;
}
}
-impl<T> Index<(uint, uint), T> for Mat<T> {
+impl<T> Index<(uint, uint)> for Mat<T> {
+ type Output = T;
+
fn index<'a>(&'a self, &(row, col): &(uint, uint)) -> &'a T {
&self.data[row * self.cols + col]
}
}
-impl<'a, T> Index<(uint, uint), T> for &'a Mat<T> {
+impl<'a, T> Index<(uint, uint)> for &'a Mat<T> {
+ type Output = T;
+
fn index<'b>(&'b self, index: &(uint, uint)) -> &'b T {
(*self).index(index)
}
struct Row<M> { mat: M, row: uint, }
-impl<T, M: Index<(uint, uint), T>> Index<uint, T> for Row<M> {
+impl<T, M: Index<(uint, uint), Output=T>> Index<uint> for Row<M> {
+ type Output = T;
+
fn index<'a>(&'a self, col: &uint) -> &'a T {
&self.mat[(self.row, *col)]
}
}
}
-impl ops::Index<bool,int> for Point {
+impl ops::Index<bool> for Point {
+ type Output = int;
+
fn index(&self, x: &bool) -> &int {
if *x {
&self.x
// Test overloading of the `[]` operator. In particular test that it
// takes its argument *by reference*.
+#![feature(associated_types)]
+
use std::ops::Index;
struct AssociationList<K,V> {
}
}
-impl<K: PartialEq + std::fmt::Show, V:Clone> Index<K,V> for AssociationList<K,V> {
+impl<K: PartialEq + std::fmt::Show, V:Clone> Index<K> for AssociationList<K,V> {
+ type Output = V;
+
fn index<'a>(&'a self, index: &K) -> &'a V {
for pair in self.pairs.iter() {
if pair.key == *index {
// Test overloaded indexing combined with autoderef.
+#![feature(associated_types)]
+
use std::ops::{Index, IndexMut};
struct Foo {
y: int,
}
-impl Index<int,int> for Foo {
+impl Index<int> for Foo {
+ type Output = int;
+
fn index(&self, z: &int) -> &int {
if *z == 0 {
&self.x
}
}
-impl IndexMut<int,int> for Foo {
+impl IndexMut<int> for Foo {
+ type Output = int;
+
fn index_mut(&mut self, z: &int) -> &mut int {
if *z == 0 {
&mut self.x
// Test using overloaded indexing when the "map" is stored in a
// field. This caused problems at some point.
+#![feature(associated_types)]
+
use std::ops::Index;
struct Foo {
foo: Foo
}
-impl Index<int,int> for Foo {
+impl Index<int> for Foo {
+ type Output = int;
+
fn index(&self, z: &int) -> &int {
if *z == 0 {
&self.x
// option. This file may not be copied, modified, or distributed
// except according to those terms.
+#![feature(associated_types)]
+
use std::ops::{Index, IndexMut};
struct Foo {
y: int,
}
-impl Index<int,int> for Foo {
+impl Index<int> for Foo {
+ type Output = int;
+
fn index(&self, z: &int) -> &int {
if *z == 0 {
&self.x
}
}
-impl IndexMut<int,int> for Foo {
+impl IndexMut<int> for Foo {
+ type Output = int;
+
fn index_mut(&mut self, z: &int) -> &mut int {
if *z == 0 {
&mut self.x