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 macro_rules! __impl_slice_eq1 {
85 ($Lhs: ty, $Rhs: ty) => {
86 __impl_slice_eq1! { $Lhs, $Rhs, Sized }
88 ($Lhs: ty, $Rhs: ty, $Bound: ident) => {
89 #[stable(feature = "rust1", since = "1.0.0")]
90 impl<'a, 'b, A: $Bound, B> PartialEq<$Rhs> for $Lhs where A: PartialEq<B> {
92 fn eq(&self, other: &$Rhs) -> bool { self[..] == other[..] }
94 fn ne(&self, other: &$Rhs) -> bool { self[..] != other[..] }
99 macro_rules! __impl_slice_eq2 {
100 ($Lhs: ty, $Rhs: ty) => {
101 __impl_slice_eq2! { $Lhs, $Rhs, Sized }
103 ($Lhs: ty, $Rhs: ty, $Bound: ident) => {
104 __impl_slice_eq1!($Lhs, $Rhs, $Bound);
106 #[stable(feature = "rust1", since = "1.0.0")]
107 impl<'a, 'b, A: $Bound, B> PartialEq<$Lhs> for $Rhs where B: PartialEq<A> {
109 fn eq(&self, other: &$Lhs) -> bool { self[..] == other[..] }
111 fn ne(&self, other: &$Lhs) -> bool { self[..] != other[..] }
116 // macro for implementing n-element array functions and operations
117 macro_rules! array_impls {
120 #[stable(feature = "rust1", since = "1.0.0")]
121 impl<T> AsRef<[T]> for [T; $N] {
123 fn as_ref(&self) -> &[T] {
128 #[stable(feature = "rust1", since = "1.0.0")]
129 impl<T> AsMut<[T]> for [T; $N] {
131 fn as_mut(&mut self) -> &mut [T] {
136 #[stable(feature = "array_borrow", since = "1.4.0")]
137 impl<T> Borrow<[T]> for [T; $N] {
138 fn borrow(&self) -> &[T] {
143 #[stable(feature = "array_borrow", since = "1.4.0")]
144 impl<T> BorrowMut<[T]> for [T; $N] {
145 fn borrow_mut(&mut self) -> &mut [T] {
150 #[stable(feature = "try_from", since = "1.34.0")]
151 impl<T> TryFrom<&[T]> for [T; $N] where T: Copy {
152 type Error = TryFromSliceError;
154 fn try_from(slice: &[T]) -> Result<[T; $N], TryFromSliceError> {
155 <&Self>::try_from(slice).map(|r| *r)
159 #[stable(feature = "try_from", since = "1.34.0")]
160 impl<'a, T> TryFrom<&'a [T]> for &'a [T; $N] {
161 type Error = TryFromSliceError;
163 fn try_from(slice: &[T]) -> Result<&[T; $N], TryFromSliceError> {
164 if slice.len() == $N {
165 let ptr = slice.as_ptr() as *const [T; $N];
168 Err(TryFromSliceError(()))
173 #[stable(feature = "try_from", since = "1.34.0")]
174 impl<'a, T> TryFrom<&'a mut [T]> for &'a mut [T; $N] {
175 type Error = TryFromSliceError;
177 fn try_from(slice: &mut [T]) -> Result<&mut [T; $N], TryFromSliceError> {
178 if slice.len() == $N {
179 let ptr = slice.as_mut_ptr() as *mut [T; $N];
180 unsafe { Ok(&mut *ptr) }
182 Err(TryFromSliceError(()))
187 #[stable(feature = "rust1", since = "1.0.0")]
188 impl<T: Hash> Hash for [T; $N] {
189 fn hash<H: hash::Hasher>(&self, state: &mut H) {
190 Hash::hash(&self[..], state)
194 #[stable(feature = "rust1", since = "1.0.0")]
195 impl<T: fmt::Debug> fmt::Debug for [T; $N] {
196 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197 fmt::Debug::fmt(&&self[..], f)
201 #[stable(feature = "rust1", since = "1.0.0")]
202 impl<'a, T> IntoIterator for &'a [T; $N] {
204 type IntoIter = Iter<'a, T>;
206 fn into_iter(self) -> Iter<'a, T> {
211 #[stable(feature = "rust1", since = "1.0.0")]
212 impl<'a, T> IntoIterator for &'a mut [T; $N] {
213 type Item = &'a mut T;
214 type IntoIter = IterMut<'a, T>;
216 fn into_iter(self) -> IterMut<'a, T> {
221 // NOTE: some less important impls are omitted to reduce code bloat
222 __impl_slice_eq1! { [A; $N], [B; $N] }
223 __impl_slice_eq2! { [A; $N], [B] }
224 __impl_slice_eq2! { [A; $N], &'b [B] }
225 __impl_slice_eq2! { [A; $N], &'b mut [B] }
226 // __impl_slice_eq2! { [A; $N], &'b [B; $N] }
227 // __impl_slice_eq2! { [A; $N], &'b mut [B; $N] }
229 #[stable(feature = "rust1", since = "1.0.0")]
230 impl<T:Eq> Eq for [T; $N] { }
232 #[stable(feature = "rust1", since = "1.0.0")]
233 impl<T:PartialOrd> PartialOrd for [T; $N] {
235 fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
236 PartialOrd::partial_cmp(&&self[..], &&other[..])
239 fn lt(&self, other: &[T; $N]) -> bool {
240 PartialOrd::lt(&&self[..], &&other[..])
243 fn le(&self, other: &[T; $N]) -> bool {
244 PartialOrd::le(&&self[..], &&other[..])
247 fn ge(&self, other: &[T; $N]) -> bool {
248 PartialOrd::ge(&&self[..], &&other[..])
251 fn gt(&self, other: &[T; $N]) -> bool {
252 PartialOrd::gt(&&self[..], &&other[..])
256 #[stable(feature = "rust1", since = "1.0.0")]
257 impl<T:Ord> Ord for [T; $N] {
259 fn cmp(&self, other: &[T; $N]) -> Ordering {
260 Ord::cmp(&&self[..], &&other[..])
269 10 11 12 13 14 15 16 17 18 19
270 20 21 22 23 24 25 26 27 28 29
274 // The Default impls cannot be generated using the array_impls! macro because
275 // they require array literals.
277 macro_rules! array_impl_default {
278 {$n:expr, $t:ident $($ts:ident)*} => {
279 #[stable(since = "1.4.0", feature = "array_default")]
280 impl<T> Default for [T; $n] where T: Default {
281 fn default() -> [T; $n] {
282 [$t::default(), $($ts::default()),*]
285 array_impl_default!{($n - 1), $($ts)*}
288 #[stable(since = "1.4.0", feature = "array_default")]
289 impl<T> Default for [T; $n] {
290 fn default() -> [T; $n] { [] }
295 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}