mozilla/rust — pull request #11977 by wycats (2014-02-01 19:26:36+00:00)
Title: Add IndexRef and IndexMut

This patch adds two new traits that implement aspects of index
overloading.

It retains the existing Index trait, which takes an &Element and returns
a Result. This is useful for objects whose indexing behavior is to
return simple values like booleans or numbers.

It adds a new IndexRef trait, which takes an &Element and returns a
&Result. This is useful for objects whose indexing behavior is to return
larger values where cloning would be awkward, inappropriate or
impossible.

It also adds a new IndexMut trait, which takes self as &mut self, takes
an &Element and returns a &mut Result. This is useful for objects that
want to allow mutations to their indexed results.

The Index and IndexRef trait both use the `index` method name, so that
only one of the two can be implemented by a given struct. The IndexMut
trait uses the `index_mut` method name, so it can be implemented
alongside one of Index or IndexRef.