diff options
| author | Patrick Walton <pcwalton@mimiga.net> | 2014-07-03 14:32:41 -0700 |
|---|---|---|
| committer | Patrick Walton <pcwalton@mimiga.net> | 2014-07-07 11:43:23 -0700 |
| commit | 7e4e99123a68c92f684e5c4466101c1951e86895 (patch) | |
| tree | 270c38b9308597595fc05b233e25ef8252628439 /src/libcore | |
| parent | 4f120e6bafe971452adfede158a7957b00562a4e (diff) | |
| download | rust-7e4e99123a68c92f684e5c4466101c1951e86895.tar.gz rust-7e4e99123a68c92f684e5c4466101c1951e86895.zip | |
librustc (RFC #34): Implement the new `Index` and `IndexMut` traits.
This will break code that used the old `Index` trait. Change this code
to use the new `Index` traits. For reference, here are their signatures:
pub trait Index<Index,Result> {
fn index<'a>(&'a self, index: &Index) -> &'a Result;
}
pub trait IndexMut<Index,Result> {
fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
}
Closes #6515.
[breaking-change]
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/ops.rs | 41 | ||||
| -rw-r--r-- | src/libcore/prelude.rs | 4 |
2 files changed, 40 insertions, 5 deletions
diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index d42c09b8163..fc37fdde8f5 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -613,7 +613,7 @@ shr_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64) /** * * The `Index` trait is used to specify the functionality of indexing operations - * like `arr[idx]`. + * like `arr[idx]` when used in an immutable context. * * # Example * @@ -624,9 +624,9 @@ shr_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64) * struct Foo; * * impl Index<Foo, Foo> for Foo { - * fn index(&self, _rhs: &Foo) -> Foo { + * fn index<'a>(&'a self, _rhs: &Foo) -> &'a Foo { * println!("Indexing!"); - * *self + * self * } * } * @@ -636,9 +636,42 @@ shr_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64) * ``` */ #[lang="index"] +#[cfg(not(stage0))] pub trait Index<Index,Result> { /// The method for the indexing (`Foo[Bar]`) operation - fn index(&self, index: &Index) -> Result; + fn index<'a>(&'a self, index: &Index) -> &'a Result; +} + +/** + * + * The `IndexMut` trait is used to specify the functionality of indexing + * operations like `arr[idx]`, when used in a mutable context. + * + * # Example + * + * A trivial implementation of `IndexMut`. When `Foo[Foo]` happens, it ends up + * calling `index`, and therefore, `main` prints `Indexing!`. + * + * ``` + * struct Foo; + * + * impl IndexMut<Foo, Foo> for Foo { + * fn index_mut<'a>(&'a mut self, _rhs: &Foo) -> &'a mut Foo { + * println!("Indexing!"); + * self + * } + * } + * + * fn main() { + * &mut Foo[Foo]; + * } + * ``` + */ +#[lang="index_mut"] +#[cfg(not(stage0))] +pub trait IndexMut<Index,Result> { + /// The method for the indexing (`Foo[Bar]`) operation + fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result; } /** diff --git a/src/libcore/prelude.rs b/src/libcore/prelude.rs index df9c0e67b0d..f967a2a5fa5 100644 --- a/src/libcore/prelude.rs +++ b/src/libcore/prelude.rs @@ -33,7 +33,9 @@ pub use kinds::{Copy, Send, Sized, Share}; pub use ops::{Add, Sub, Mul, Div, Rem, Neg, Not}; pub use ops::{BitAnd, BitOr, BitXor}; pub use ops::{Drop, Deref, DerefMut}; -pub use ops::{Shl, Shr, Index}; +pub use ops::{Shl, Shr}; +#[cfg(not(stage0))] +pub use ops::{Index, IndexMut}; pub use option::{Option, Some, None}; pub use result::{Result, Ok, Err}; |
