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
31 /// [`AsRef`]: ../convert/trait.AsRef.html
32 /// [`AsMut`]: ../convert/trait.AsMut.html
33 #[unstable(feature = "fixed_size_array", issue = "27778")]
34 pub unsafe trait FixedSizeArray<T> {
35 /// Converts the array to immutable slice
36 #[unstable(feature = "fixed_size_array", issue = "27778")]
37 fn as_slice(&self) -> &[T];
38 /// Converts the array to mutable slice
39 #[unstable(feature = "fixed_size_array", issue = "27778")]
40 fn as_mut_slice(&mut self) -> &mut [T];
43 #[unstable(feature = "fixed_size_array", issue = "27778")]
44 unsafe impl<T, A: Unsize<[T]>> FixedSizeArray<T> for A {
46 fn as_slice(&self) -> &[T] {
50 fn as_mut_slice(&mut self) -> &mut [T] {
55 /// The error type returned when a conversion from a slice to an array fails.
56 #[stable(feature = "try_from", since = "1.34.0")]
57 #[derive(Debug, Copy, Clone)]
58 pub struct TryFromSliceError(());
60 #[stable(feature = "core_array", since = "1.36.0")]
61 impl fmt::Display for TryFromSliceError {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 fmt::Display::fmt(self.__description(), f)
68 impl TryFromSliceError {
69 #[unstable(feature = "array_error_internals",
70 reason = "available through Error trait and this method should not \
75 pub fn __description(&self) -> &str {
76 "could not convert slice to array"
80 #[stable(feature = "try_from_slice_error", since = "1.36.0")]
81 impl From<Infallible> for TryFromSliceError {
82 fn from(x: Infallible) -> TryFromSliceError {
87 #[stable(feature = "rust1", since = "1.0.0")]
88 impl<T, const N: usize> AsRef<[T]> for [T; N]
90 [T; N]: LengthAtMost32,
93 fn as_ref(&self) -> &[T] {
98 #[stable(feature = "rust1", since = "1.0.0")]
99 impl<T, const N: usize> AsMut<[T]> for [T; N]
101 [T; N]: LengthAtMost32,
104 fn as_mut(&mut self) -> &mut [T] {
109 #[stable(feature = "array_borrow", since = "1.4.0")]
110 impl<T, const N: usize> Borrow<[T]> for [T; N]
112 [T; N]: LengthAtMost32,
114 fn borrow(&self) -> &[T] {
119 #[stable(feature = "array_borrow", since = "1.4.0")]
120 impl<T, const N: usize> BorrowMut<[T]> for [T; N]
122 [T; N]: LengthAtMost32,
124 fn borrow_mut(&mut self) -> &mut [T] {
129 #[stable(feature = "try_from", since = "1.34.0")]
130 impl<T, const N: usize> TryFrom<&[T]> for [T; N]
133 [T; N]: LengthAtMost32,
135 type Error = TryFromSliceError;
137 fn try_from(slice: &[T]) -> Result<[T; N], TryFromSliceError> {
138 <&Self>::try_from(slice).map(|r| *r)
142 #[stable(feature = "try_from", since = "1.34.0")]
143 impl<'a, T, const N: usize> TryFrom<&'a [T]> for &'a [T; N]
145 [T; N]: LengthAtMost32,
147 type Error = TryFromSliceError;
149 fn try_from(slice: &[T]) -> Result<&[T; N], TryFromSliceError> {
150 if slice.len() == N {
151 let ptr = slice.as_ptr() as *const [T; N];
154 Err(TryFromSliceError(()))
159 #[stable(feature = "try_from", since = "1.34.0")]
160 impl<'a, T, const N: usize> TryFrom<&'a mut [T]> for &'a mut [T; N]
162 [T; N]: LengthAtMost32,
164 type Error = TryFromSliceError;
166 fn try_from(slice: &mut [T]) -> Result<&mut [T; N], TryFromSliceError> {
167 if slice.len() == N {
168 let ptr = slice.as_mut_ptr() as *mut [T; N];
169 unsafe { Ok(&mut *ptr) }
171 Err(TryFromSliceError(()))
176 #[stable(feature = "rust1", since = "1.0.0")]
177 impl<T: Hash, const N: usize> Hash for [T; N]
179 [T; N]: LengthAtMost32,
181 fn hash<H: hash::Hasher>(&self, state: &mut H) {
182 Hash::hash(&self[..], state)
186 #[stable(feature = "rust1", since = "1.0.0")]
187 impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N]
189 [T; N]: LengthAtMost32,
191 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
192 fmt::Debug::fmt(&&self[..], f)
196 #[stable(feature = "rust1", since = "1.0.0")]
197 impl<'a, T, const N: usize> IntoIterator for &'a [T; N]
199 [T; N]: LengthAtMost32,
202 type IntoIter = Iter<'a, T>;
204 fn into_iter(self) -> Iter<'a, T> {
209 #[stable(feature = "rust1", since = "1.0.0")]
210 impl<'a, T, const N: usize> IntoIterator for &'a mut [T; N]
212 [T; N]: LengthAtMost32,
214 type Item = &'a mut T;
215 type IntoIter = IterMut<'a, T>;
217 fn into_iter(self) -> IterMut<'a, T> {
222 #[stable(feature = "rust1", since = "1.0.0")]
223 impl<A, B, const N: usize> PartialEq<[B; N]> for [A; N]
226 [A; N]: LengthAtMost32,
227 [B; N]: LengthAtMost32,
230 fn eq(&self, other: &[B; N]) -> bool {
231 self[..] == other[..]
234 fn ne(&self, other: &[B; N]) -> bool {
235 self[..] != other[..]
239 #[stable(feature = "rust1", since = "1.0.0")]
240 impl<A, B, const N: usize> PartialEq<[B]> for [A; N]
243 [A; N]: LengthAtMost32,
246 fn eq(&self, other: &[B]) -> bool {
247 self[..] == other[..]
250 fn ne(&self, other: &[B]) -> bool {
251 self[..] != other[..]
255 #[stable(feature = "rust1", since = "1.0.0")]
256 impl<A, B, const N: usize> PartialEq<[A; N]> for [B]
259 [A; N]: LengthAtMost32,
262 fn eq(&self, other: &[A; N]) -> bool {
263 self[..] == other[..]
266 fn ne(&self, other: &[A; N]) -> bool {
267 self[..] != other[..]
271 #[stable(feature = "rust1", since = "1.0.0")]
272 impl<'b, A, B, const N: usize> PartialEq<&'b [B]> for [A; N]
275 [A; N]: LengthAtMost32,
278 fn eq(&self, other: &&'b [B]) -> bool {
279 self[..] == other[..]
282 fn ne(&self, other: &&'b [B]) -> bool {
283 self[..] != other[..]
287 #[stable(feature = "rust1", since = "1.0.0")]
288 impl<'b, A, B, const N: usize> PartialEq<[A; N]> for &'b [B]
291 [A; N]: LengthAtMost32,
294 fn eq(&self, other: &[A; N]) -> bool {
295 self[..] == other[..]
298 fn ne(&self, other: &[A; N]) -> bool {
299 self[..] != other[..]
303 #[stable(feature = "rust1", since = "1.0.0")]
304 impl<'b, A, B, const N: usize> PartialEq<&'b mut [B]> for [A; N]
307 [A; N]: LengthAtMost32,
310 fn eq(&self, other: &&'b mut [B]) -> bool {
311 self[..] == other[..]
314 fn ne(&self, other: &&'b mut [B]) -> bool {
315 self[..] != other[..]
319 #[stable(feature = "rust1", since = "1.0.0")]
320 impl<'b, A, B, const N: usize> PartialEq<[A; N]> for &'b mut [B]
323 [A; N]: LengthAtMost32,
326 fn eq(&self, other: &[A; N]) -> bool {
327 self[..] == other[..]
330 fn ne(&self, other: &[A; N]) -> bool {
331 self[..] != other[..]
335 // NOTE: some less important impls are omitted to reduce code bloat
336 // __impl_slice_eq2! { [A; $N], &'b [B; $N] }
337 // __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
339 #[stable(feature = "rust1", since = "1.0.0")]
340 impl<T: Eq, const N: usize> Eq for [T; N] where [T; N]: LengthAtMost32 {}
342 #[stable(feature = "rust1", since = "1.0.0")]
343 impl<T: PartialOrd, const N: usize> PartialOrd for [T; N]
345 [T; N]: LengthAtMost32,
348 fn partial_cmp(&self, other: &[T; N]) -> Option<Ordering> {
349 PartialOrd::partial_cmp(&&self[..], &&other[..])
352 fn lt(&self, other: &[T; N]) -> bool {
353 PartialOrd::lt(&&self[..], &&other[..])
356 fn le(&self, other: &[T; N]) -> bool {
357 PartialOrd::le(&&self[..], &&other[..])
360 fn ge(&self, other: &[T; N]) -> bool {
361 PartialOrd::ge(&&self[..], &&other[..])
364 fn gt(&self, other: &[T; N]) -> bool {
365 PartialOrd::gt(&&self[..], &&other[..])
369 #[stable(feature = "rust1", since = "1.0.0")]
370 impl<T: Ord, const N: usize> Ord for [T; N]
372 [T; N]: LengthAtMost32,
375 fn cmp(&self, other: &[T; N]) -> Ordering {
376 Ord::cmp(&&self[..], &&other[..])
380 /// Implemented for lengths where trait impls are allowed on arrays in core/std
381 #[rustc_on_unimplemented(
382 message="arrays only have std trait implementations for lengths 0..=32",
384 #[unstable(feature = "const_generic_impls_guard", issue = "0",
385 reason = "will never be stable, just a temporary step until const generics are stable")]
386 pub trait LengthAtMost32 {}
388 macro_rules! array_impls {
389 ($($N:literal)+) => {
391 #[unstable(feature = "const_generic_impls_guard", issue = "0")]
392 impl<T> LengthAtMost32 for [T; $N] {}
399 10 11 12 13 14 15 16 17 18 19
400 20 21 22 23 24 25 26 27 28 29
404 // The Default impls cannot be generated using the array_impls! macro because
405 // they require array literals.
407 macro_rules! array_impl_default {
408 {$n:expr, $t:ident $($ts:ident)*} => {
409 #[stable(since = "1.4.0", feature = "array_default")]
410 impl<T> Default for [T; $n] where T: Default {
411 fn default() -> [T; $n] {
412 [$t::default(), $($ts::default()),*]
415 array_impl_default!{($n - 1), $($ts)*}
418 #[stable(since = "1.4.0", feature = "array_default")]
419 impl<T> Default for [T; $n] {
420 fn default() -> [T; $n] { [] }
425 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}