diff options
| author | pubfnbar <66903996+pubfnbar@users.noreply.github.com> | 2020-07-31 23:19:10 +0300 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2020-11-16 09:05:15 -0500 |
| commit | c03dfa6671bb462d243c12c72c8829f98c99e394 (patch) | |
| tree | ac59db09b4933023c60988f6ac6f2203ebedebe0 /library/core/src/array | |
| parent | 04688459242356c0f6b9fdad3ba76c9ec4dcc354 (diff) | |
| download | rust-c03dfa6671bb462d243c12c72c8829f98c99e394.tar.gz rust-c03dfa6671bb462d243c12c72c8829f98c99e394.zip | |
Implement Index[Mut] for arrays
Adds implementations of `Index` and `IndexMut` for arrays that simply forward to the slice indexing implementation.
Diffstat (limited to 'library/core/src/array')
| -rw-r--r-- | library/core/src/array/mod.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index 123a191dd2c..769ef02873d 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -12,6 +12,7 @@ use crate::convert::{Infallible, TryFrom}; use crate::fmt; use crate::hash::{self, Hash}; use crate::marker::Unsize; +use crate::ops::{Index, IndexMut}; use crate::slice::{Iter, IterMut}; mod iter; @@ -208,6 +209,30 @@ impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N] { } } +#[stable(feature = "index_trait_on_arrays", since = "1.50.0")] +impl<T, I, const N: usize> Index<I> for [T; N] +where + [T]: Index<I>, +{ + type Output = <[T] as Index<I>>::Output; + + #[inline] + fn index(&self, index: I) -> &Self::Output { + Index::index(self as &[T], index) + } +} + +#[stable(feature = "index_trait_on_arrays", since = "1.50.0")] +impl<T, I, const N: usize> IndexMut<I> for [T; N] +where + [T]: IndexMut<I>, +{ + #[inline] + fn index_mut(&mut self, index: I) -> &mut Self::Output { + IndexMut::index_mut(self as &mut [T], index) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N] where |
