1 //! Implementations of things like `Eq` for fixed-length arrays
2 //! up to a certain length. Eventually, we should be 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::{self, Hash};
14 use crate::marker::Unsize;
15 use crate::slice::{Iter, IterMut};
19 #[unstable(feature = "array_value_iter", issue = "65798")]
20 pub use iter::IntoIter;
22 /// Utility trait implemented only on arrays of fixed size
24 /// This trait can be used to implement other traits on fixed-size arrays
25 /// without causing much metadata bloat.
27 /// The trait is marked unsafe in order to restrict implementors to fixed-size
28 /// arrays. User of this trait can assume that implementors have the exact
29 /// layout in memory of a fixed size array (for example, for unsafe
32 /// Note that the traits [`AsRef`] and [`AsMut`] provide similar methods for types that
33 /// may not be fixed-size arrays. Implementors should prefer those traits
36 /// [`AsRef`]: ../convert/trait.AsRef.html
37 /// [`AsMut`]: ../convert/trait.AsMut.html
38 #[unstable(feature = "fixed_size_array", issue = "27778")]
39 pub unsafe trait FixedSizeArray<T> {
40 /// Converts the array to immutable slice
41 #[unstable(feature = "fixed_size_array", issue = "27778")]
42 fn as_slice(&self) -> &[T];
43 /// Converts the array to mutable slice
44 #[unstable(feature = "fixed_size_array", issue = "27778")]
45 fn as_mut_slice(&mut self) -> &mut [T];
48 #[unstable(feature = "fixed_size_array", issue = "27778")]
49 unsafe impl<T, A: Unsize<[T]>> FixedSizeArray<T> for A {
51 fn as_slice(&self) -> &[T] {
55 fn as_mut_slice(&mut self) -> &mut [T] {
60 /// The error type returned when a conversion from a slice to an array fails.
61 #[stable(feature = "try_from", since = "1.34.0")]
62 #[derive(Debug, Copy, Clone)]
63 pub struct TryFromSliceError(());
65 #[stable(feature = "core_array", since = "1.36.0")]
66 impl fmt::Display for TryFromSliceError {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 fmt::Display::fmt(self.__description(), f)
73 impl TryFromSliceError {
75 feature = "array_error_internals",
76 reason = "available through Error trait and this method should not \
82 pub fn __description(&self) -> &str {
83 "could not convert slice to array"
87 #[stable(feature = "try_from_slice_error", since = "1.36.0")]
88 impl From<Infallible> for TryFromSliceError {
89 fn from(x: Infallible) -> TryFromSliceError {
94 #[stable(feature = "rust1", since = "1.0.0")]
95 impl<T, const N: usize> AsRef<[T]> for [T; N]
97 [T; N]: LengthAtMost32,
100 fn as_ref(&self) -> &[T] {
105 #[stable(feature = "rust1", since = "1.0.0")]
106 impl<T, const N: usize> AsMut<[T]> for [T; N]
108 [T; N]: LengthAtMost32,
111 fn as_mut(&mut self) -> &mut [T] {
116 #[stable(feature = "array_borrow", since = "1.4.0")]
117 impl<T, const N: usize> Borrow<[T]> for [T; N]
119 [T; N]: LengthAtMost32,
121 fn borrow(&self) -> &[T] {
126 #[stable(feature = "array_borrow", since = "1.4.0")]
127 impl<T, const N: usize> BorrowMut<[T]> for [T; N]
129 [T; N]: LengthAtMost32,
131 fn borrow_mut(&mut self) -> &mut [T] {
136 #[stable(feature = "try_from", since = "1.34.0")]
137 impl<T, const N: usize> TryFrom<&[T]> for [T; N]
140 [T; N]: LengthAtMost32,
142 type Error = TryFromSliceError;
144 fn try_from(slice: &[T]) -> Result<[T; N], TryFromSliceError> {
145 <&Self>::try_from(slice).map(|r| *r)
149 #[stable(feature = "try_from", since = "1.34.0")]
150 impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N]
152 [T; N]: LengthAtMost32,
154 type Error = TryFromSliceError;
156 fn try_from(slice: &[T]) -> Result<&[T; N], TryFromSliceError> {
157 if slice.len() == N {
158 let ptr = slice.as_ptr() as *const [T; N];
159 // SAFETY: ok because we just checked that the length fits
162 Err(TryFromSliceError(()))
167 #[stable(feature = "try_from", since = "1.34.0")]
168 impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N]
170 [T; N]: LengthAtMost32,
172 type Error = TryFromSliceError;
174 fn try_from(slice: &mut [T]) -> Result<&mut [T; N], TryFromSliceError> {
175 if slice.len() == N {
176 let ptr = slice.as_mut_ptr() as *mut [T; N];
177 // SAFETY: ok because we just checked that the length fits
178 unsafe { Ok(&mut *ptr) }
180 Err(TryFromSliceError(()))
185 #[stable(feature = "rust1", since = "1.0.0")]
186 impl<T: Hash, const N: usize> Hash for [T; N]
188 [T; N]: LengthAtMost32,
190 fn hash<H: hash::Hasher>(&self, state: &mut H) {
191 Hash::hash(&self[..], state)
195 #[stable(feature = "rust1", since = "1.0.0")]
196 impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N]
198 [T; N]: LengthAtMost32,
200 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201 fmt::Debug::fmt(&&self[..], f)
205 #[stable(feature = "rust1", since = "1.0.0")]
206 impl<'a, T, const N: usize> IntoIterator for &'a [T; N]
208 [T; N]: LengthAtMost32,
211 type IntoIter = Iter<'a, T>;
213 fn into_iter(self) -> Iter<'a, T> {
218 #[stable(feature = "rust1", since = "1.0.0")]
219 impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]
221 [T; N]: LengthAtMost32,
223 type Item = &'a mut T;
224 type IntoIter = IterMut<'a, T>;
226 fn into_iter(self) -> IterMut<'a, T> {
231 #[stable(feature = "rust1", since = "1.0.0")]
232 impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N]
235 [A; N]: LengthAtMost32,
236 [B; N]: LengthAtMost32,
239 fn eq(&self, other: &[B; N]) -> bool {
240 self[..] == other[..]
243 fn ne(&self, other: &[B; N]) -> bool {
244 self[..] != other[..]
248 #[stable(feature = "rust1", since = "1.0.0")]
249 impl<A, B, const N: usize> PartialEq<[B]> for [A; N]
252 [A; N]: LengthAtMost32,
255 fn eq(&self, other: &[B]) -> bool {
256 self[..] == other[..]
259 fn ne(&self, other: &[B]) -> bool {
260 self[..] != other[..]
264 #[stable(feature = "rust1", since = "1.0.0")]
265 impl<A, B, const N: usize> PartialEq<[A; N]> for [B]
268 [A; N]: LengthAtMost32,
271 fn eq(&self, other: &[A; N]) -> bool {
272 self[..] == other[..]
275 fn ne(&self, other: &[A; N]) -> bool {
276 self[..] != other[..]
280 #[stable(feature = "rust1", since = "1.0.0")]
281 impl<'b, A, B, const N: usize> PartialEq<&'b [B]> for [A; N]
284 [A; N]: LengthAtMost32,
287 fn eq(&self, other: &&'b [B]) -> bool {
288 self[..] == other[..]
291 fn ne(&self, other: &&'b [B]) -> bool {
292 self[..] != other[..]
296 #[stable(feature = "rust1", since = "1.0.0")]
297 impl<'b, A, B, const N: usize> PartialEq<[A; N]> for &'b [B]
300 [A; N]: LengthAtMost32,
303 fn eq(&self, other: &[A; N]) -> bool {
304 self[..] == other[..]
307 fn ne(&self, other: &[A; N]) -> bool {
308 self[..] != other[..]
312 #[stable(feature = "rust1", since = "1.0.0")]
313 impl<'b, A, B, const N: usize> PartialEq<&'b mut [B]> for [A; N]
316 [A; N]: LengthAtMost32,
319 fn eq(&self, other: &&'b mut [B]) -> bool {
320 self[..] == other[..]
323 fn ne(&self, other: &&'b mut [B]) -> bool {
324 self[..] != other[..]
328 #[stable(feature = "rust1", since = "1.0.0")]
329 impl<'b, A, B, const N: usize> PartialEq<[A; N]> for &'b mut [B]
332 [A; N]: LengthAtMost32,
335 fn eq(&self, other: &[A; N]) -> bool {
336 self[..] == other[..]
339 fn ne(&self, other: &[A; N]) -> bool {
340 self[..] != other[..]
344 // NOTE: some less important impls are omitted to reduce code bloat
345 // __impl_slice_eq2! { [A; $N], &'b [B; $N] }
346 // __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
348 #[stable(feature = "rust1", since = "1.0.0")]
349 impl<T: Eq, const N: usize> Eq for [T; N] where [T; N]: LengthAtMost32 {}
351 #[stable(feature = "rust1", since = "1.0.0")]
352 impl<T: PartialOrd, const N: usize> PartialOrd for [T; N]
354 [T; N]: LengthAtMost32,
357 fn partial_cmp(&self, other: &[T; N]) -> Option<Ordering> {
358 PartialOrd::partial_cmp(&&self[..], &&other[..])
361 fn lt(&self, other: &[T; N]) -> bool {
362 PartialOrd::lt(&&self[..], &&other[..])
365 fn le(&self, other: &[T; N]) -> bool {
366 PartialOrd::le(&&self[..], &&other[..])
369 fn ge(&self, other: &[T; N]) -> bool {
370 PartialOrd::ge(&&self[..], &&other[..])
373 fn gt(&self, other: &[T; N]) -> bool {
374 PartialOrd::gt(&&self[..], &&other[..])
378 #[stable(feature = "rust1", since = "1.0.0")]
379 impl<T: Ord, const N: usize> Ord for [T; N]
381 [T; N]: LengthAtMost32,
384 fn cmp(&self, other: &[T; N]) -> Ordering {
385 Ord::cmp(&&self[..], &&other[..])
389 /// Implemented for lengths where trait impls are allowed on arrays in core/std
390 #[rustc_on_unimplemented(message = "arrays only have std trait implementations for lengths 0..=32")]
392 feature = "const_generic_impls_guard",
394 reason = "will never be stable, just a temporary step until const generics are stable"
396 pub trait LengthAtMost32 {}
398 macro_rules! array_impls {
399 ($($N:literal)+) => {
401 #[unstable(feature = "const_generic_impls_guard", issue = "none")]
402 impl<T> LengthAtMost32 for [T; $N] {}
409 10 11 12 13 14 15 16 17 18 19
410 20 21 22 23 24 25 26 27 28 29
414 // The Default impls cannot be generated using the array_impls! macro because
415 // they require array literals.
417 macro_rules! array_impl_default {
418 {$n:expr, $t:ident $($ts:ident)*} => {
419 #[stable(since = "1.4.0", feature = "array_default")]
420 impl<T> Default for [T; $n] where T: Default {
421 fn default() -> [T; $n] {
422 [$t::default(), $($ts::default()),*]
425 array_impl_default!{($n - 1), $($ts)*}
428 #[stable(since = "1.4.0", feature = "array_default")]
429 impl<T> Default for [T; $n] {
430 fn default() -> [T; $n] { [] }
435 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}