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 {
85 macro_rules! __impl_slice_eq1 {
86 ($Lhs: ty, $Rhs: ty) => {
87 __impl_slice_eq1! { $Lhs, $Rhs, Sized }
89 ($Lhs: ty, $Rhs: ty, $Bound: ident) => {
90 #[stable(feature = "rust1", since = "1.0.0")]
91 impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
93 fn eq(&self, other: &$Rhs) -> bool { self[..] == other[..] }
95 fn ne(&self, other: &$Rhs) -> bool { self[..] != other[..] }
101 macro_rules! __impl_slice_eq2 {
102 ($Lhs: ty, $Rhs: ty) => {
103 __impl_slice_eq2! { $Lhs, $Rhs, Sized }
105 ($Lhs: ty, $Rhs: ty, $Bound: ident) => {
106 __impl_slice_eq1!($Lhs, $Rhs, $Bound);
108 #[stable(feature = "rust1", since = "1.0.0")]
109 impl<'a, 'b, A: $Bound, B> PartialEq<$Lhs> for $Rhs where B: PartialEq<A> {
111 fn eq(&self, other: &$Lhs) -> bool { self[..] == other[..] }
113 fn ne(&self, other: &$Lhs) -> bool { self[..] != other[..] }
118 // macro for implementing n-element array functions and operations
120 macro_rules! array_impls {
123 #[stable(feature = "rust1", since = "1.0.0")]
124 impl<T> AsRef<[T]> for [T; $N] {
126 fn as_ref(&self) -> &[T] {
131 #[stable(feature = "rust1", since = "1.0.0")]
132 impl<T> AsMut<[T]> for [T; $N] {
134 fn as_mut(&mut self) -> &mut [T] {
139 #[stable(feature = "array_borrow", since = "1.4.0")]
140 impl<T> Borrow<[T]> for [T; $N] {
141 fn borrow(&self) -> &[T] {
146 #[stable(feature = "array_borrow", since = "1.4.0")]
147 impl<T> BorrowMut<[T]> for [T; $N] {
148 fn borrow_mut(&mut self) -> &mut [T] {
153 #[stable(feature = "try_from", since = "1.34.0")]
154 impl<T> TryFrom<&[T]> for [T; $N] where T: Copy {
155 type Error = TryFromSliceError;
157 fn try_from(slice: &[T]) -> Result<[T; $N], TryFromSliceError> {
158 <&Self>::try_from(slice).map(|r| *r)
162 #[stable(feature = "try_from", since = "1.34.0")]
163 impl<'a, T> TryFrom<&'a [T]> for &'a [T; $N] {
164 type Error = TryFromSliceError;
166 fn try_from(slice: &[T]) -> Result<&[T; $N], TryFromSliceError> {
167 if slice.len() == $N {
168 let ptr = slice.as_ptr() as *const [T; $N];
171 Err(TryFromSliceError(()))
176 #[stable(feature = "try_from", since = "1.34.0")]
177 impl<'a, T> TryFrom<&'a mut [T]> for &'a mut [T; $N] {
178 type Error = TryFromSliceError;
180 fn try_from(slice: &mut [T]) -> Result<&mut [T; $N], TryFromSliceError> {
181 if slice.len() == $N {
182 let ptr = slice.as_mut_ptr() as *mut [T; $N];
183 unsafe { Ok(&mut *ptr) }
185 Err(TryFromSliceError(()))
190 #[stable(feature = "rust1", since = "1.0.0")]
191 impl<T: Hash> Hash for [T; $N] {
192 fn hash<H: hash::Hasher>(&self, state: &mut H) {
193 Hash::hash(&self[..], state)
197 #[stable(feature = "rust1", since = "1.0.0")]
198 impl<T: fmt::Debug> fmt::Debug for [T; $N] {
199 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200 fmt::Debug::fmt(&&self[..], f)
204 #[stable(feature = "rust1", since = "1.0.0")]
205 impl<'a, T> IntoIterator for &'a [T; $N] {
207 type IntoIter = Iter<'a, T>;
209 fn into_iter(self) -> Iter<'a, T> {
214 #[stable(feature = "rust1", since = "1.0.0")]
215 impl<'a, T> IntoIterator for &'a mut [T; $N] {
216 type Item = &'a mut T;
217 type IntoIter = IterMut<'a, T>;
219 fn into_iter(self) -> IterMut<'a, T> {
224 // NOTE: some less important impls are omitted to reduce code bloat
225 __impl_slice_eq1! { [A; $N], [B; $N] }
226 __impl_slice_eq2! { [A; $N], [B] }
227 __impl_slice_eq2! { [A; $N], &'b [B] }
228 __impl_slice_eq2! { [A; $N], &'b mut [B] }
229 // __impl_slice_eq2! { [A; $N], &'b [B; $N] }
230 // __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
232 #[stable(feature = "rust1", since = "1.0.0")]
233 impl<T:Eq> Eq for [T; $N] { }
235 #[stable(feature = "rust1", since = "1.0.0")]
236 impl<T:PartialOrd> PartialOrd for [T; $N] {
238 fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
239 PartialOrd::partial_cmp(&&self[..], &&other[..])
242 fn lt(&self, other: &[T; $N]) -> bool {
243 PartialOrd::lt(&&self[..], &&other[..])
246 fn le(&self, other: &[T; $N]) -> bool {
247 PartialOrd::le(&&self[..], &&other[..])
250 fn ge(&self, other: &[T; $N]) -> bool {
251 PartialOrd::ge(&&self[..], &&other[..])
254 fn gt(&self, other: &[T; $N]) -> bool {
255 PartialOrd::gt(&&self[..], &&other[..])
259 #[stable(feature = "rust1", since = "1.0.0")]
260 impl<T:Ord> Ord for [T; $N] {
262 fn cmp(&self, other: &[T; $N]) -> Ordering {
263 Ord::cmp(&&self[..], &&other[..])
270 #[cfg(not(bootstrap))]
271 mod impls_using_const_generics {
274 #[stable(feature = "rust1", since = "1.0.0")]
275 impl<T, const N: usize> AsRef<[T]> for [T; N]
277 [T; N]: LengthAtMost32,
280 fn as_ref(&self) -> &[T] {
285 #[stable(feature = "rust1", since = "1.0.0")]
286 impl<T, const N: usize> AsMut<[T]> for [T; N]
288 [T; N]: LengthAtMost32,
291 fn as_mut(&mut self) -> &mut [T] {
296 #[stable(feature = "array_borrow", since = "1.4.0")]
297 impl<T, const N: usize> Borrow<[T]> for [T; N]
299 [T; N]: LengthAtMost32,
301 fn borrow(&self) -> &[T] {
306 #[stable(feature = "array_borrow", since = "1.4.0")]
307 impl<T, const N: usize> BorrowMut<[T]> for [T; N]
309 [T; N]: LengthAtMost32,
311 fn borrow_mut(&mut self) -> &mut [T] {
316 #[stable(feature = "try_from", since = "1.34.0")]
317 impl<T, const N: usize> TryFrom<&[T]> for [T; N]
320 [T; N]: LengthAtMost32,
322 type Error = TryFromSliceError;
324 fn try_from(slice: &[T]) -> Result<[T; N], TryFromSliceError> {
325 <&Self>::try_from(slice).map(|r| *r)
329 #[stable(feature = "try_from", since = "1.34.0")]
330 impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N]
332 [T; N]: LengthAtMost32,
334 type Error = TryFromSliceError;
336 fn try_from(slice: &[T]) -> Result<&[T; N], TryFromSliceError> {
337 if slice.len() == N {
338 let ptr = slice.as_ptr() as *const [T; N];
341 Err(TryFromSliceError(()))
346 #[stable(feature = "try_from", since = "1.34.0")]
347 impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N]
349 [T; N]: LengthAtMost32,
351 type Error = TryFromSliceError;
353 fn try_from(slice: &mut [T]) -> Result<&mut [T; N], TryFromSliceError> {
354 if slice.len() == N {
355 let ptr = slice.as_mut_ptr() as *mut [T; N];
356 unsafe { Ok(&mut *ptr) }
358 Err(TryFromSliceError(()))
363 #[stable(feature = "rust1", since = "1.0.0")]
364 impl<T: Hash, const N: usize> Hash for [T; N]
366 [T; N]: LengthAtMost32,
368 fn hash<H: hash::Hasher>(&self, state: &mut H) {
369 Hash::hash(&self[..], state)
373 #[stable(feature = "rust1", since = "1.0.0")]
374 impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N]
376 [T; N]: LengthAtMost32,
378 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
379 fmt::Debug::fmt(&&self[..], f)
383 #[stable(feature = "rust1", since = "1.0.0")]
384 impl<'a, T, const N: usize> IntoIterator for &'a [T; N]
386 [T; N]: LengthAtMost32,
389 type IntoIter = Iter<'a, T>;
391 fn into_iter(self) -> Iter<'a, T> {
396 #[stable(feature = "rust1", since = "1.0.0")]
397 impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]
399 [T; N]: LengthAtMost32,
401 type Item = &'a mut T;
402 type IntoIter = IterMut<'a, T>;
404 fn into_iter(self) -> IterMut<'a, T> {
409 #[stable(feature = "rust1", since = "1.0.0")]
410 impl<'a, 'b, A, B, const N: usize> PartialEq<[B; N]> for [A; N]
413 [A; N]: LengthAtMost32,
414 [B; N]: LengthAtMost32,
417 fn eq(&self, other: &[B; N]) -> bool {
418 self[..] == other[..]
421 fn ne(&self, other: &[B; N]) -> bool {
422 self[..] != other[..]
426 #[stable(feature = "rust1", since = "1.0.0")]
427 impl<'a, 'b, A, B, const N: usize> PartialEq<[B]> for [A; N]
430 [A; N]: LengthAtMost32,
433 fn eq(&self, other: &[B]) -> bool {
434 self[..] == other[..]
437 fn ne(&self, other: &[B]) -> bool {
438 self[..] != other[..]
442 #[stable(feature = "rust1", since = "1.0.0")]
443 impl<'a, 'b, A, B, const N: usize> PartialEq<[A; N]> for [B]
446 [A; N]: LengthAtMost32,
449 fn eq(&self, other: &[A; N]) -> bool {
450 self[..] == other[..]
453 fn ne(&self, other: &[A; N]) -> bool {
454 self[..] != other[..]
458 #[stable(feature = "rust1", since = "1.0.0")]
459 impl<'a, 'b, A, B, const N: usize> PartialEq<&'b [B]> for [A; N]
462 [A; N]: LengthAtMost32,
465 fn eq(&self, other: &&'b [B]) -> bool {
466 self[..] == other[..]
469 fn ne(&self, other: &&'b [B]) -> bool {
470 self[..] != other[..]
474 #[stable(feature = "rust1", since = "1.0.0")]
475 impl<'a, 'b, A, B, const N: usize> PartialEq<[A; N]> for &'b [B]
478 [A; N]: LengthAtMost32,
481 fn eq(&self, other: &[A; N]) -> bool {
482 self[..] == other[..]
485 fn ne(&self, other: &[A; N]) -> bool {
486 self[..] != other[..]
490 #[stable(feature = "rust1", since = "1.0.0")]
491 impl<'a, 'b, A, B, const N: usize> PartialEq<&'b mut [B]> for [A; N]
494 [A; N]: LengthAtMost32,
497 fn eq(&self, other: &&'b mut [B]) -> bool {
498 self[..] == other[..]
501 fn ne(&self, other: &&'b mut [B]) -> bool {
502 self[..] != other[..]
506 #[stable(feature = "rust1", since = "1.0.0")]
507 impl<'a, 'b, A, B, const N: usize> PartialEq<[A; N]> for &'b mut [B]
510 [A; N]: LengthAtMost32,
513 fn eq(&self, other: &[A; N]) -> bool {
514 self[..] == other[..]
517 fn ne(&self, other: &[A; N]) -> bool {
518 self[..] != other[..]
522 // NOTE: some less important impls are omitted to reduce code bloat
523 // __impl_slice_eq2! { [A; $N], &'b [B; $N] }
524 // __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
526 #[stable(feature = "rust1", since = "1.0.0")]
527 impl<T: Eq, const N: usize> Eq for [T; N] where [T; N]: LengthAtMost32 {}
529 #[stable(feature = "rust1", since = "1.0.0")]
530 impl<T: PartialOrd, const N: usize> PartialOrd for [T; N]
532 [T; N]: LengthAtMost32,
535 fn partial_cmp(&self, other: &[T; N]) -> Option<Ordering> {
536 PartialOrd::partial_cmp(&&self[..], &&other[..])
539 fn lt(&self, other: &[T; N]) -> bool {
540 PartialOrd::lt(&&self[..], &&other[..])
543 fn le(&self, other: &[T; N]) -> bool {
544 PartialOrd::le(&&self[..], &&other[..])
547 fn ge(&self, other: &[T; N]) -> bool {
548 PartialOrd::ge(&&self[..], &&other[..])
551 fn gt(&self, other: &[T; N]) -> bool {
552 PartialOrd::gt(&&self[..], &&other[..])
556 #[stable(feature = "rust1", since = "1.0.0")]
557 impl<T: Ord, const N: usize> Ord for [T; N]
559 [T; N]: LengthAtMost32,
562 fn cmp(&self, other: &[T; N]) -> Ordering {
563 Ord::cmp(&&self[..], &&other[..])
568 /// Implemented for lengths where trait impls are allowed on arrays in core/std
569 #[rustc_on_unimplemented(
570 message="arrays only have std trait implementations for lengths 0..=32",
572 #[unstable(feature = "const_generic_impls_guard", issue = "0",
573 reason = "will never be stable, just a temporary step until const generics are stable")]
574 #[cfg(not(bootstrap))]
575 pub trait LengthAtMost32 {}
577 #[cfg(not(bootstrap))]
578 macro_rules! array_impls {
579 ($($N:literal)+) => {
581 #[unstable(feature = "const_generic_impls_guard", issue = "0")]
582 impl<T> LengthAtMost32 for [T; $N] {}
589 10 11 12 13 14 15 16 17 18 19
590 20 21 22 23 24 25 26 27 28 29
594 // The Default impls cannot be generated using the array_impls! macro because
595 // they require array literals.
597 macro_rules! array_impl_default {
598 {$n:expr, $t:ident $($ts:ident)*} => {
599 #[stable(since = "1.4.0", feature = "array_default")]
600 impl<T> Default for [T; $n] where T: Default {
601 fn default() -> [T; $n] {
602 [$t::default(), $($ts::default()),*]
605 array_impl_default!{($n - 1), $($ts)*}
608 #[stable(since = "1.4.0", feature = "array_default")]
609 impl<T> Default for [T; $n] {
610 fn default() -> [T; $n] { [] }
615 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}