1 //! Implementations of things like `Eq` for fixed-length arrays
2 //! up to a certain length. Eventually we should able to generalize
5 //! *[See also the array primitive type](../../std/primitive.array.html).*
7 #![stable(feature = "core_array", since = "1.36.0")]
9 use crate::borrow::{Borrow, BorrowMut};
10 use crate::cmp::Ordering;
11 use crate::convert::{Infallible, TryFrom};
13 use crate::hash::{Hash, self};
14 use crate::marker::Unsize;
15 use crate::slice::{Iter, IterMut};
17 /// Utility trait implemented only on arrays of fixed size
19 /// This trait can be used to implement other traits on fixed-size arrays
20 /// without causing much metadata bloat.
22 /// The trait is marked unsafe in order to restrict implementors to fixed-size
23 /// arrays. User of this trait can assume that implementors have the exact
24 /// layout in memory of a fixed size array (for example, for unsafe
27 /// Note that the traits AsRef and AsMut provide similar methods for types that
28 /// may not be fixed-size arrays. Implementors should prefer those traits
30 #[unstable(feature = "fixed_size_array", issue = "27778")]
31 pub unsafe trait FixedSizeArray<T> {
32 /// Converts the array to immutable slice
33 #[unstable(feature = "fixed_size_array", issue = "27778")]
34 fn as_slice(&self) -> &[T];
35 /// Converts the array to mutable slice
36 #[unstable(feature = "fixed_size_array", issue = "27778")]
37 fn as_mut_slice(&mut self) -> &mut [T];
40 #[unstable(feature = "fixed_size_array", issue = "27778")]
41 unsafe impl<T, A: Unsize<[T]>> FixedSizeArray<T> for A {
43 fn as_slice(&self) -> &[T] {
47 fn as_mut_slice(&mut self) -> &mut [T] {
52 /// The error type returned when a conversion from a slice to an array fails.
53 #[stable(feature = "try_from", since = "1.34.0")]
54 #[derive(Debug, Copy, Clone)]
55 pub struct TryFromSliceError(());
57 #[stable(feature = "core_array", since = "1.36.0")]
58 impl fmt::Display for TryFromSliceError {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 fmt::Display::fmt(self.__description(), f)
65 impl TryFromSliceError {
66 #[unstable(feature = "array_error_internals",
67 reason = "available through Error trait and this method should not \
72 pub fn __description(&self) -> &str {
73 "could not convert slice to array"
77 #[stable(feature = "try_from_slice_error", since = "1.36.0")]
78 impl From<Infallible> for TryFromSliceError {
79 fn from(x: Infallible) -> TryFromSliceError {
84 #[stable(feature = "rust1", since = "1.0.0")]
85 impl<T, const N: usize> AsRef<[T]> for [T; N]
87 [T; N]: LengthAtMost32,
90 fn as_ref(&self) -> &[T] {
95 #[stable(feature = "rust1", since = "1.0.0")]
96 impl<T, const N: usize> AsMut<[T]> for [T; N]
98 [T; N]: LengthAtMost32,
101 fn as_mut(&mut self) -> &mut [T] {
106 #[stable(feature = "array_borrow", since = "1.4.0")]
107 impl<T, const N: usize> Borrow<[T]> for [T; N]
109 [T; N]: LengthAtMost32,
111 fn borrow(&self) -> &[T] {
116 #[stable(feature = "array_borrow", since = "1.4.0")]
117 impl<T, const N: usize> BorrowMut<[T]> for [T; N]
119 [T; N]: LengthAtMost32,
121 fn borrow_mut(&mut self) -> &mut [T] {
126 #[stable(feature = "try_from", since = "1.34.0")]
127 impl<T, const N: usize> TryFrom<&[T]> for [T; N]
130 [T; N]: LengthAtMost32,
132 type Error = TryFromSliceError;
134 fn try_from(slice: &[T]) -> Result<[T; N], TryFromSliceError> {
135 <&Self>::try_from(slice).map(|r| *r)
139 #[stable(feature = "try_from", since = "1.34.0")]
140 impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N]
142 [T; N]: LengthAtMost32,
144 type Error = TryFromSliceError;
146 fn try_from(slice: &[T]) -> Result<&[T; N], TryFromSliceError> {
147 if slice.len() == N {
148 let ptr = slice.as_ptr() as *const [T; N];
151 Err(TryFromSliceError(()))
156 #[stable(feature = "try_from", since = "1.34.0")]
157 impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N]
159 [T; N]: LengthAtMost32,
161 type Error = TryFromSliceError;
163 fn try_from(slice: &mut [T]) -> Result<&mut [T; N], TryFromSliceError> {
164 if slice.len() == N {
165 let ptr = slice.as_mut_ptr() as *mut [T; N];
166 unsafe { Ok(&mut *ptr) }
168 Err(TryFromSliceError(()))
173 #[stable(feature = "rust1", since = "1.0.0")]
174 impl<T: Hash, const N: usize> Hash for [T; N]
176 [T; N]: LengthAtMost32,
178 fn hash<H: hash::Hasher>(&self, state: &mut H) {
179 Hash::hash(&self[..], state)
183 #[stable(feature = "rust1", since = "1.0.0")]
184 impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N]
186 [T; N]: LengthAtMost32,
188 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
189 fmt::Debug::fmt(&&self[..], f)
193 #[stable(feature = "rust1", since = "1.0.0")]
194 impl<'a, T, const N: usize> IntoIterator for &'a [T; N]
196 [T; N]: LengthAtMost32,
199 type IntoIter = Iter<'a, T>;
201 fn into_iter(self) -> Iter<'a, T> {
206 #[stable(feature = "rust1", since = "1.0.0")]
207 impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]
209 [T; N]: LengthAtMost32,
211 type Item = &'a mut T;
212 type IntoIter = IterMut<'a, T>;
214 fn into_iter(self) -> IterMut<'a, T> {
219 #[stable(feature = "rust1", since = "1.0.0")]
220 impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N]
223 [A; N]: LengthAtMost32,
224 [B; N]: LengthAtMost32,
227 fn eq(&self, other: &[B; N]) -> bool {
228 self[..] == other[..]
231 fn ne(&self, other: &[B; N]) -> bool {
232 self[..] != other[..]
236 #[stable(feature = "rust1", since = "1.0.0")]
237 impl<A, B, const N: usize> PartialEq<[B]> for [A; N]
240 [A; N]: LengthAtMost32,
243 fn eq(&self, other: &[B]) -> bool {
244 self[..] == other[..]
247 fn ne(&self, other: &[B]) -> bool {
248 self[..] != other[..]
252 #[stable(feature = "rust1", since = "1.0.0")]
253 impl<A, B, const N: usize> PartialEq<[A; N]> for [B]
256 [A; N]: LengthAtMost32,
259 fn eq(&self, other: &[A; N]) -> bool {
260 self[..] == other[..]
263 fn ne(&self, other: &[A; N]) -> bool {
264 self[..] != other[..]
268 #[stable(feature = "rust1", since = "1.0.0")]
269 impl<'b, A, B, const N: usize> PartialEq<&'b [B]> for [A; N]
272 [A; N]: LengthAtMost32,
275 fn eq(&self, other: &&'b [B]) -> bool {
276 self[..] == other[..]
279 fn ne(&self, other: &&'b [B]) -> bool {
280 self[..] != other[..]
284 #[stable(feature = "rust1", since = "1.0.0")]
285 impl<'b, A, B, const N: usize> PartialEq<[A; N]> for &'b [B]
288 [A; N]: LengthAtMost32,
291 fn eq(&self, other: &[A; N]) -> bool {
292 self[..] == other[..]
295 fn ne(&self, other: &[A; N]) -> bool {
296 self[..] != other[..]
300 #[stable(feature = "rust1", since = "1.0.0")]
301 impl<'b, A, B, const N: usize> PartialEq<&'b mut [B]> for [A; N]
304 [A; N]: LengthAtMost32,
307 fn eq(&self, other: &&'b mut [B]) -> bool {
308 self[..] == other[..]
311 fn ne(&self, other: &&'b mut [B]) -> bool {
312 self[..] != other[..]
316 #[stable(feature = "rust1", since = "1.0.0")]
317 impl<'b, A, B, const N: usize> PartialEq<[A; N]> for &'b mut [B]
320 [A; N]: LengthAtMost32,
323 fn eq(&self, other: &[A; N]) -> bool {
324 self[..] == other[..]
327 fn ne(&self, other: &[A; N]) -> bool {
328 self[..] != other[..]
332 // NOTE: some less important impls are omitted to reduce code bloat
333 // __impl_slice_eq2! { [A; $N], &'b [B; $N] }
334 // __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
336 #[stable(feature = "rust1", since = "1.0.0")]
337 impl<T: Eq, const N: usize> Eq for [T; N] where [T; N]: LengthAtMost32 {}
339 #[stable(feature = "rust1", since = "1.0.0")]
340 impl<T: PartialOrd, const N: usize> PartialOrd for [T; N]
342 [T; N]: LengthAtMost32,
345 fn partial_cmp(&self, other: &[T; N]) -> Option<Ordering> {
346 PartialOrd::partial_cmp(&&self[..], &&other[..])
349 fn lt(&self, other: &[T; N]) -> bool {
350 PartialOrd::lt(&&self[..], &&other[..])
353 fn le(&self, other: &[T; N]) -> bool {
354 PartialOrd::le(&&self[..], &&other[..])
357 fn ge(&self, other: &[T; N]) -> bool {
358 PartialOrd::ge(&&self[..], &&other[..])
361 fn gt(&self, other: &[T; N]) -> bool {
362 PartialOrd::gt(&&self[..], &&other[..])
366 #[stable(feature = "rust1", since = "1.0.0")]
367 impl<T: Ord, const N: usize> Ord for [T; N]
369 [T; N]: LengthAtMost32,
372 fn cmp(&self, other: &[T; N]) -> Ordering {
373 Ord::cmp(&&self[..], &&other[..])
377 /// Implemented for lengths where trait impls are allowed on arrays in core/std
378 #[rustc_on_unimplemented(
379 message="arrays only have std trait implementations for lengths 0..=32",
381 #[unstable(feature = "const_generic_impls_guard", issue = "0",
382 reason = "will never be stable, just a temporary step until const generics are stable")]
383 pub trait LengthAtMost32 {}
385 macro_rules! array_impls {
386 ($($N:literal)+) => {
388 #[unstable(feature = "const_generic_impls_guard", issue = "0")]
389 impl<T> LengthAtMost32 for [T; $N] {}
396 10 11 12 13 14 15 16 17 18 19
397 20 21 22 23 24 25 26 27 28 29
401 // The Default impls cannot be generated using the array_impls! macro because
402 // they require array literals.
404 macro_rules! array_impl_default {
405 {$n:expr, $t:ident $($ts:ident)*} => {
406 #[stable(since = "1.4.0", feature = "array_default")]
407 impl<T> Default for [T; $n] where T: Default {
408 fn default() -> [T; $n] {
409 [$t::default(), $($ts::default()),*]
412 array_impl_default!{($n - 1), $($ts)*}
415 #[stable(since = "1.4.0", feature = "array_default")]
416 impl<T> Default for [T; $n] {
417 fn default() -> [T; $n] { [] }
422 array_impl_default!{32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T}