]> git.lizzy.rs Git - rust.git/commit
core: Use primitive indexing in slice's Index/IndexMut
authorUlrik Sverdrup <bluss@users.noreply.github.com>
Tue, 13 Sep 2016 18:51:39 +0000 (20:51 +0200)
committerUlrik Sverdrup <bluss@users.noreply.github.com>
Wed, 14 Sep 2016 18:19:35 +0000 (20:19 +0200)
commita4ee9c6e96025fa2b5eb254e4ccdd4c6910f5f60
tree60a3c916b9fab3680945fd2a0a58b4daab3e58a4
parenta5dbf8a0f8843c5466c3866cc2a288c0ef7051d2
core: Use primitive indexing in slice's Index/IndexMut

[T]'s Index implementation is normally not used for indexing, instead
the compiler supplied indexing is used.

Use the compiler supplied version in Index/IndexMut.

This removes an inconsistency:

Compiler supplied bound check failures look like this:

thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 4'

If you convince Rust to use the Index impl for slices, bounds check
failure looks like this instead:

thread 'main' panicked at 'assertion failed: index < self.len()'

The latter is used if you for example use Index generically::

   use std::ops::Index;
   fn foo<T: ?Sized>(x: &T) where T: Index<usize> { &x[4]; }

   foo(&[1, 2, 3][..])
src/libcore/slice.rs
src/test/run-fail/bounds-check-no-overflow.rs