diff options
| author | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2016-09-13 20:51:39 +0200 |
|---|---|---|
| committer | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2016-09-14 20:19:35 +0200 |
| commit | a4ee9c6e96025fa2b5eb254e4ccdd4c6910f5f60 (patch) | |
| tree | 60a3c916b9fab3680945fd2a0a58b4daab3e58a4 /src/libcore | |
| parent | a5dbf8a0f8843c5466c3866cc2a288c0ef7051d2 (diff) | |
| download | rust-a4ee9c6e96025fa2b5eb254e4ccdd4c6910f5f60.tar.gz rust-a4ee9c6e96025fa2b5eb254e4ccdd4c6910f5f60.zip | |
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][..])
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/slice.rs | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index b22bdb43414..97556233659 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -520,8 +520,8 @@ impl<T> ops::Index<usize> for [T] { type Output = T; fn index(&self, index: usize) -> &T { - assert!(index < self.len()); - unsafe { self.get_unchecked(index) } + // NB built-in indexing + &(*self)[index] } } @@ -530,8 +530,8 @@ impl<T> ops::Index<usize> for [T] { impl<T> ops::IndexMut<usize> for [T] { #[inline] fn index_mut(&mut self, index: usize) -> &mut T { - assert!(index < self.len()); - unsafe { self.get_unchecked_mut(index) } + // NB built-in indexing + &mut (*self)[index] } } |
